CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    May 2009
    Location
    Netherlands
    Posts
    103

    Timer and object?

    hi i tried searching some timer tutorials but i cannot find what i am looking for.
    coming from a C++ background, this assignment is my homework.
    i am perfectly fluent in C++, but i dont know much about C#.
    How do i go about passing the Pet object object into the static timer function?
    Does anyone have tips on my code? please hlep me on this one.

    here is my code:
    Code:
    using System;
    using System.Text;
    using System.Timers;
    
    namespace Tamagotchi
    {
        class Pet
        {
            public int Age;
            public int Mood;
            public int Weight;
            public int Energy;
            public bool Crapped;
            public bool Healthy;
            public int Hunger;
            public bool Alive;
            public Timer Event = new Timer();
    
            public string Name;
    
            protected virtual void OnAge() { }
            protected virtual void OnCrapped() { }
            protected virtual void OnHealth() { }
            protected virtual void OnMood(int x) { }
            protected virtual void OnHunger(int x) { }
            protected virtual void OnWeight(int x) { }
            protected virtual void OnEnergy(int x) { }
            protected virtual void OnAlive() { }
    
            private static void Event_Tick(object sender, ElapsedEventArgs args)
            {
                Random s = new Random();
                if (Crapped)
                {
                    int i = s.Next(1000, 2000);
                    if (i > 1400 && i < 1600)
                    {
                        Healthy = false;
                        OnHealth();
                    }
                }
                switch(s.Next(0, 3))
                {
                    case 0:
                        int x = Hunger++;
                        OnHunger(x);
                        int i = s.Next(1000, 2000);
                        if (i > 1400 && i < 1600)
                        {
                            Crapped = true;
                            OnCrapped();
                        }
                        break;
                    case 1:
                        int x = Mood--;
                        OnMood(x);
                        break;
                    case 2:
                        int x = Weight--;
                        OnWeight(x);
                        break;
                    case 3:
                        int x = Energy--;
                        OnEnergy(x);
                        break;
                }
                Event.Interval = s.Next(1000, 2000);
            }
    
            public void Cure()
            {
                Healthy = true;
            }
    
            public Pet()
            {
                Event.Elapsed += new ElapsedEventHandler(Event_Tick);
                Event.Interval = 1000;
            }
            
            public void Start()
            {
                Healthy = true;
                Crapped = false;
                Energy = 0;
                Age = 0;
                Mood = 0;
                Hunger = 0;
                Weight = 0;
                Alive = true;
                Event.Start();
                OnAlive();
            }
    
            public void Clean()
            {
                Crapped = false;
            }
    
            public void End()
            {
                Event.Stop();
                Alive = false;
                OnAlive();
            }
    
            public void Feed()
            {
                Hunger++;
                Weight++;
            }
    
            public void Play()
            {
                Mood++;
                Weight--;
            }
        }
    
        class Program
        {
            class MyPet: Pet
            {
                protected override void OnAge()
                {
                    Console.WriteLine(Name + " older.");
                }
    
                protected override void OnMood(int x)
                {
                    if(Mood > 5)
                        Console.WriteLine(Name + " happy.");
                    else if(Mood < -5)
                        Console.WriteLine(Name + " unhappy.");
                }
    
                protected override void OnWeight(int x)
                {
                    if (Weight < 5)
                        Console.WriteLine(Name + " skinny.");
                    else if (Weight > 10)
                        Console.WriteLine(Name + " fat.");
                }
    
                protected override void OnHunger(int x)
                {
                    if(Hunger > 0)
                        Console.WriteLine(Name + " starving.");
                }
    
                protected override void OnCrapped()
                {
                    if (Crapped)
                        Console.WriteLine(Name + " poo.");
                    else
                        Console.WriteLine(Name + " flush.");
                }
    
                protected override void OnAlive()
                {
                    if (Alive)
                        Console.WriteLine(Name + " born.");
                    else
                        Console.WriteLine(Name + " die.");
                }
    
                public void Clean()
                {
                    base.Clean();
                    Console.WriteLine(Name + " cleaned.");
                }
    
                public void Feed()
                {
                    base.Feed();
                    Console.WriteLine(Name + " fed.");
                }
    
                public void Play()
                {
                    base.Play();
                    Console.WriteLine(Name + " play.");
                }
    
                public void Cure()
                {
                    base.Cure();
                    Console.WriteLine(Name + " cure.");
                }
    
                public void PrintStatus()
                {
                    Console.WriteLine("Name: " + Name + ".");
                    Console.WriteLine("Age: " + Age + ".");
                    Console.WriteLine("Mood: " + Mood + ".");
                    Console.WriteLine("Hunger: " + Hunger + ".");
                    Console.WriteLine("Weight: " + Weight + ".");
                    Console.WriteLine("Energy: " + Energy + ".");
                    Console.WriteLine("Crapped: " + Crapped + ".");
                    Console.WriteLine("Healthy: " + Healthy + ".");
                }
            };
    
            static MyPet pet = new MyPet();
    
            static void PrintMenu()
            {
                Console.WriteLine("0. Quit");
                Console.WriteLine("1. Pet Status");
                Console.WriteLine("2. Feed Pet");
                Console.WriteLine("3. Play Pet");
                Console.WriteLine("4. Clean Pet");
                Console.WriteLine("5. Cure Pet");
                Console.WriteLine("X. Save Pet");
            }
    
            static void Main(string[] args)
            {
                Console.WriteLine("1. New Pet");
                Console.WriteLine("X. Load Pet");
                switch (Console.ReadKey(true).KeyChar)
                {
                    case '1':
                        Console.Write("Enter a name for your pet: ");
                        pet.Name = Console.ReadLine();
                        pet.Start();
                        break;
                }
                PrintMenu();
                while (true)
                {
                    switch (Console.ReadKey(true).KeyChar)
                    {
                        case '0':
                            return;
                        case '1':
                            pet.PrintStatus();
                            break;
                        case '2':
                            pet.Feed();
                            break;
                        case '3':
                            pet.Play();
                            break;
                        case '4':
                            pet.Clean();
                            break;
                        case '5':
                            pet.Cure();
                            break;
                        default:
                            Console.WriteLine("Invalid command.");
                            PrintMenu();
                            break;
                    }
                }
            }
        }
    }

  2. #2
    Join Date
    Jan 2002
    Location
    Scaro, UK
    Posts
    5,940

    Re: Timer and object?

    Here's a hint, since this is homework. The timer event method doesn't need to be static : it can be an instance member of a class.

    Darwen.
    www.pinvoker.com - PInvoker - the .NET PInvoke Interface Exporter for C++ Dlls.

  3. #3
    Join Date
    May 2007
    Posts
    1,546

    Re: Timer and object?

    Code:
    Console.WriteLine("Name: " + Name + ".");
    Can also be written as:

    Code:
    Console.WriteLine("Name: {0}.", Name);
    Similarly for the rest. It's slightly more readable like that, especially when the number of parameters goes up:

    Code:
    Console.WriteLine ("{0} year old {1} is feeling {2}", Age, Name, Mood);
    versus
    Console.WriteLine (Age.ToString () + " year old " + Name + " is feeling " + Mood.ToString ());
    www.monotorrent.com For all your .NET bittorrent needs

    NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.

  4. #4
    Join Date
    May 2009
    Location
    Netherlands
    Posts
    103

    Re: Timer and object?

    i know it can be written that way. i prefer this method.
    if there is any reason why your method is better. i would be glad to know. like performance? even tho thats not important in homework really, i just wanna get my homework done and be done with it so i can do my own projects

    how do you mean it can be a member of the class? in C++ that would mean that the function is no good, sorry dont know how to explain it. do you mean i can just make it non-static and it will just magicly work?

    okay thanks for your both replies;.
    Last edited by wigga; May 30th, 2009 at 11:55 AM.

  5. #5
    Join Date
    Jan 2002
    Location
    Scaro, UK
    Posts
    5,940

    Re: Timer and object?

    in C++ that would mean that the function is no good
    Yes, quite true. Function pointers in C++ are usually static.

    This is not the case in C# (.NET). Delegates can (and most often are) created for instance methods - the delegate itself holds a pointer to the instance as well as the function.

    do you mean i can just make it non-static and it will just magicly work?
    I'd have thought it would have been quicker to try it than to reply to the post - but yes, it will work if you make your method non-static.

    Then since you have an instance method it can gain access to all instance member variables of the class.

    Darwen.
    www.pinvoker.com - PInvoker - the .NET PInvoke Interface Exporter for C++ Dlls.

  6. #6
    Join Date
    May 2009
    Location
    Netherlands
    Posts
    103

    Re: Timer and object?

    Thanks for your reply.

    "I'd have thought it would have been quicker to try it than to reply to the post"

    that might be true, but than i would leave this forum and never knowing what C# is really actually doing, and not learning anything usefull

    And now you have explained it to me perfectly. Thanks alot!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured