Class and Object Tutorial

Geekitygeek 2 Tallied Votes 491 Views Share

In the simplest of terms, a Class is like a cookie cutter. It describes what members and methods an object should have. You use the cookie cutter (class) to make (instantiate) one or more cookies that are all the same shape. The cookies are called objects.
The same way that you can decorate each cookie a different way, you can set the member values of each object to different values.
For instance, if you had a Class like this:

public class Cookie
{
   public string Topping;
}

You can create two cookies:

Cookie firstCookie = new Cookie();
Cookie secondCookie = new Cookie();

At this point each cookie is the same because they are fresh from the cutter. Now we can decorate them:

firstCookie.Topping = "Chocolate";
secondCookie.Topping = "Icing";

At this point we have two cookies that are different. Each has the same members but the values stored in them are different.

Now we need to be able to eat the cookies, so lets add a method. A method outlines a Classes behaviour.

public class Cookie
{
   public string Topping;

   public void TakeBite()
   {
       //crunch..nom nom nom
   }
}

SO, now we have two objects of the cookie class (firstCookie and secondCookie). To enjoy a chocolate flavour mouthful we need to bite into the right cookie. In our case, the chocolate topping went on firstCookie so thats the one we need to bite:

firstCookie.TakeBite();

A static class behaves a little differently. A static class is used to describe something you will only ever have one of. Lets expand our example. Before we can eat our cookies we need to cook them. Unless you have an industrial kitchen you will only have one oven. You don't want to have to re-build it every time you use it so you build it once and have it ready to use whenever you need it. Thats what static classes are:

public static class Oven
    {
        public static int Temperature;

        //the method is static so we don't need to instantiate an Oven object to use it. 
        //It accepts a Cookie as an input parameter (rawCookie)
        //It has a return type of Cookie which means it will return a Cookie when it finishes (return finishedCookie).
        public static Cookie CookCookie(Cookie rawCookie)
        {
            Cookie finishedCookie = rawCookie; //plus temperature and time to cook
            return finishedCookie;
        }
    }

We have made our oven and its ready to use. The static methods can be called any time we need them. So to cook our cookies we put them in the oven:

//the CookCookie has a return value of the type Cookie.
//So we send the cookie into the Cook method and get a cooked cookie back.
firstCookie = Oven.CookCookie(firstCookie);
secondCookie = Oven.CookCookie(secondCookie);

We didn't need to create an oven Oven myOven = new Oven(); because the method we need is static.

Hope that helps to clear up some of the confusion :)

ddanbe commented: Nice! +5
sknake commented: very nice +6
using System;
using System.Collections.Generic;
using System.Text;

namespace ObjectExample
{
    class Program
    {
        static void Main(string[] args)
        {
            //create two instances of the Cookie Class
            Cookie firstCookie = new Cookie();
            Cookie secondCookie = new Cookie();

            //pass our cookies to our method
            Oven.CookCookie(firstCookie);
            Oven.CookCookie(secondCookie);
            
            //Now lets decorate one or our cookies
            secondCookie.Topping = "Chocolate";

            Console.WriteLine("This cookie is {0}", firstCookie.Topping);
            Console.WriteLine("This cookie is {0}", secondCookie.Topping);
            Console.WriteLine();

            //now to sample the cookies
            firstCookie.TakeBite();
            secondCookie.TakeBite();
            Console.ReadKey();

        }
    }

    public class Cookie
    {
        public string Topping;

        public void TakeBite()
        {
            Console.WriteLine("crunch...nom nom nom");
        }

    }

    public static class Oven
    {
        public static int Temperature;

        //Method is static so no instance of Oven Class is needed to call it
        //classes are reference type so rawCookie will contain a pointer to our class in memory
        public static void CookCookie(Cookie rawCookie)
        {
            //once they are cooked we have plain cookies
            rawCookie.Topping = "Plain";
        }
    }
}
ddanbe 2,724 Professional Procrastinator Featured Poster

Makes one get hungry and wanting for more!!!:)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.