making a virtual object in c#

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • myburt
    New Member
    • Jun 2007
    • 4

    #1

    making a virtual object in c#

    I'm working on very simple lessons to learn c #.
    Can anyone give an example of how to make a virtual object such as these


    Thank You.

    Code:
    namespace critter_menu
    {
        class Program
        {
            static void Main(string[] args)
            {
                bool keepgoing = true;
                int choice;
                critter mycritter=new critter();
                mycritter.name = "George";
    
                while (keepgoing)
                {
                    mycritter.age();
                    choice = showmenu();
                    switch (choice)
                    {
                        case 0:
                            keepgoing = false;
                            break;
                        case 1:
                            mycritter.talk();
                            break;
                        case 2:
                            mycritter.eat();
                            Console.WriteLine("You have fed the critter");
                            break;
                        case 3:
                            mycritter.play();
                            Console.WriteLine("You have played with the critter");
                            break;
                        case 4:
                            Console.WriteLine("Current name: {0}",mycritter.name);
                            Console.WriteLine("Change name to: ");
                            mycritter.name = Console.ReadLine();
                            break;
                        default:
                            Console.WriteLine("That was not a valid input");
                            break;
                    }
                }
       
            }
            static int showmenu()
            {
                int input = 1;
                Console.WriteLine();
                Console.WriteLine();
                Console.WriteLine();
                Console.WriteLine("0) Exit");
                Console.WriteLine("1) Listen to Critter");
                Console.WriteLine("2) Feed Critter");
                Console.WriteLine("3) Play with Critter");
                Console.WriteLine("4) Rename Critter");
                try
                {
                    input = Convert.ToInt32(Console.ReadLine());
                }
                catch (FormatException)
                {
                    Console.WriteLine("Incorrect Input");
                    input = 1;
                }
                return input;
            }
        }
        class critter
        {
            int phappy = 10;
            int pfull = 10;
            int page = 0;
            string pName;
            public string name
            {
                get
                {
                    return pName;
                }
                set
                {
                    if (value.Length > 8)
                    {
                        value = value.Substring(0, 8);
                        pName = value;
                        Console.WriteLine("The name can't be longer than 8 chacters");
                        Console.WriteLine("The critter name has been set to:{0}", pName);
                    }
                    else
                        pName = value;
                }
            }
            public void talk()
            {
                Console.WriteLine("The critter says \"My name is {0}\"", pName);
                {
                    if (pfull < 1)
                        Console.WriteLine("Feed me or i'll die");
                    else if (pfull < 4)
                        Console.WriteLine("I'm kinda getting hungry");
                    else
                        Console.WriteLine("I'm not hungry right now");
                }
                if (phappy < 5)
                    Console.WriteLine("Play with me.");
                
            }
            public void age()
            {
                page++;
                pfull--;
                phappy--;
                //if hungry gets un happy faster
                if (pfull < 3)
                    phappy--;
            }
            public void eat()
            {
                pfull += 4;
            }
            public void play()
            {
                phappy += 3;
            }
        }
    }
  • RoninZA
    New Member
    • Jul 2007
    • 78

    #2
    myburt - the problem with these 'Learn C# in 21 Days' kinda tutorials actually irritate me slightly. They maybe teach you the language, but you are never taught WHY you would want to use certain functions, so I'll explain why you would want to use a virtual operator.

    With inheritance, the virtual operator allows you to define a method for which code can be defined in the base class, but this method can then also be overridden in the inheriting class, as follows:
    Code:
    class Person
    {
        public string Name = string.Empty;
        public bool IsFemale = false;
    
        public virtual string PersonGender()
        {
            if (IsFemale)
                return "This person is female.";
            else
                return "This person is male.";
        }
    }
    
    class Female : Person
    {
        public Female()
        {
             this.IsFemale = true;
        }
    
        public override string PersonGender()
        {
            return "This person is female.";
        }
    }
    
    class Male : Person
    {
        public Female()
        {
             this.IsFemale = false;
        }
    }
    If you understand what I'm trying to illustrate above is that, a virtual method CAN be overridden, but doesn't HAVE to be overridden. If you compile the code above, you'll see all three classes have the PersonGender() method, but depending on which class is called, methods are called from different locations.

    virtual is different to abstract in that an abstract method can not contain code in the base class, and MUST be overridden in the inheriting classes. Also, abstract methods must be contained in an abstract class. But that's a lesson for another day... I hope that this has shed some light on the matter for you :)

    Comment

    Working...