Thursday, 23 February 2012

Introduction to Classes

Classes Fundamentals   99materials


Introduction:


To use a variable, we can declare it using either var keyword or a known and simple data type. For example, we can use an integer to declare a variable that represented the number of bedrooms of a house. Here is an example:



class Program
{
    static void Main()
    {
        int bedrooms = 3;
    }
}

As opposed to a simple variable, in C#, you can use one or more variables to create a more complete or complex object.
A class is a technique of using one or a group of variables to be used as a foundation for a more detailed variable. When creating a class, you start with the class keyword followed by a name and its body delimited by curly brackets.
Application: Introducing Classes  99materials
  1. Start Microsoft Visual Studio
  2. To create a new application, on the main menu, click File -> New Project
  3. In the middle list, click Empty Project
  4. Change the Name to DepartmentStore1
  5. Click OK   
Naming a Class 99materials
Like a variable, a class must have a name. The name primarily follows the same rules as the variables. You can then add your own rules. In our lessons:
  • If the name of a class is in one word, the first letter will be in uppercase. Examples are Identification, Vehicle, or Student
  • If the name of a class is a combination of words, the first letter of each word will be in uppercase. Examples are DrivingRecord, GeometricShape, or SocialSecurityInformation.

Managing Classes  99materials

Introduction
Microsoft Visual Studio provides many tools and visual objects used to manage classes of an application. 
The studio makes it possible to conveniently rename a class (or one of its members) or to easily navigate to a 
class.

The Class View
Probably the best window to manage classes is called the Class View. To get the Class View:
  • If the Class View is not yet displaying, on the main menu, you can click View -> Class View
  • If the Class View is showing already, click its tab
The Class View is made of six sections. The title bar and the use of the Class View follow the same 
description as for the Solution Explorer:
99materials classview

The second part of the title bar is its toolbar .

The Class View New Folder button allows you to create and add a new folder to the project. The Back and 
the Forward buttons allow you to navigate among the classes.
Under the toolbar, there is another bar made of a combo box and a button. These allow you to search.
The body of the Class View is made of two sections. The first node of the upper section displays the

name of the project. Under the project node, the names of classes display. The bottom section of the

Class View is used to display the members of a class. To see the members of a class, you can click it

in the top window. Here is an example:
99materials-class members

Creating a Class 99materials
There are various ways you can create a class. If you are working from a text editor, start a file, type class,
 followed by a name, and followed by curly brackets. Here is an example of a class named House:
class House
{
}
From your text editor, save the file with the extension .cs. The name of the file doesn't have to be the same 
as the class. You should save the file in the same folder that contains the other files of the same project.
A class is created in a code file. As such, you can include it in the first file of your project. Here is an example:
class House
{
}

class Exercise
{
    static void Main()
    {
        int bedrooms = 3;
    }
}

If you are creating your project using Microsoft Visual C# 2010 Express or Microsoft Visual Studio, it can 
assist you with creating a class. In Lesson 1, we saw how to create a code file. After creating one, you 
can just type the code for the class. When you use the Add New Item option from the main menu or 
from the Solution Explorer, you can select any type of file you want to create, including a class. Otherwise,
 you can explicitly indicate that you want to create a class. This would automatically select the Class option 
in the Add New Item dialog box.
To create a new class:
  • On the main menu, click Project -> Add Class... (or Project -> Add New Item... In the middle list of the
           Add New Item dialog box, click Class)
  • In the Solution Explorer, right-click the name of the project, position the mouse on Add, and click Class
  • In the Class View, right-click the name of the Project, position the mouse on Add, and click Class
Any of these actions would display the Add New Item dialog box with the Class option selected. If you had 
selected the Add New Item from the main menu or the Solution Explorer, you can click Class. In the Name 
text box, accept or change the default name and click Add. A new file named after the class with the 
.cs extension would be added to your project.
You can also use skeleton code to start a class: 
  • In the section of the Code Editor where you want to add the class, right-click and click Insert Snippet...
           Double-click Visual C#. In the list that appears, double-click class  
 
                     99materials

If you had already written code, such as declaring some variables, and you want to include that code in

a new class, select that code. Right-click it and click Surround With...
   

99materials
 
99materials

No comments:

Post a Comment

Introduction to Classes

Thursday, 23 February 2012

Introduction to Classes

Classes Fundamentals   99materials


Introduction:


To use a variable, we can declare it using either var keyword or a known and simple data type. For example, we can use an integer to declare a variable that represented the number of bedrooms of a house. Here is an example:



class Program
{
    static void Main()
    {
        int bedrooms = 3;
    }
}

As opposed to a simple variable, in C#, you can use one or more variables to create a more complete or complex object.
A class is a technique of using one or a group of variables to be used as a foundation for a more detailed variable. When creating a class, you start with the class keyword followed by a name and its body delimited by curly brackets.
Application: Introducing Classes  99materials
  1. Start Microsoft Visual Studio
  2. To create a new application, on the main menu, click File -> New Project
  3. In the middle list, click Empty Project
  4. Change the Name to DepartmentStore1
  5. Click OK   
Naming a Class 99materials
Like a variable, a class must have a name. The name primarily follows the same rules as the variables. You can then add your own rules. In our lessons:
  • If the name of a class is in one word, the first letter will be in uppercase. Examples are Identification, Vehicle, or Student
  • If the name of a class is a combination of words, the first letter of each word will be in uppercase. Examples are DrivingRecord, GeometricShape, or SocialSecurityInformation.

Managing Classes  99materials

Introduction
Microsoft Visual Studio provides many tools and visual objects used to manage classes of an application. 
The studio makes it possible to conveniently rename a class (or one of its members) or to easily navigate to a 
class.

The Class View
Probably the best window to manage classes is called the Class View. To get the Class View:
  • If the Class View is not yet displaying, on the main menu, you can click View -> Class View
  • If the Class View is showing already, click its tab
The Class View is made of six sections. The title bar and the use of the Class View follow the same 
description as for the Solution Explorer:
99materials classview

The second part of the title bar is its toolbar .

The Class View New Folder button allows you to create and add a new folder to the project. The Back and 
the Forward buttons allow you to navigate among the classes.
Under the toolbar, there is another bar made of a combo box and a button. These allow you to search.
The body of the Class View is made of two sections. The first node of the upper section displays the

name of the project. Under the project node, the names of classes display. The bottom section of the

Class View is used to display the members of a class. To see the members of a class, you can click it

in the top window. Here is an example:
99materials-class members

Creating a Class 99materials
There are various ways you can create a class. If you are working from a text editor, start a file, type class,
 followed by a name, and followed by curly brackets. Here is an example of a class named House:
class House
{
}
From your text editor, save the file with the extension .cs. The name of the file doesn't have to be the same 
as the class. You should save the file in the same folder that contains the other files of the same project.
A class is created in a code file. As such, you can include it in the first file of your project. Here is an example:
class House
{
}

class Exercise
{
    static void Main()
    {
        int bedrooms = 3;
    }
}

If you are creating your project using Microsoft Visual C# 2010 Express or Microsoft Visual Studio, it can 
assist you with creating a class. In Lesson 1, we saw how to create a code file. After creating one, you 
can just type the code for the class. When you use the Add New Item option from the main menu or 
from the Solution Explorer, you can select any type of file you want to create, including a class. Otherwise,
 you can explicitly indicate that you want to create a class. This would automatically select the Class option 
in the Add New Item dialog box.
To create a new class:
  • On the main menu, click Project -> Add Class... (or Project -> Add New Item... In the middle list of the
           Add New Item dialog box, click Class)
  • In the Solution Explorer, right-click the name of the project, position the mouse on Add, and click Class
  • In the Class View, right-click the name of the Project, position the mouse on Add, and click Class
Any of these actions would display the Add New Item dialog box with the Class option selected. If you had 
selected the Add New Item from the main menu or the Solution Explorer, you can click Class. In the Name 
text box, accept or change the default name and click Add. A new file named after the class with the 
.cs extension would be added to your project.
You can also use skeleton code to start a class: 
  • In the section of the Code Editor where you want to add the class, right-click and click Insert Snippet...
           Double-click Visual C#. In the list that appears, double-click class  
 
                     99materials

If you had already written code, such as declaring some variables, and you want to include that code in

a new class, select that code. Right-click it and click Surround With...
   

99materials
 
99materials

No comments:

Post a Comment