Confused about references
Hi all,
I want to modify the contents of a DataTable cell in a method. I assumed that just passing table.Rows[i]["Value"] would be enough. There is no passing by value in this case. I figured that this is actually a method returning a reference as result and as such I would be able to modify the cell contents.
This didn't work and I don't understand why. I tried passing a reference explicitly but that didn't work neither becasue you can not reference an indexer.
In the end I got something working but I got confused about references in this particular instance. This is how I got it working:
Code:
Object temp = table.Rows[i]["Value"];
CheckCellIntegrity(ref temp, table.Rows[i]["Type"].ToString(), i+1);
table.Rows[i]["Value"] = temp;
Thanks in advance for the clarification,
Jef
Re: Confused about references
I don't understand. You're trying to assign table.Rows[i]["Value"] the value of temp inside CheckCellIntegrity() ?
Re: Confused about references
Why not just pass in the row item to the check method?
Code:
Object temp = table.Rows[ i ]["Value"];
if( CheckCellIntegrity( table.Rows[ i ], i+1 ) )
{
table.Rows[ i ]["Value"] = temp;
}
Re: Confused about references
Well, suppose I know that a certain column contains strings and I want to modify a cell of the table inside a method, I do this:
Code:
table.Rows[ i ]["Value"] = "test 1";
Method(table.Rows[ i ]["Value"]);
with
Code:
void Method(Object cell)
{
object = "test 2";
}
When I check the value of table.Rows[ i ]["Value"] after the execution of method it contains "test 1". Why?
Jef
Re: Confused about references
Code:
void Method(Object cell)
{
object = "test 2";
}
what you are assigning is to object but you should assigning it to the instance of it i.e, "cell"
Re: Confused about references
Sorry, I made a mistake in my example, this wouldn't even compile. But with this code I have the same problem
Code:
void Method(Object cell)
{
cell = "test 2";
}
Re: Confused about references
In general, you'll want to specify a specific type as a method parameter.
Object is the base type for all objects, so the compiler doesn't know if it's a type that can contain a string or not.
Re: Confused about references
table.Rows[ i ]["Value"] is of the type object so I must pass it that way. The compiler allows assigning a string to object, this seems logical since String is inherited from Object.
Your reasoning would imply that it is impossible to pass a cell and modify it, I can hardly believe that. I still feel like I'm missing something.
Re: Confused about references
Quote:
Originally Posted by
Jef Patat
Your reasoning would imply that it is impossible to pass a cell and modify it, I can hardly believe that. I still feel like I'm missing something.
You misunderstand. I'm suggesting to pass in a strong type.
This won't work:
Code:
void Method(Object cell)
{
cell = "test 2";
}
these will
Code:
void Method(string cell)
{
cell = "test 2";
}
void Method(int cell)
{
cell = 1;
}
My reasoning is that you should avoid working with objects as much as possible because the compiler can't help you detect type mismatches.
If you used strongly typed parameters in your methods, then the compiler can catch mismatches; otherwise, you are going to have runtime errors.
Re: Confused about references
Code:
Object temp = table.Rows[i]["Value"];
CheckCellIntegrity(ref temp, table.Rows[i]["Type"].ToString(), i+1);
table.Rows[i]["Value"] = temp;
the keyword "ref" might be the one which is making it to work as it is actually pointing to the exact memory location where temp is present.
so try using "ref" keyword in the method function and also at the place where you are calling the method function :
Code:
void Method(ref Object cell)
{
cell = "test 2";
}