Visibility of an object

  • Thread starter Thread starter Alberto
  • Start date Start date
A

Alberto

I have in a form a SqlConnection and a SqlCommand but I also want to use
them in form shown as DialogForm. I declared the objects as public but I
don't know how to go on.

Thank you.
 
Hi,

You have to pass a reference from the current form to the new form,
something like this:

Form2 f2 = new Form2( this ); // here I pass the reference to the current
form, where the Sql* are declared

f2.ShowDialog();

This is how Form2 looks like
class Form2{
Form1 parent

public Form2( Form1 parent){
this.parent = parent;
}

then in the Form2 you can use

parent.SqlCommand , etc

Cheers,
 
I finally did it passing at the constructor of the form2, references to the
connection and command objects.
Thanks anyway.
 
Hi alberto,

That is also a solution :)

Now, what I advise you is to use a aux class to hold these objects , if you
make this class a singleton it will be MUCH easier to you to use them,
otherwise you will have to pass them around to any other form that you use.


Cheers,
 
Back
Top