setting controls/values from one form to another

  • Thread starter Thread starter Milsnips
  • Start date Start date
M

Milsnips

Hi there,

Just doing a little testing and if i set a control or variable , say on
form2 (DataGrid1), then i type:

Form1 f1 = new Form1();
f1.DataGrid1.datasource = ......
f1.Show()
---------------------------------

(in Form2) i set:

private DataGrid DataGrid1; -->> public DataGrid DataGrid1;

but in order to access the datagrid from this form, i just changed it from
Private to Public. Is it okay to do it this way in order to access controls
on other forms?
Does anyone have a reason why not to do it like this:

thanks,
Paul
 
Milsnips said:
[...]
(in Form2) i set:

private DataGrid DataGrid1; -->> public DataGrid DataGrid1;

but in order to access the datagrid from this form, i just changed it from
Private to Public. Is it okay to do it this way in order to access
controls on other forms?

This will work, but it is a best practice to publish a property while
keeping the field itself private:

private DataGrid DataGrid1;
public DataGrid TheDataGrid
{
get { return DataGrid1; }
}

Notice that this is a read-only property. It still allows the DataSource
or other properties of the Grid to be modified from outside the form, but
will prevent the Grid itself to be replaced from the outside.
 
Ok thanks, i'l give that a try.

regards,
Paul

Alberto Poblacion said:
Milsnips said:
[...]
(in Form2) i set:

private DataGrid DataGrid1; -->> public DataGrid DataGrid1;

but in order to access the datagrid from this form, i just changed it
from Private to Public. Is it okay to do it this way in order to access
controls on other forms?

This will work, but it is a best practice to publish a property while
keeping the field itself private:

private DataGrid DataGrid1;
public DataGrid TheDataGrid
{
get { return DataGrid1; }
}

Notice that this is a read-only property. It still allows the
DataSource or other properties of the Grid to be modified from outside the
form, but will prevent the Grid itself to be replaced from the outside.
 
Back
Top