473,466 Members | 1,338 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Pass a class object (this) by reference

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


Nov 15 '05 #1
9 12858
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...
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

Nov 15 '05 #2
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...
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...
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


Nov 15 '05 #3
Jay Douglas <RE*********************************@squarei.com > wrote:
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.


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
Nov 15 '05 #4

"Jay Douglas" <RE*********************************@squarei.com > wrote in
message news:OQ**************@TK2MSFTNGP09.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...
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...
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


The problem might be the passing of the Task object before it is completely
constructed.

bob
Nov 15 '05 #5
Jon,

The reason I want to pass (this) is because the data class makes multiple
changes to the task object and I don't want to have to reset all the
properties back in the Task object. What I'm trying to avoid is needing to
do something like:

(This is how I currently do it with out the references, the data class
returns a new task object)
Task task data.GetTaskFromId(this);
this.Property1 = task.Property1
this.Property2 = task.Property2

I need to break out all of the data classes because a project implementation
may use the database (MS SQL, LDAP, MySQL) or the file system.

I would like to change (this) in another class without needing to reset all
of the properties. This is a has been a development issue for me across the
board. I'm not opposed to a different development pattern to address this
issue. I would just like to come up with something.

All suggestions are appreciated.
--
Jay Douglas
Fort Collins, CO

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Jay Douglas <RE*********************************@squarei.com > wrote:
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.


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

Nov 15 '05 #6
Jay Douglas <RE*********************************@squarei.com > wrote:
The reason I want to pass (this) is because the data class makes multiple
changes to the task object and I don't want to have to reset all the
properties back in the Task object.
You don't. As I thought, you don't quite understand what pass-by-
reference does, or what reference types are in .NET. Read the page I
linked to before.

<snip>
I would like to change (this) in another class without needing to reset all
of the properties.


You can change the properties *of* "this" but you can't change which
object "this" is a reference to. It doesn't really make sense (IMO) to
be able to do

this = new Task();

which is what you'd be allowing yourself to do if you could pass this
by reference.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #7
You still don't need the "ref" in there, unless I miss what your trying to
do. Remove the ref and see if that does what you need.

"Jay Douglas" <RE*********************************@squarei.com > wrote in
message news:OQ**************@TK2MSFTNGP09.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...
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...
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



Nov 15 '05 #8
100
Hi Jay,
Just remove the ref modifier form the task parameter of GetTaskFromId
method. Since Task is a class (reference type) you pass references to its
instances anyway. Making task parameter *ref* means that changing the value
of the formal parameter (task) will change the value of the actual parameter
used for the method call. You cannot change *this*. That's why the compiler
reports an error and says that *this* is readonly.

So, once again my siggestion is to remove the *ref* modifier from the *task*
parameter of GetTaskFromId method.

HTH
B\rgds
100

"Jay Douglas" <RE*********************************@squarei.com > wrote in
message news:OQ**************@TK2MSFTNGP09.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...
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...
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



Nov 15 '05 #9
Jon,
It looks like I was really missing the big picture. You advise was
really helpful and gave me a sold answer. Here is the modified code that
works:

public class Task
{
public Task(int taskId)
{
this.taskId = taskId;

TaskData data = new TaskData();
data.GetTaskFromId(this);
data.Close();

Console.WriteLine(this.Name); // Out puts the change
// that the data class
// made to the Task object
}
}

public class TaskData
{
internal void GetTaskFromId(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.TaskId = Convert.ToInt32(reader["TaskId"]);
task.Name = reader["Name"].ToString();
task.Description = reader["Descr"].ToString();
}

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

}
}

--
Jay Douglas
Fort Collins, CO

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Jay Douglas <RE*********************************@squarei.com > wrote:
The reason I want to pass (this) is because the data class makes multiple changes to the task object and I don't want to have to reset all the
properties back in the Task object.


You don't. As I thought, you don't quite understand what pass-by-
reference does, or what reference types are in .NET. Read the page I
linked to before.

<snip>
I would like to change (this) in another class without needing to reset all of the properties.


You can change the properties *of* "this" but you can't change which
object "this" is a reference to. It doesn't really make sense (IMO) to
be able to do

this = new Task();

which is what you'd be allowing yourself to do if you could pass this
by reference.

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

Nov 15 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Vanessa | last post by:
Hi, I would like to know whether there's any way for me to pass an object by reference to another form? Regards Vanessa
1
by: Wayne | last post by:
How do you pass an object reference to a property during design? In other words, I would like to type the name of the object in on the properties window and then use that object to do neat stuff....
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.