andre prins

andre prins

  • NA
  • 2
  • 0

Visual C# 2008 express edition - newbie question

Feb 24 2009 3:45 AM

I can build windows forms applications, and know about the various commands, but I am from vbscript / pascal origin, so the OOP programming style is something I have to get used to.

There is one very basic understanding I would like to verify if the assumption below is correct.

I can store my information in a class i.e. "employee"

but in the visual edition it generates a lot of code, and the Main method calls Application.Run(new Form1());  and never gets back to Main till you close form1. My problem lies in "global" variables - where do I store my data so that I can access it in all the various methods (i.e. click events)
If I define a variable in one of the form methods (i.e. click on the "File, Open" menu or click on "button1") that variable is only available in that method and nowhere else, so clicking "file close" needs to reference data that I collected in another method. And the way those methods are called, they do not pass on my "own" class object or other data....

so what I want to verify with you visual C# guru's :
Am I right in my assumption, that I have to see the form1 class also in the same way as my employee class :

To create my "global variables" so I can access / update them in all the events that my form can generate, it should become something like this ?

public partial class Form1:Form
{
      private employee myemp = new employee();   (my "global" variables so I can access it in all the events)
      private int myint;

      public form1()     (is this the default constructor for form1, so should / could it also contain the default constructor for myint and myemp?)
      {   InitializeComponent();}
      public form1(int number)    another constructor  
      {  myint = number;}    (and should this constructor also always call InitializeComponent too ??)
      etc..... 

      private void button1_click(object sender, EventArgs e)
      {
            myemp.name = "john.Doe";
      }
}

 

 


Answers (1)