Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing objects between winform objects

Tags:

c#

winforms

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;
  }
like image 242
John K. Avatar asked Feb 12 '26 13:02

John K.


2 Answers

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

like image 62
Wade73 Avatar answered Feb 15 '26 05:02

Wade73


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.

like image 34
Jon Skeet Avatar answered Feb 15 '26 04:02

Jon Skeet



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!