Classes and Objects
The concept of class and object is the most important thing in object-oriented programming. Objects are derived from classes. Objects created from the same class have common properties.
For example, when we add a button on our form from the toolbox, we create an object of the Button class. This object is given an automatic name. We can create many objects of the same class with different names. They all have properties of the Button class.
We work with many ready-made classes when developing Windows Forms Applications. Classes contain structures such as methods, properties, and fields. For example, a text box has many properties such as Text, Enabled, BackColor. These properties are defined inside that class.
Object Oriented Programming languages allow programmers to write their own classes. This method provides many advantages. If we leave aside the advantages it provides in terms of programming technique, it will make our work much easier in the following sense. We do not have to rewrite the methods or information that we will use throughout our program every time. By writing these codes in a class, we can access them from anywhere we want. And when we need to make a change in the codes, we don't have to make changes everywhere, we just need to change the codes inside the class. Even this is an important convenience for us.
Class Identification
The class statement is used to define a class . Then the class name is specified. The naming rules in variables and methods apply when naming a class.
class NewClass
{
// Methods, properties and fields will be created here.
}
You can review previous lessons on what methods are and how they are used.
In Windows Forms Applications we can follow these steps to create a valid class in the whole project:
-
Right-click on the project name in the Solution Explorer panel.
-
Click Class from the Insert menu. Type the class name and say OK.
-
We will see that a file with the cs extension has been added to our project with the name we specified.
In the example below, a class named Numbers is created. Let's examine and explain this example.
class Numbers
{
int number1, number2;
public Numbers(int a, int b)
{
number1 = a;
number2 = b;
}
public int SumBetween()
{
int total = 0;
for (int i = number1; i <= number2; i++)
{
total += i;
}
return total;
}
public string WriteBetween()
{
string text="";
for (int i = number1; i <=number2; i++)
{
text += i + " ";
}
return text;
}
}
Let's examine the example piece by piece.
int number1, number2;
First, two variables were created in the Numbers class. We can use these variables anywhere in the class.
public Numbers(int a, int b)
{
number1 = a;
number2 = b;
}
Classes have a constructor method with the same name. This constructor method may or may not receive external information. Constructor methods contain the operations that must be done when creating an object from that class. If there is no need for an operation while creating the object, the constructor method can be left blank or not written.
In the above example, the Numbers constructor method takes two parameters of type int from the outside. It is used to transfer the numbers sent to it to the variables number1 and number2. These number1 and number2 variables are designed to be used in other methods in the class.
public int SumBetween()
{
int total = 0;
for (int i = number1; i <= number2; i++)
{
total += i;
}
return total;
}
Two different methods are defined within the Numbers class. Of these, the SumBetween method adds all the numbers between the variables number1 and number2 and returns the result.
We can use this method as Numbers.SumBetween() anywhere in the program. (The usage example is at the bottom of the page..)
Similarly, the WriteBetween method returns the result by concatenating all numbers between number1 and number2 with a space between them.
The public statement that appears in the example is the access specifier of the method. We mentioned access specifiers in our previous Method Definition and Use page.
Object Creation
In order to access the properties and methods in a class from within the program, we must first create an object from that class.
Numbers object1 = new Numbers( 10, 20 );
An object named Object1 is defined above from the Numbers class. On the right of the equation, the new command is called, followed by the constructor method of the class. While defining the constructor method, we defined it to take two parameters from the outside. That's why we gave two parameters when calling.
int Sum = object1.SumBetween();
Using the object we created, we will be able to access the methods in that class. We see this above. Below is another example of using the Numbers class and methods.
private void button1_Click(object sender, EventArgs e)
{
int a = int.Parse(textBox1.Text);
int b = int.Parse(textBox2.Text);
Numbers object = new Numbers(a, b);
label1.Text = object.SumBetween().ToString();
label2.Text = object.WriteBetween();
}
On this page we have explained the concept of class and object. We defined the class and created its methods. Then we saw how to use that object by deriving an object from this class.
You can find the feature and space usage in the classroom on our next page.
c# class definition, c# class usage, object oriented programming class definition and usage, c# object definition and usage, classes and objects
EXERCISES
There are no examples related to this subject.
Read 1097 times.