tags:

views:

225

answers:

2

Hello,

I'm hoping someone has come across this same predicament I've encountered. I'm developing a .NET 2.0 Winforms application and am trying to exchange an ADO.NET datatable object between two different winforms.

I've got a button on form1 that when clicked, instantiates a different form object and shows it modally. The second modal form allows the user to run some search criteria and bring back an ado.net datatable of search results.

When the user closes the modal form, I want the Datatable of search results to get passed back into the original form, but as I'm stepping through the code, I'm seeing the original empty datatable.

So the second form has a custom constructor where I am trying to pass in the datatable that I am interested in exchanging between both forms.

My understanding is that when you pass an object in as a parameter into a function or constructor, you are in the "by reference" mode and you are manipulating the original contents of the object? But that is not what seems to be happening here. Any insight would be much appreciated.

thanks in advance.

// button click handler code in Form1
 DataTable searchResults = new DataTable();
 Search searchForm = new Search(this.DropdownDataset, searchResults);
 searchForm.ShowDialog(this);

// custom winform constructor code in Form2
  public Search(DataSet dropdownData, DataTable searchResults)
  {
       this.InitializeComponent();
       this._dropdownData = dropdownData;
       this._lidSearch = new LIDSearch();
       this._searchResults = searchResults;
  }
+1  A: 

Passing by reference isn't quite the same as passing a reference by value, but in this case I don't think you'll need to worry about it.

You haven't shown how you're trying to "fetch" the search results afterwards. So far it looks okay, but if you could show the "passing back" part that would help. A short but complete example (e.g. just adding a dummy record to the DataTable) would help even more.

Jon Skeet
In addition to Jon's answer, you have to make sure you are not reassigning the `DataTable` reference to another object in the process (i.e. changing the data table shouldn't mean creating a new table with modified contents like `this._searchResults = new DataTable()`)
Mehrdad Afshari
@Wade73: No, objects aren't passed *at all*; reference type parameters and value type parameters are always passed by value by default. Please read the link in my answer.
Jon Skeet
+1 for correcting the fool. I apologize for being wrong (looked the answer up on Google), but at one time I thought I found information saying this was the behavior in .Net
Wade73
@Wade73: I'm sure you did. It's an unfortunate myth which is widely propagated. For many people it doesn't matter, but I'm a stickler for correct terminology - because for people from a C++ background (or similar) it really matters a lot.
Jon Skeet
+1  A: 

You need to use this._searchResults.merge(searchResults);

Wade73
That did it!!!! Much appreciated, Wade73! You guys rock! :)
John K.
Could you kindly mark as answer? Thanks.
Wade73
Still a little new to stackoverflow, thanks for the reminder...just marked it as answered by you, thanks...
John K.
An explanation would be helpful too - I suspect there's a cleaner way of doing this without merging anything... but we need more code to see it...
Jon Skeet
Jon, I wish I had one. I ran into this problem and the solution I found had no explanation. Though this post has perked my interest about it.
Wade73
Wade73, this is only a theory, but could it be that the reason I need to call the Merge() method of the DataTable object is because Merge() is copying the datatable schema/data BACK to the original empty DataTable() that was instantiated in the button click handler code of Form1? Just a thought...
John K.
@John K: If you could provide a short but complete program which demonstrates the issue (just with sample hard-coded data) I'd be very happy to try to explain the necessity for Merge - or (preferably) remove it completely. You still haven't shown how you get data back from the second form.
Jon Skeet