|
-
August 19th, 2009, 03:42 AM
#1
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
-
August 19th, 2009, 11:43 AM
#2
Re: Confused about references
I don't understand. You're trying to assign table.Rows[i]["Value"] the value of temp inside CheckCellIntegrity() ?
-
August 19th, 2009, 01:02 PM
#3
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;
}
-
August 20th, 2009, 05:51 AM
#4
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
-
August 20th, 2009, 06:09 AM
#5
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"
-
August 20th, 2009, 07:47 AM
#6
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";
}
-
August 20th, 2009, 09:33 AM
#7
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.
-
August 20th, 2009, 09:42 AM
#8
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.
-
August 20th, 2009, 10:44 AM
#9
Re: Confused about references
 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.
-
August 21st, 2009, 12:38 AM
#10
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";
}
Last edited by vcdebugger; August 21st, 2009 at 12:41 AM.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|