Link to home
Start Free TrialLog in
Avatar of jtran007
jtran007

asked on

c# global object

HI,

I create an object from form1, how can I refer it from form2?
Thanks,
JT
Avatar of Carl Tawn
Carl Tawn
Flag of United Kingdom of Great Britain and Northern Ireland image

Depending on what it is you are trying to achieve, you would probably just be better of passing the object to the second form in its constructor.
As long as the object is declared publically you could use:

form1.objectname in form2.

This is not in anyway the recommended way of accomplishing this.  Carls suggestion is more accurate from a programming standpoint.

-saige-
Avatar of jtran007
jtran007

ASKER

Hi,

Thanks for your suggestion. Also I have a minor to ask. In the form1, I have define  
a textbox, right after the Initialization, I try copy the content of object to textbox.txt but
the compiler complains that textbox is not defined. I don't understand. Please help.
Thanks,
JT
Right after initialization where? As long as it is after the call to InitializeComponent() it should be ok.
Hi,

In the maiform, right after initialization.

Regards,
JT
Hi,

In fact, after InitializeComponent();

Regards,
JT
Can you post the code in question?
Hi,

In the form:

 static void Setup()
{
   this.textbox1.Text = "testing";   <--- compiler complains
}
 public Initialization()
        {
            InitializeComponent();

            Setup();
         }


Regards,
JT
ASKER CERTIFIED SOLUTION
Avatar of Carl Tawn
Carl Tawn
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
HI,
Sorry I click the wrong button.
I want to credit this point to Carl.

Regards,
JT
Thanks,
JT