I'm getting an error when I passed by ref a dataset to a new form

G

Guest

Why am I getting an error when I passed by ref a dataset to a new form? The
message reads "cannot convert from 'ref Paimport.unixAccount' to 'ref
System.Data.DataSet'.
Paimport is the name of the form where the dataset UnixAccount is created.
I created the dataset using the designer tool and not by code. So it's like
a type or class which I then declare an instance of it :
UnixAccoutns dsAccounts = new UnixAccoutns ();

I pass to a new form in the construction as follow:
filterDlg = new frmFilter(ref dsAccounts, local, this);

How can I pass the entire dataset with all the datatables and the data in it
so I don't have to pass one table at a time? Thank you.
 
J

Jon Skeet [C# MVP]

Pucca said:
Why am I getting an error when I passed by ref a dataset to a new form? The
message reads "cannot convert from 'ref Paimport.unixAccount' to 'ref
System.Data.DataSet'.

Parameters passed by reference have to match type exactly.
Paimport is the name of the form where the dataset UnixAccount is created.
I created the dataset using the designer tool and not by code. So it's like
a type or class which I then declare an instance of it :
UnixAccoutns dsAccounts = new UnixAccoutns ();

I pass to a new form in the construction as follow:
filterDlg = new frmFilter(ref dsAccounts, local, this);

How can I pass the entire dataset with all the datatables and the data in it
so I don't have to pass one table at a time? Thank you.

My guess is that you really don't need to pass it by reference at all.
Why do you think you do?
See http://pobox.com/~skeet/csharp/parameters.html
 
G

Guest

I thought I have to pass by ref so some of the tables in the dataset can have
new data insert to it. Is that not true? Thank you.
 
P

Peter Duniho

I thought I have to pass by ref so some of the tables in the dataset can
have new data insert to it. Is that not true? Thank you.

That is not true.

You would have to pass as "ref" if you needed to modify the actual
reference to the instance. That is, you wanted to change the variable
from referencing one DataSet to referencing a different DataSet.

But if you simply want to modify the existing DataSet, passing the
reference by value is fine and in fact is what would normally be done.
See Jon's link for more details.

Pete
 
G

Guest

Yes, that does clear the error. And thank you for the link and the
explanation. Both were very helpful. That finally correct my wrong
understanding of ref type. Thank you both.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top