Home Page
"Join my free Amazing ASP.NET newsletter"
It's safe and easy to signup. You get:
  • Notified anytime a new article comes out
  • Technical step-by-step tutorials on all important ASP.NET topics
  • Expert guidance so you can reach unlimited ASP.NET success
  • Important ASP.NET news updates
Enter your Email

Click here to download the sample source code for this Class variables lab exercise 

We saw in the previous exercises that Classes can contain Methods.  Classes can also contain what are called Instance Variables.  These types of variables are also referred to as Class Variables and Member Variables.  Instance variables are declared at the top of the Class and can be used by all the Methods inside the class.  Instance variables are a good way for Methods in the Class to be able to share information with each other, kind of like global variables.  Instance variables are also used to store state because when you instantiate and object of a Class (using the new keyword), the instance variables will keep whatever information is stored in them until the object variable goes out of scope.  As you practice more with C# you will understand this concept better by experience.

Let’s declare some instance variables in the Class Arithmetic.  At the top of the Class, above the “public Arithmetic()” declare the following 2 variables:

 

int mNumber1;
int mNumber2;

Then add a new Method:

 

public Arithmetic(int iNum1, int iNum2)
{
 mNumber1 = iNum1;
 mNumber2 = iNum2;
}

Make sure to leave the existing Constructor Arithmetic as is, you should now have 2 Methods named Arithmetic.  In the example above, I am taking the parameters iNum1 and iNum2 and storing the values of those variables in the instance variables mNumber1 and mNumber2.  Now add a new AddTwoNumbers Method:

 

public int AddTwoNumbers()
{
 return mNumber1 + mNumber2;
}

You should have 2 Methods named AddTwoNumbers: one that takes arguments and the new one that does not.  In contrast to the old AddTwoNumbers, this new one doesn’t require any arguments, but simply adds the two instance variables together and returns the result.  Now we are going to practice calling the new Constructor and using the instance variables.  Create a new web form called Default2.aspx.  Add a Button and TextBox to the form.  Change the Text for the Button to say “Call Class constructor”.  Double click the Button and put the following:

 

Arithmetic mynewclass = new Arithmetic(100, 200);
int iResult = mynewclass.AddTwoNumbers();
TextBox1.Text = iResult.ToString();

On the first line above, I am calling the Class constructor and passing two numbers: 100 and 200.  Once I do that, the mynewclass object is going to keep the values 100 and 200 in the instance variables.  Then when I call AddTwoNumbers() on the next line, that Method adds the two instance variables and returns the result of that operation.  This is in contrast to the prior lessons where I was calling the other version of the AddTwoNumbers Method that takes 2 arguments.  In this example, the Constructor Method Arithmetic and the AddTwoNumbers Method are sharing the instance variables.

Here are some important things to note about this lesson.  First notice that my Class Arithmetic has 2 Constructors and I can call either one when I instantiate the Class:

 

Arithmetic mynewclass = new Arithmetic();

OR

 

Arithmetic mynewclass = new Arithmetic(100, 200);

Also notice that there are two versions of the AddTwoNumbers Method as well and I can also choose which one to use.  Having more than one version of a Method is called Overloading.  Even though I can have more than one Method with the same name, each has a unique signature or set of parameters.  I CANNOT have two Methods with the same name and the same set of parameters, this is not allowed.  Each Method has to have its own distinct set of parameter types.  Experiment with this on your own and you will see what I mean.

Did you enjoy this article? Join my free Amazing ASP.NET newsletter.
It's safe and easy to signup. Unleash your unlimited web programming potential. You get:
  • Notified anytime a new article comes out
  • Technical step-by-step tutorials on all important ASP.NET topics
  • Expert guidance so you can reach unlimited ASP.NET success
  • Important ASP.NET news updates
Enter your Email

No Comments »

No comments yet.

RSS feed for comments on this post. TrackBack URL

Leave a comment