RE: Application-wide Objects in Windows Forms Apps
- From: v-jzho@xxxxxxxxxxxxxxxxxxxx ("\"Ji Zhou [MSFT]\"")
- Date: Mon, 06 Oct 2008 07:31:12 GMT
Hello Alex,
Thanks for using Microsoft Newsgroup Support Service, my name is Ji Zhou
[MSFT] and I will be working on this issue with you.
Based on my understanding on your post, the objective is creating a global
accessible object that can be consumed in every forms in our windows form
application, right? If I have misunderstood, please feel free to let me
know.
Actually, if we want to share an object globally, we can put it in the
Program class as an public static class variable. The Program class is used
to hold the Main() function which is the entry of the whole application.
The following is what I have done on my side, and it works fine. Please let
me know if this addresses your issue.
1.I create a CustomClass using these codes,
class CustomClass
{
int x;
int y;
public CustomClass(int a, int b)
{
x = a;
y = b;
}
public int Add()
{
return x + y;
}
}
2.Create a public static class variable in the Program.cs file before the
Main function,
static class Program
{
public static CustomClass c = new CustomClass(3,4);
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
3.Then, we can access the variable c anywhere in the windows form
application project,
private void Form1_Load(object sender, EventArgs e)
{
Debug.Print(Program.c.Add().ToString());
Form2 f = new Form2();
f.Show();
}
private void Form2_Load(object sender, EventArgs e)
{
Program.c.Add();
}
Please let me know if you have any other questions or concerns on this, and
I will try my best to provide future help!
Best regards,
Ji Zhou (v-jzho@xxxxxxxxxxxxxxxxxxxx, remove 'online.')
Microsoft Online Community Support
Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
msdnmg@xxxxxxxxxxxxxx
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.
Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://support.microsoft.com/select/default.aspx?target=assistance&ln=en-us.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
.
- Follow-Ups:
- RE: Application-wide Objects in Windows Forms Apps
- From: Alex Maghen
- RE: Application-wide Objects in Windows Forms Apps
- References:
- Application-wide Objects in Windows Forms Apps
- From: Alex Maghen
- Application-wide Objects in Windows Forms Apps
- Prev by Date: Re: Application-wide Objects in Windows Forms Apps
- Next by Date: Re: Trouble getting a row add to cancel in DataGridView
- Previous by thread: Re: Application-wide Objects in Windows Forms Apps
- Next by thread: RE: Application-wide Objects in Windows Forms Apps
- Index(es):
Relevant Pages
|
Loading