Windows Develop Bookmark and Share   
 index > Windows Forms General > simple c# question
 

simple c# question

hi all.

this is a very basic c# question.

in my c# WinForm application i have 2 forms and the program.cs file.

how can i declare a member in the program.cs class and let all my form's recognize it?

shimshon  Saturday, January 13, 2007 7:00 PM

you would need to pass a reference of that class into the winforms constructor and then set that reference in a private global field. Example:

 

//winform:

 

private Program theProgramRef = null; //global

public Form1(Program programRef)

{

   this.theProgramRef = programRef;

   InitializeComponent();

}

 

then in program.cs.....

 

..

Application.Run(new Form1(this));

 

and remember, make any methods or variables as public so that the class (form) can see them

 

This will now have your form be able to access the program class file, the same applies for any class file that you want to access from another class file, you need to pass in a reference as well as making any methods or variables public so that the class that wishes to access them can access them.

so now to make that method or variable public you do this... example in the program class file..

 

private string myString = string.Empty;

//now we need to expose the mString variable public...via a property accessor:

public string MyString

{

   get { return this.myString; }

   set { this.myString = value; }

}

 

..rest of code here

 

so now from your form class, since we passed in that class reference, we simply do this to access the myString property in the program class file in the form class:

 

this.theProgramRef.MyString = "hello";

//mystring should now be "hello"

 

MessageBox.Show(this.theProgramRef.MyString);

 

be noted however, its best practice to use a different class file than the program file since that is really used for kicking up your application to life, you should use a new different class file for whatever you want to use it for

I hope this helps

ahmedilyas  Saturday, January 13, 2007 7:43 PM

you would need to pass a reference of that class into the winforms constructor and then set that reference in a private global field. Example:

 

//winform:

 

private Program theProgramRef = null; //global

public Form1(Program programRef)

{

   this.theProgramRef = programRef;

   InitializeComponent();

}

 

then in program.cs.....

 

..

Application.Run(new Form1(this));

 

and remember, make any methods or variables as public so that the class (form) can see them

 

This will now have your form be able to access the program class file, the same applies for any class file that you want to access from another class file, you need to pass in a reference as well as making any methods or variables public so that the class that wishes to access them can access them.

so now to make that method or variable public you do this... example in the program class file..

 

private string myString = string.Empty;

//now we need to expose the mString variable public...via a property accessor:

public string MyString

{

   get { return this.myString; }

   set { this.myString = value; }

}

 

..rest of code here

 

so now from your form class, since we passed in that class reference, we simply do this to access the myString property in the program class file in the form class:

 

this.theProgramRef.MyString = "hello";

//mystring should now be "hello"

 

MessageBox.Show(this.theProgramRef.MyString);

 

be noted however, its best practice to use a different class file than the program file since that is really used for kicking up your application to life, you should use a new different class file for whatever you want to use it for

I hope this helps

ahmedilyas  Saturday, January 13, 2007 7:43 PM

google singleton pattern

buy

Design Patterns

also get

Refactoring: Improving the Design of Existing Code

Blair Allen Stark  Saturday, January 13, 2007 7:59 PM

You can use google to search for other answers

Custom Search

More Threads

• Compact & Repair Database
• some problem arised while removing a group from the listview ?
• connecting to a remote sql server database from form
• Tables in a RichTextBox
• Last chance exception caught in Main() : does not work outside VS?
• Print HTML
• Comparing strings
• TreeView - Expandable Nodes
• How to add images to toolstrip menu ?
• Scroll bar doesn't scroll far enough to see all controls