通过引用传递类对象(this) [英] Pass a class object (this) by reference

查看:67
本文介绍了通过引用传递类对象(this)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我需要通过引用另一个类中的方法

来传递一个类对象(this)。当我执行以下代码时,我得到错误(不能

传递''< this>''作为ref或out参数,因为它是只读的)..是否有

任何方式制作< this>不是只读的?


我简化了以下代码。有一个Task类和一个Data

类。我试图通过ref将任务类传递给数据类。


公共类任务

{

public Task( int taskId)

{

TaskData data = new TaskData();

data.GetTaskFromId(ref taskId);

data.Close();

}

}


公共类TaskData

{

public void GetTaskFromId(ref Task task)

{

SqlCommand cmd = new SqlCommand(" sp_Task_GetByID_Select",conn);

cmd.CommandType = CommandType.StoredProcedure;

cmd.Parameters.Add(new SqlParameter(" @ TaskID",task.TaskId));


SqlDataReader reader = cmd.ExecuteReader();

while(reader.Read())

{

task.Name = reader [" Name"]。ToString();

task.Description = reader [" Descr"]。ToString();

}


reader.Close();

cmd.Dispose();


}

}


所有的建议都是适用的。


谢谢!


-

Jay Douglas

Fort Collins,CO

Hello, I am needing to pass a class object (this) by reference to a method
in a different class. When I do the following code I get the error (Cannot
pass ''<this>'' as a ref or out argument because it is read-only) .. Is there
any way to make <this> not read-only?

I have simplified the following code. There is a Task class and a Data
Class. I am trying to pass the task class by ref to the data class.

public class Task
{
public Task(int taskId)
{
TaskData data = new TaskData();
data.GetTaskFromId(ref taskId);
data.Close();
}
}

public class TaskData
{
public void GetTaskFromId(ref Task task)
{
SqlCommand cmd = new SqlCommand("sp_Task_GetByID_Select", conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(new SqlParameter("@TaskID", task.TaskId));

SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
task.Name = reader["Name"].ToString();
task.Description = reader["Descr"].ToString();
}

reader.Close();
cmd.Dispose();

}
}

All suggestions are appreicated.

Thanks!

--
Jay Douglas
Fort Collins, CO


推荐答案

您尝试将ref传递给taskId,这是一个int。在您的GetTaskFromId

方法中,签名是Task类型。我想你可能想要

" data.GetTaskFromId(data);"你不需要参考给TaskData

访问该对象的成员。删除参考并在方法调用中将taskId更改为

数据,这应该可以做你想要的。

--wjs mvp


Jay Douglas < RE ********************************* @ squarei.com>写在

消息新闻:u0 ************** @ TK2MSFTNGP09.phx.gbl ...
Your trying to pass ref to taskId which is an int. In your GetTaskFromId
method, the signature is a Task type. I think you may want
"data.GetTaskFromId( data );" You don''t need the "ref" to give TaskData
access to members of this object. Remove the ref and change the taskId to
data in your method call, and this should do what you want.
--wjs mvp

"Jay Douglas" <RE*********************************@squarei.com > wrote in
message news:u0**************@TK2MSFTNGP09.phx.gbl...
你好,我需要通过引用另一个类中的方法来传递一个类对象(this)。当我执行以下代码时,我收到错误
(无法传递''< this>''作为ref或out参数,因为它是只读的)..
有什么方法可以<这>不是只读的?

我简化了以下代码。有一个Task类和一个Data
类。我试图通过引用将任务类传递给数据类。

公共类任务
{
公共任务(int taskId)
{
TaskData data = new TaskData();
data.GetTaskFromId(ref taskId);
data.Close();
}
}

公共课TaskData
{
public void GetTaskFromId(ref Task task)
{/ s> SqlCommand cmd = new SqlCommand(" sp_Task_GetByID_Select",conn);
cmd.CommandType = CommandType .StoredProcedure;
cmd.Parameters.Add(new SqlParameter(" @ TaskID",task.TaskId));

SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
task.Name = reader [" Name"]。ToString();
task.Description = reader [" Descr"]。ToString(阅读.Close();
br />所有的建议都是适用的。

谢谢!

-
Jay Dougl作为柯林斯堡,CO
Hello, I am needing to pass a class object (this) by reference to a method
in a different class. When I do the following code I get the error (Cannot pass ''<this>'' as a ref or out argument because it is read-only) .. Is there any way to make <this> not read-only?

I have simplified the following code. There is a Task class and a Data
Class. I am trying to pass the task class by ref to the data class.

public class Task
{
public Task(int taskId)
{
TaskData data = new TaskData();
data.GetTaskFromId(ref taskId);
data.Close();
}
}

public class TaskData
{
public void GetTaskFromId(ref Task task)
{
SqlCommand cmd = new SqlCommand("sp_Task_GetByID_Select", conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(new SqlParameter("@TaskID", task.TaskId));

SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
task.Name = reader["Name"].ToString();
task.Description = reader["Descr"].ToString();
}

reader.Close();
cmd.Dispose();

}
}

All suggestions are appreicated.

Thanks!

--
Jay Douglas
Fort Collins, CO



你知道吗,我匆忙分解了这段代码。我提供的代码是

不正确。请查看新代码中的错误无法通过< this>''作为

ref或out参数,因为它是只读的我调整的行是

" data.GetTaskFromId(this);"在Task类中。我的意思是不要双重

检查我的帖子。


我简化了以下代码。有一个Task类和一个Data

类。我试图通过ref将任务类传递给数据类。


公共类任务

{

public Task( int taskId)

{

TaskData data = new TaskData();

data.GetTaskFromId(this);

data.Close();

}

}


公共类TaskData

{

public void GetTaskFromId(ref任务任务)

{

SqlCommand cmd = new SqlCommand(" sp_Task_GetByID_Select",conn);

cmd.CommandType = CommandType.StoredProcedure;

cmd.Parameters.Add(new SqlParameter(" @ TaskID",task.TaskId));


SqlDataReader reader = cmd.ExecuteReader();

while(reader.Read())

{

task.Name = reader [" Name"]。ToString();

task.Description = reader [" Descr"]。ToString();

}


reader.Close();

cmd.Dispose();


}

}


-

Jay Douglas

Fort Collins,CO


" William Stacey" < ST *********** @ mvps.org>在消息中写道

新闻:%2 **************** @ TK2MSFTNGP12.phx.gbl ...
You know what, I hastily broke down that code. The code I provided is
incorrect. Please view the new code for the error "Cannot pass ''<this>'' as
a ref or out argument because it is read-only" The line that I adjusted is
"data.GetTaskFromId(this);" in the Task class. My appologies for not double
checking my post.

I have simplified the following code. There is a Task class and a Data
Class. I am trying to pass the task class by ref to the data class.

public class Task
{
public Task(int taskId)
{
TaskData data = new TaskData();
data.GetTaskFromId(this);
data.Close();
}
}

public class TaskData
{
public void GetTaskFromId(ref Task task)
{
SqlCommand cmd = new SqlCommand("sp_Task_GetByID_Select", conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(new SqlParameter("@TaskID", task.TaskId));

SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
task.Name = reader["Name"].ToString();
task.Description = reader["Descr"].ToString();
}

reader.Close();
cmd.Dispose();

}
}

--
Jay Douglas
Fort Collins, CO

"William Stacey" <st***********@mvps.org> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
你的尝试将ref传递给taskId,这是一个int。在GetTaskFromId
方法中,签名是Task类型。我想你可能想要
data.GetTaskFromId(data);你不需要参考给TaskData
访问该对象的成员。删除ref并将taskId更改为方法调用中的数据,这应该可以做你想要的。
--wjs mvp

Jay Douglas < RE ********************************* @ squarei.com>在
消息新闻中写道:u0 ************** @ TK2MSFTNGP09.phx.gbl ...
Your trying to pass ref to taskId which is an int. In your GetTaskFromId
method, the signature is a Task type. I think you may want
"data.GetTaskFromId( data );" You don''t need the "ref" to give TaskData
access to members of this object. Remove the ref and change the taskId to
data in your method call, and this should do what you want.
--wjs mvp

"Jay Douglas" <RE*********************************@squarei.com > wrote in
message news:u0**************@TK2MSFTNGP09.phx.gbl...
你好,我需要传递一个课程object(this)引用另一个类中的
方法。当我执行以下代码时,我得到错误
Hello, I am needing to pass a class object (this) by reference to a method in a different class. When I do the following code I get the error


(不能

传递''< this>''作为ref或out参数,因为它是只读的) ..
pass ''<this>'' as a ref or out argument because it is read-only) .. Is


任何方式制作< this>不是只读的?

我简化了以下代码。有一个Task类和一个Data
类。我试图通过引用将任务类传递给数据类。

公共类任务
{
公共任务(int taskId)
{
TaskData data = new TaskData();
data.GetTaskFromId(ref taskId);
data.Close();
}
}

公共课TaskData
{
public void GetTaskFromId(ref Task task)
{/ s> SqlCommand cmd = new SqlCommand(" sp_Task_GetByID_Select",conn);
cmd.CommandType = CommandType .StoredProcedure;
cmd.Parameters.Add(new SqlParameter(" @ TaskID",task.TaskId));

SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
task.Name = reader [" Name"]。ToString();
task.Description = reader [" Descr"]。ToString(阅读.Close();
br />所有的建议都是适用的。

谢谢!

-
Jay Dougl作为柯林斯堡,CO
any way to make <this> not read-only?

I have simplified the following code. There is a Task class and a Data
Class. I am trying to pass the task class by ref to the data class.

public class Task
{
public Task(int taskId)
{
TaskData data = new TaskData();
data.GetTaskFromId(ref taskId);
data.Close();
}
}

public class TaskData
{
public void GetTaskFromId(ref Task task)
{
SqlCommand cmd = new SqlCommand("sp_Task_GetByID_Select", conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(new SqlParameter("@TaskID", task.TaskId));

SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
task.Name = reader["Name"].ToString();
task.Description = reader["Descr"].ToString();
}

reader.Close();
cmd.Dispose();

}
}

All suggestions are appreicated.

Thanks!

--
Jay Douglas
Fort Collins, CO




Jay Douglas< RE ***** ****************************@squarei.com>写道:
Jay Douglas <RE*********************************@squarei.com > wrote:
你知道吗,我匆忙打破了那段代码。我提供的代码不正确。请查看新代码中的错误无法通过< this>''作为参考或输出参数,因为它是只读的我调整的行是
data.GetTaskFromId(this);在Task类中。我的意思是不要双重检查我的帖子。

我简化了以下代码。有一个Task类和一个Data
类。我试图通过引用将任务类传递给数据类。
You know what, I hastily broke down that code. The code I provided is
incorrect. Please view the new code for the error "Cannot pass ''<this>'' as
a ref or out argument because it is read-only" The line that I adjusted is
"data.GetTaskFromId(this);" in the Task class. My appologies for not double
checking my post.

I have simplified the following code. There is a Task class and a Data
Class. I am trying to pass the task class by ref to the data class.




我没看到*为什么*你试图通过引用传递它在第一个

的地方 - 你能解释一下吗?


你*可能*误解了参考的传递参数真的是

关于。请参阅
http://www.pobox.com/ ~sipet / csharp / parameters.html


-

Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet

如果回复该团体,请不要给我发邮件



I don''t see *why* you''re trying to pass it by reference in the first
place - could you explain?

You *may* misunderstand what passing parameters by reference is really
about. See
http://www.pobox.com/~skeet/csharp/parameters.html

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


这篇关于通过引用传递类对象(this)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆