• (3 Pages)
  • +
  • 1
  • 2
  • 3

C# For Beginners Tutorial I Guide to Classes and their uses..

#1 Fungle  Icon User is online

  • D.I.C Regular
  • member icon

Reputation: 249
  • View blog
  • Posts: 337
  • Joined: 06-March 10

Posted 12 January 2011 - 10:42 AM

*
POPULAR

Welcome to the tutorial for beginners on the importance and purpose of classes in C# and how you would set about using them in programs :)

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 ^^ Right so we've covered properties and how to use the class in your codes so lets cover methods next!

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


Is This A Good Question/Topic? 100
  • +

Replies To: C# For Beginners Tutorial I

#2 Joe Klemmer  Icon User is offline

  • New D.I.C Head

Reputation: 6
  • View blog
  • Posts: 15
  • Joined: 02-April 10

Posted 16 January 2011 - 11:48 AM

Nice job here. I am extremely novice with C# so it came in handy.

Side note: You misspelled "organized".
Was This Post Helpful? 3
  • +
  • -

#3 Shane Hudson  Icon User is offline

  • D.I.C Technophile
  • member icon

Reputation: 342
  • View blog
  • Posts: 1,286
  • Joined: 06-December 09

Posted 16 January 2011 - 12:18 PM

View PostJoe Klemmer, on 16 January 2011 - 06:48 PM, said:

Nice job here. I am extremely novice with C# so it came in handy.

Side note: You misspelled "organized".


ROFL, I think you will find that is the American version... :P and of course the English version has to be right because it is, well, English!

This really is a great tutorial, somehow you managed to make such a boring (though important) subject very interesting and fun.
Was This Post Helpful? 3
  • +
  • -

#4 Fungle  Icon User is online

  • D.I.C Regular
  • member icon

Reputation: 249
  • View blog
  • Posts: 337
  • Joined: 06-March 10

Posted 16 January 2011 - 12:19 PM

unEdited! Thanks for mentioning it ^^ Shane

This post has been edited by Fungle: 16 January 2011 - 12:21 PM

Was This Post Helpful? 2
  • +
  • -

#5 lalaoye  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 15-June 09

Posted 16 January 2011 - 05:27 PM

This is really interesting, i hope i get to learn something here. Keep it up!
Was This Post Helpful? 0
  • +
  • -

#6 Fungle  Icon User is online

  • D.I.C Regular
  • member icon

Reputation: 249
  • View blog
  • Posts: 337
  • Joined: 06-March 10

Posted 16 January 2011 - 05:38 PM

Thank you very much ^^
Was This Post Helpful? 1
  • +
  • -

#7 Toft  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 5
  • Joined: 01-December 10

Posted 31 January 2011 - 11:52 AM

Nice Tutorial :) you are good at explaining things i think.. Good Job :)
Was This Post Helpful? 0
  • +
  • -

#8 FugitiveEye  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 05-February 11

Posted 05 February 2011 - 02:07 PM

I'm just got an interest in programming and this really helped. Thank you!
Was This Post Helpful? 0
  • +
  • -

#9 abhishek4  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 25-February 11

Posted 25 February 2011 - 04:42 AM

Nice work.keep it up. :smile2:
Was This Post Helpful? 1
  • +
  • -

#10 NantucketSleighride  Icon User is offline

  • D.I.C Head

Reputation: 21
  • View blog
  • Posts: 105
  • Joined: 13-February 11

Posted 26 February 2011 - 08:23 AM

is the "get/set" part necessary after declaring something public? Unless you want to hold them to being able to only do one or the other, they can automatically do both just by making it public, no?

I'm wondering if I'm missing something on that part.
Was This Post Helpful? 0
  • +
  • -

#11 Fungle  Icon User is online

  • D.I.C Regular
  • member icon

Reputation: 249
  • View blog
  • Posts: 337
  • Joined: 06-March 10

Posted 29 March 2011 - 02:01 AM

View PostNantucketSleighride, on 26 February 2011 - 04:23 PM, said:

is the "get/set" part necessary after declaring something public? Unless you want to hold them to being able to only do one or the other, they can automatically do both just by making it public, no?

I'm wondering if I'm missing something on that part.


The Get/Set is required when setting a property but only in the context of what you want the programmer to get from it, e.g. You can just set it to get so they cannot modify the value.
Was This Post Helpful? 0
  • +
  • -

#12 jupiejupe  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 16-April 11

Posted 16 April 2011 - 08:22 PM

you really need to have a code complete of all the parts afterwards so noobs like me can see how it all fits together.
Was This Post Helpful? 0
  • +
  • -

#13 yarok  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 12
  • Joined: 11-January 10

Posted 21 April 2011 - 04:30 PM

Looking forward for the next episode! :)

This post has been edited by yarok: 21 April 2011 - 04:30 PM

Was This Post Helpful? 0
  • +
  • -

#14 danx55  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 3
  • Joined: 17-May 11

Posted 17 May 2011 - 07:54 AM

hi,
this is great! thanks :)
more please ;)

im just starting out with c# and finding it REALLY slow going - so stuff like this is really good

i agree with an earlier comment about having the final code list shown - thanks

cheers
dan
Was This Post Helpful? 1
  • +
  • -

#15 Lizzytemmy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 23-May 11

Posted 26 May 2011 - 11:03 AM

This tutorial is indeed interesting, can't wait for the Part II. Please, can you make the next tutorial to be on a web application? and please make it as soon as possible. I'm a novice to C# and I really want to get going, grow in developing web-based application.

Thanks.
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3