Wednesday 12 September 2012

Inheritance in C#

There are two kinds of inheritance in  OOP (object-oriented programming), the simple inheritance (inherit from one class) and multiple inheritance (inherit from multiple classes). C# only have simple inheritance.
Example about inheritance in C#: assume that we have class Animal contains information about Name and number of its foot. Now we create some class to be inheritance from class Animal

    using System;
    using System.Collections.Generic;
    using System.Text;

    namespace __OOP_Inheritance
    {
        class Program
        {
            static void Main(string[] args)
            {
                Dog objDog = new Dog(4);
                objDog.displayProperties();
                Chicken objChicken = new Chicken(2);
                objChicken.displayProperties();
                Console.Read();
            }
        }

        class Animal
        {
            protected int ifoots;
            protected string sName;

            protected void setFoot(int ival)
            {
                ifoots = ival;
            }

            protected void setName(string sVal)
            {
                sName = sVal;
            }

            public void displayProperties()
            {
                Console.WriteLine(sName + " have " + ifoots.ToString()+ " foots");
            }
        }

        class Dog : Animal
        {
            public Dog(int ival)
            {
                setName("Dog");
                ifoots = ival;
            }
        }

        class Chicken : Animal
        {
            public Chicken(int ival)
            {
                setName("Chicken");
                setFoot(ival);
            }
        }
    }


If you want to override function public void displayProperties(), You should set it virtual. Like This:

    using System;
    using System.Collections.Generic;
    using System.Text;

    namespace __OOP_Inheritance
    {
        class Program
        {
            static void Main(string[] args)
            {
                Dog objDog = new Dog(4);
                objDog.displayProperties();
                Chicken objChicken = new Chicken(2);
                objChicken.displayProperties();
                Tiger objTiger = new Tiger(4);
                objTiger.displayProperties();
                Console.Read();
            }
        }

        class Animal
        {
            protected int ifoots;
            protected string sName;

            protected void setFoot(int ival)
            {
                ifoots = ival;
            }

            protected void setName(string sVal)
            {
                sName = sVal;
            }

            public virtual void displayProperties() // Changed here
            {
                Console.WriteLine(sName + " has " + ifoots.ToString()+ " foots");
            }
        }

        class Dog : Animal
        {
            public Dog(int ival)
            {
                setName("Dog");
                ifoots = ival;
            }
        }

        class Chicken : Animal
        {
            public Chicken(int ival)
            {
                setName("Chicken");
                setFoot(ival);
            }

            public void displayProperties()
            {
                base.displayProperties();
                Console.WriteLine(sName + " have " + ifoots.ToString() + " foots (from Chicken class)");
            }
        }

        class Tiger : Animal
        {
            public Tiger(int ival)
            {
                setFoot(ival);
            }

            public override void displayProperties() // override here
            {
                Console.WriteLine("Tiger has " + ifoots.ToString()+ " foots");
            }
        }
    }
    


Tuesday 11 September 2012

Format Code to Post on Blog

This is my first post in this blog. I take many time to find "how to create a coding region in my post?", because I will share all experiments about any field which I research.

This is a post very interest about comparison of some ways to Post Code to your Blog. They have show all pros and cons: "How to Post Code To Your Blog and other Religious Arguments"
In my blog, I use a very simple way to create Format Code. It doesn't quite format things as nicely but it does allow you to copy and paste and retains all the line breaks and formatting. It is also an online editor that you can use for free.

To use this, we will need do this step:


1. Include CSS for format code into template of Blog. I will show you How to do with Blogger (Blogspot)
- Log in Blogspot.
- Go to Template and choice Customise.
- Copy content of this CSS file (download here) to text box (like picture) and press Apply to Blog.

2. Go to   http://dotnetslackers.com/articles/csharpformat.aspx   and generate your code with format.
3. Copy generate code to HTML of post and see result

        private void btnUpload_Click(object sender, EventArgs e)
        {
            Auth myAuth = flic.AuthGetToken(tempFrob);
            flic.AuthToken = myAuth.Token;

            string file = "Lena.bmp";
            string title = "Lena Photo";
            string description = "This is demo uploading with Lena Photo";
            string tags = "Lena";
            string photoID = flic.UploadPicture(file, title, description, tags);
            label1.Text = photoID;
            Photoset pset = new Photoset();
            pset.Title = "My new Photoset";
            flic.PhotosetsCreate("My new photoset", "Test outsite photo", photoID);
        }