• Have Any Question?
  • contact@eitworld.com

csharp.net classes





  • A class is a user-defined data type and it is the base of the object oriented programming language.
  • A class is simply a abstract model in order to define new data type.
  • A class encapsulates the variable and the method inside it.

Defining a Class

    • A class is defined using the ‘class’ keyword.
    • Basic syntax of the class is given below:
class class-name
{
Variable-declaration;
Method declaration;
}

In the above syntax access specifier decides the access level of the class in the whole program. The default access specifer for the class is ‘internal’. You can study the access specifier in details in the further section.

Adding variable to Class

    • Data is encapsulated in a class by putting data fields inside the body of a class.
    • These variables are referred as instance variables.
    • Here is basic syntax to declare variables in the class:
class class-name
{
data-type variable1;
data-type variable2;
…………………………………………………………….
…………………………………………………………….
data –type variablen;
}

access specifier of the variable decides its access level and by-default the access specifier for the variable is ‘private’. data-type defines the type of the variable.

Adding Methods to a Class

    • Methods are used to perform operations on the variables. So variables are useless without methods.
    • The syntax for declaring a method is as follows:
class class-name
{
//variable declaration
data-type variable1;
data-type variable2;
…………………………………………………………….
…………………………………………………………….
data –type variablen;
//method declaration
return-type method-name(parameter-list)
{
Method body
}
…….
……..
}

Access specifier of method defines the access rule for the method. By-default access specifier for method is ‘private’. return –type defines the type of value return by the method .

Creating Object of Class

    • Object is instance of class which can access all the members of class.
    • A class is an abstract concept means it is not exist in really but object is a realistic concept which contains all properties of the class. For example if vegetable is a class then potato, leady-finger, bringle etc are its object.
    • The actual instance of class is created by making its objects.
    • All objects of the class have their own copies of variables and method defined in the class.
    • The basic syntax to declare an object of class is as follows:
class-name object-name=new class-name();

    • After creating an object of class we can access the members of class through the object.
    • The basic syntax for accessing the member of class through object is as follows:
object-name.variable-name;
object-name.method-name();

Here is an example which is sufficient to understand the concept of class:

The example given below shows the complete definition of a class:

Example
    • Here we will add a class in the windows application. To add the class in your project you have to right click on project name under the Solution Explorer as follows: csharp.net-classes
    • In this you have to select the Add and now you will see other options.
    • In these options you have to select the New Item.. and click on it.
    • Now one window will be open as follows:

csharp.net-classes

    • For here you have to select the class and give a name to class or click on Add button.
    • From here you can add interface, window form etc.
    • When you click on Add button then the following class code is shown to you:

csharp.net-classes

    • Here we given the name of class is A.
    • Now you can add the variables inside the class.
    • Here we have created a design as follows:

csharp.net-classes

    • Here we have used one label and two buttons. Now click on button “Class Variable” and add the following code:
private void button1_Click(object sender, EventArgs e)
{
A a = new A();
label1.Text = a.message;

}

    • In this we have created the object of class A and here we are trying to access the value of variable defined in the class and assigning to the label.
    • Now click on button “Class Method” and add the following code:
private void button2_Click(object sender, EventArgs e)
{
A a = new A();
label1.Text = a.display();

}

    • In this we have called a method display() which return string as a message.
    • The class code is as follows:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WindowsFormsApplication2
{
public class A : Form1
{
public String message = “Class Message : eitworld.com”;

public String display()
{
message = “eitworld.com”;
return message;
}
}
}

    • Now press F5 to run the application.
    • When application launch then the first screen appears as follows:

csharp.net-classes

    • Now click on “Class Variable” button, now the message will be changed as follows:

csharp.net-classes

    • Now click on button “Class Method” now the message will be changed as follows:

csharp.net-classes



Real Time Web Analytics