Solved

c# global object

Posted on 2011-02-23
12
420 Views
Last Modified: 2013-12-17
HI,

I create an object from form1, how can I refer it from form2?
Thanks,
JT
0
Comment
Question by:jtran007
12 Comments
 
LVL 52

Expert Comment

by:Carl Tawn
Comment Utility
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.
0
 
LVL 32

Expert Comment

by:it_saige
Comment Utility
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-
0
 

Author Comment

by:jtran007
Comment Utility
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
0
 
LVL 52

Expert Comment

by:Carl Tawn
Comment Utility
Right after initialization where? As long as it is after the call to InitializeComponent() it should be ok.
0
 
LVL 74

Expert Comment

by:käµfm³d 👽
Comment Utility
0
 

Author Comment

by:jtran007
Comment Utility
Hi,

In the maiform, right after initialization.

Regards,
JT
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 

Author Comment

by:jtran007
Comment Utility
Hi,

In fact, after InitializeComponent();

Regards,
JT
0
 
LVL 52

Expert Comment

by:Carl Tawn
Comment Utility
Can you post the code in question?
0
 

Author Comment

by:jtran007
Comment Utility
Hi,

In the form:

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

            Setup();
         }


Regards,
JT
0
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 250 total points
Comment Utility
It will. The TextBox is an instance member of the Form, you cannot access it from a static method.
0
 

Author Comment

by:jtran007
Comment Utility
HI,
Sorry I click the wrong button.
I want to credit this point to Carl.

Regards,
JT
0
 

Author Closing Comment

by:jtran007
Comment Utility
Thanks,
JT
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now