First, as always there are some requirements, You will need one or more of the following:
Visual Studio [2008 +] C# Express will work as well as full versions
Knowledge of the Barebones [Creating Variables, Using Methods etc..]
Ok, Lets begin the tutorial with a bit of theory and logic before we start firing up Visual Studio or Text editors carefully organised by some not-so-fancy sub headings
What is a Class?
As a very good teacher of mine refers to it, "A Class is a blueprint of an object" and in this class you define your objects methods and it's properties and what they do. I'll use an example, Say you have a Word Counter, You don't need to put all that code in your form making it look all messy and out of line and not very dynamic! So lets sweep it under the nearest piece of furniture and shove it in a class called WordCount!
So, WordCount class would have the content of what the name suggests, A method that calculates the total words in a string which could be a Rich Text Box. In order to create this we could have a property for the amount of words in the string which we can return in the end to be able to assign it to a label so the user can see how much work they have or haven't done.
We would also need to make a method for it to actually count the words in the string, that would go in the class too.
Now the great thing is that, once you've made your class you can re-use it anywhere in your project that you want to! which I will explain how to do later in this tutorial. The reason that being able to re-use code is so that we don't have to endlessly (and frankly, wastefully) declaring variables over and over again, and reproducing for every form you have or want to use.
Classes provide us with structure and a method of organising our sometimes vast seas of code, they also provide from a design point of view a very logical outlook on what exactly you need to think about when you set about making your program, Because you can create empty classes for all your objects that you want to use and what kind of methods and properties they will have.
Also, there are some neat and magical things that you can do with classes too such as Inheritance which the clue is in the name really, allows you to inherit properties from a previous class and use them in another class so if you wanted to make a base class of Parent, The child might have some of the same bits from the parent but they need to be slightly altered so this way you don't have to re type all the codes from the parent class just to do the child class!
Pfft, Why would I use that?
Because programming is a LOGICAL task and keeping your code organised in a logical way is a big part of this logic, There is no point having a pool of codes hoping that it works and if it doesn't adding more code to fix it because it's about as useful as throwing some cabbage at a duck. Which brings me onto point two, Optimization, You don't need to waste your time endlessly re-writing or re-configuring code you have already written right? Not very logical if you ask me... So you use classes to keep yourself organised and your program organised.
Classes are a fundamental part of most Object Orientated languages and they save the programmer so much time compared to having one big un-organised, grey and damp file that only IBM's RoadRunner can read. Don't dismiss them and don't be scared of them if you are new to programming because they're very nice really under all that code
Ok!, Ok! I give, How do I use it!
They are surprisingly easy to use, If you've just started C# and you make a new class you might feel depressed by the four or five lines of code provided in the template with no indication on what the heck your meant to do in this void of white-code-less-ness.
Lets understand the template that Visual Studio provides
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Form1 { class Class1 { } }
As you can see, this code is very boring and brief which doesn't really make a whole lot of sense as a whole but lets de-compile it and see what we can understand from it
using System; using System.Collections.Generic; using System.Linq; using System.Text;
These of course are the libarys, All come from the Microsoft .NET Framework which is very handy, for instance when you make a button, your actually making a new instance of the Button class found in the .NET Framework which some poor sod has already made for you to use however you want to
namespace Form1
This is the namespace, This is a vital part of the code because it is telling us what it's related to in a sense, If I had a project called Form1 all of the classes I used would have their namespace as Form1, This is needed in the Non-GUI version specifically.
class Class1 { }
This is where all of our code is going to go, in the middle of our little curvey friends, The class Class1 is where we are defining the name of our class, We could call it anything we wanted to, So for now just to lemon the mundanity of your day, Lets call it uh, Turnip..
class Turnip { }
Now that our mundanities are sufficiently lemoned, lets work out what makes a turnip.
Ok, Turnips are... sort of purple and white but for arguments sake lets say their purple..but then again they could be other colors, Lets let the programmer decide! So we need to make a property for our Turnip for "Color" (American spellings in programming remeber!)
NOTE! You need to add this line to your libarys to use the color class:
using System.Drawing;
Heres how we will do it:
public Color T_Color { get; set; }
Lets go through it line by line...or rather word by word
public means that it is accessible for use OUTSIDE of the class as well as inside, Since we want it to be accessible lets leave it as public, The first Color is the .NET Framework class for Color, and T_Color is the name of our property and the
{get; set;}
means that we get to chose if the programmer can just get the color of the turnip or set it too, Since turnips are of questionable colors we want them to set it too to avoid turnip-related frustration.
And there you go! You made a property! but how do we use it? Easy! In your form or console code you need to make a new instance of the class, all this means is that we introduce a new variable as it were for us to play with, without messing things up
Turnip Bob = new Turnip();
There we go, We now have a new friend to play with called bob, Bob has all the properties of a Turnip and all the methods of a Turnip that's why we declare bob as a Turnip.
So, Now we have Bob we can use him just as you would use any other control, e.g. textbox1.Text you can use bob in the same way like so
Bob.T_Color = Color.Purple;
Now we've set our custom property to purple on Bob just as if you were changing a labels text property
Methods in classes are somewhat easier than properties, A Method of a turnip could be Grow();
So lets make a method that makes the turnip set about growing some more..
public int Size { get; set; } public int Grow() { Size++; return Size; }
What we've done here is make our turnip fatter when the method of Grow() is called, I added a new property called Size so we can keep track of how big it is.
Size ++ means that we add 1 to the size and return Size means we're returning the value of Size to the programmer or user, It's easy!
So then, If we wanted to call that method on bob we would just use this code:
Bob.Grow();
So, Now you know what classes are for, What they do, How to use them in a program, How to make a property and How to make a method.
That's all the classes information for now, Part II will come soon if anyone reads this tutorial
If you liked this tutorial then don't forget to press the green plus!
This post has been edited by Fungle: 16 January 2011 - 12:21 PM