J
Jay Douglas
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!
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!