ADO.NET DATASET  
Author Message
old_nick12





PostPosted: Installing and Registering Visual Studio Express Editions, ADO.NET DATASET Top

How to pass DATASET by value in C#


Visual Studio Express Editions7  
 
 
Weston Hutchins





PostPosted: Installing and Registering Visual Studio Express Editions, ADO.NET DATASET Top

The DataSet class is a reference class and therefore gets treated differently than value types (i.e. ints, doubles, etc).

There are two ways in which you can pass a reference type into a function:
1) passing reference types by value
2) passing reference types

If you pass a reference type by value, you are simply saying that any changes you make to reassign the reference inside the method will not change the reference outside the function.

If you pass a reference type by reference (using the ref keyword) you are saying that you can reassign the pointer (or reference) to the object inside the method and have it persist outside the function.

More information can be found here: http://msdn.microsoft.com/library/default.asp url=/library/en-us/csref/html/vclrfPassingMethodParameters.asp

The best example is on the MSDN website:

// PassingParams6.cs
using System;
class SwappinStrings
{
  static void SwapStrings(ref string s1, ref string s2)
  // The string parameter x is passed by reference.
  // Any changes on parameters will affect the original variables.
  {
    string temp = s1;
    s1 = s2;
    s2 = temp;
    Console.WriteLine("Inside the method: {0}, {1}", s1, s2);
  }
  public static void Main()
  {
    string str1 = "John";
    string str2 = "Smith";
    Console.WriteLine("Inside Main, before swapping: {0} {1}", 
      str1, str2);
    SwapStrings(ref str1, ref str2);  // Passing strings by reference
    Console.WriteLine("Inside Main, after swapping: {0}, {1}", 
      str1, str2);
  }
}

Output

Inside Main, before swapping: John Smith
Inside the method: Smith, John
Inside Main, after swapping: Smith, John
 

Code Discussion

In this example, the parameters need to be passed by reference to affect the variables in the calling program. If you remove the ref keyword from both the method header and the method call, no changes will take place in the calling program.



 
 
old_nick12





PostPosted: Installing and Registering Visual Studio Express Editions, ADO.NET DATASET Top

This sample is good but...

Ok, I want to pass DataSet to the func. to change some properties and I do not want to change original Dataset.

Can I do it or not, if yes how.

It looks like you can not do it with ref. types.



 
 
Weston Hutchins





PostPosted: Installing and Registering Visual Studio Express Editions, ADO.NET DATASET Top

If you pass it into a function (by ref or value) you will modify the contents. Try creating a copy of the DataSet and passing that into the function. I believe you can use DataSet.Copy method to copy the structure and the data into another DataSet.