C# – Passing objects between winform objects

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;
  }

Best Solution

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

Related Question