Solved

c# global object

Posted on 2011-02-23
12
432 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
12 Comments
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 34965099
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 34

Expert Comment

by:it_saige
ID: 34965341
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
ID: 34965388
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
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 
LVL 52

Expert Comment

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

Expert Comment

by:käµfm³d 👽
ID: 34965491
0
 

Author Comment

by:jtran007
ID: 34965553
Hi,

In the maiform, right after initialization.

Regards,
JT
0
 

Author Comment

by:jtran007
ID: 34965563
Hi,

In fact, after InitializeComponent();

Regards,
JT
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 34965574
Can you post the code in question?
0
 

Author Comment

by:jtran007
ID: 34965708
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
ID: 34965716
It will. The TextBox is an instance member of the Form, you cannot access it from a static method.
0
 

Author Comment

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

Regards,
JT
0
 

Author Closing Comment

by:jtran007
ID: 34966655
Thanks,
JT
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

617 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