Solved

c# global object

Posted on 2011-02-23
12
430 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
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 33

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
How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

 
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

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

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

Suggested Solutions

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

820 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