setting controls/values from one form to another

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
 
A

Alberto Poblacion

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.
 
M

Milsnips

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.
 

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