Saturday, March 12, 2011

Classes

Classes are used to split your application into logical parts, so your code is easier to oversee. You can for example split a warship into several parts: weapons, ship, people and etc. In code you will have three classes called: "Weapon", "Ship" and "People". The class ship will then hold an array of weapons and people. You should first write down all the names of the classes with its methods that are logical for your application and then start coding. I bet you won't in the beginning, because coding is way too fun. I thought so too in the beginning, but now I like to predict how my application will look like in the end more.

Enough small talk. Start a new console application called "Classes".


Create a new class by clicking on Project -> Add Class.


Select the template "Class", fill in the name "View" and press Add.


A new file has been created in your project and it's called "View.cs" which can be seen in your solution explorer. The file has been opened and you can see the contents in your text editor.


Add the following code to your "View" class:
public View()
{
Console.Write("I'm alive!");
Console.ReadLine();
}
"public View()" is not a method, it's a constructor. It must have the same name as the class and it mustn't have a return data type. The constructor is called when the class has been instantiated .


Switch back to the "Program.cs" file and create an instance of the class by typing:
View view = new View();
Now the class has been instantiated and the constructor will be called when running your application. Note that the instance "view" of the class "View" could've easily been called anything. The instance "view" doesn't need to be the same name as the class in lowercase. Press F5 on your keyboard to run your application now and "I'm alive!" should be shown on your screen.


Go to your class "View" and add the following method:
public void ShowOutput(string text)
{
Console.Write(text);
Console.Read();
}
The access modifier needs to be "public", because you're going to call the "ShowOutput" method from the class where you instantiated the "View" class.


Back to your "Program" class again. To call the "ShowOutput" method in the "View" class you will have to type the following below "View view = new View();":
view.ShowOutput("Hello World!");
Press F5 on your keyboard and you should see "Hello World!" on your screen after pressing enter once, because your constructor has a "Console.ReadLine();" and your application executes that first.

Use classes to give your application a logical structure. Try to divide classes in how you would do in real life. If you create a game tic tac toe and you want to check if someone made a valid move, then for example create a class called referee.

Source Code Classes Project

>> Properties

2 comments:

  1. Are you not going to continue this series?

    ReplyDelete
  2. I'm sorry, but I'm kind of busy lately. I've got a new job and I've yet to settle in. I'm thinking of doing "intermediate tutorials" soon. I'll continue the series on next monday, because you asked. Are there any specific subjects you want a tutorial for?

    ReplyDelete