Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Passing dataset by reference
Message
From
12/03/2012 12:11:34
 
General information
Forum:
ASP.NET
Category:
Other
Environment versions
Environment:
C# 2.0
Miscellaneous
Thread ID:
01538045
Message ID:
01538066
Views:
48
Dmitry,

You can pass a DataSet by reference without using the "out" keyword. The thing to be wary of when doing so is that you cannot create a new instance of that DataSet in your method. Once you do that, the reference is lost. Look at it this way:
DataSet ds = new DataSet();
ds is in a particular spot in memory.
MyClass.MyMethod(ds);
Now you've told MyMethod where that ds is, its particular spot in memory.
MyClass.MyMethod( DataSet ds)
{
     // for testing
     ds = null;
}
Now you've created a new instance, it has a totally different spot in memory.

So when you test ds for null:
if (ds =!null)
That first ds is still in its original spot in memory. That's what you're testing. Not the new ds that got created in your method in a totally different spot in memory.

If you want to create a new instance of an object in a method and pass that object in a parameter, then you have to use "out" (as Viv showed). If you don't create a new instance of that object, then you're just passing the reference to its spot in memory and all is good ... no "out" necessary.

Make sense?

~~Bonnie
Bonnie Berent DeWitt
NET/C# MVP since 2003

http://geek-goddess-bonnie.blogspot.com
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform