Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Passing dataset by reference
Message
De
12/03/2012 12:11:34
 
 
Information générale
Forum:
ASP.NET
Catégorie:
Autre
Versions des environnements
Environment:
C# 2.0
Divers
Thread ID:
01538045
Message ID:
01538066
Vues:
47
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
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform