Solved

c# global object

Posted on 2011-02-23
12
431 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
Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
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.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

738 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