C# tutorial : Classes and Objects / PDF
C# tutorial : Classes and Objects / PDF
The idea is that you can reuse this code (Class) whenever you need it, or when you need it in other projects. It saves you from having to write the same thing over and over again.
Think of a Class as a recipe. If you had a recipe for a delicious Banana Cake, the recipe will tell you what you need to do to make the cake. But it's not the cake itself. It's the instructions for the cake. If you have the recipe, you can make Banana Cakes whenever you need them.
An Object is the
thing that the recipe makes - The Banana Cake itself. You do all the coding in
a Class (recipe), and then instruct C# to make an object (a Banana Cake). The
two processes are different.
But all you are really doing with Classes and Objects is trying to separate code into chunks, so that it can be reused. When the code is being used, it's an object. But the object is created from your code (Class).
Let's get some practice.
The first Class
that you'll create is a very simple one, and won't be that much use in the real
world. But it will show you how Classes are structured, and how Objects are
created from Classes. All our Class will do is display a "Happy Birthday"
Message.
So start a new project, and add a button to your form. We'll use the button later. To add a new class to your programme, click the Project menu at the top of C#. From the Project menu, select Add Class:
You should then
see the Add New Item dialogue box popping up. Select the Class item from the
available templates. In the Name box at the bottom, type this:
Then click the Add
button on the dialogue box. You should see all the default code for your class
appear:
WindowsApplication1 was the default name when
we created a new project. If we had changed the default project name to, say,
Birthday then it would have said namespace Birthday. (A namespace is
where classes are grouped together. System is a namespace. As too are
Collections, Generic, and Text.)
Notice that the Solution Explorer on the right is showing your Class:
The class called HappyBirthday.cs
is now an item under Form1.cs. The .cs extension means that the file is
a Class.
The Class code stub that C# created for us, though, was this:
class HappyBirthday
{
}
So you start with the word "class" followed by a space. The name of your class comes next, followed by a pair of curly brackets. All the code for your class goes between the two curly brackets.
Let's add a simple message to the class.
What we'll do is to create a Method in the class that displays a message. So add this to your code:
The name of our Method is getMessage, but
we could have called it almost anything we liked. The Method is going to return
a string. Notice that we've made the Method public. A public
Method (or variable) set up inside of a class means that it can be seen from
the outside. So a button on a form can see it, for example. If you make it private
then it can only be seen from inside of the class.
C#
.NET is an Object Oriented programming language. Objects are created from
Classes, which you've already used a lot. The Windows Form itself is a Class,
and when you run your programme, you are creating Objects: a Form object, a
button object, a Textbox object, etc. In this section, you'll learn how to
write your own Classes, and turn them in to Objects. First, and explanation
What is a Class?
A Class is simply a chuck of code that does a particular job. You might have a class that handles all your database work, for example, or one that does error checking on Textboxes.The idea is that you can reuse this code (Class) whenever you need it, or when you need it in other projects. It saves you from having to write the same thing over and over again.
Think of a Class as a recipe. If you had a recipe for a delicious Banana Cake, the recipe will tell you what you need to do to make the cake. But it's not the cake itself. It's the instructions for the cake. If you have the recipe, you can make Banana Cakes whenever you need them.
What is an Object?
An Object is the
thing that the recipe makes - The Banana Cake itself. You do all the coding in
a Class (recipe), and then instruct C# to make an object (a Banana Cake). The
two processes are different. But all you are really doing with Classes and Objects is trying to separate code into chunks, so that it can be reused. When the code is being used, it's an object. But the object is created from your code (Class).
Let's get some practice.
So start a new project, and add a button to your form. We'll use the button later. To add a new class to your programme, click the Project menu at the top of C#. From the Project menu, select Add Class:
HappyBirthday.cs
Notice that the Solution Explorer on the right is showing your Class:
The Class code stub that C# created for us, though, was this:
class HappyBirthday
{
}
So you start with the word "class" followed by a space. The name of your class comes next, followed by a pair of curly brackets. All the code for your class goes between the two curly brackets.
Let's add a simple message to the class.
What we'll do is to create a Method in the class that displays a message. So add this to your code:
Create Objects from your C# Classes
Now that we
have added a Method to our Class, let's turn it into an Object.
Go back to your
form, and double click the button you added. This will bring up the code stub
for the button.
To create an
object from a class, you have to set up a variable of that class type. This
involves nothing more than typing its name. So type the "Hap" of
HappyBirthday (the name we gave our class). You should see the IntelliSense
list appear with you Class on it:.......
C# tutorial : Classes and Objects / PDF
0 commentaires: