Thread: Problem with C# timers and using objects

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    55

    Problem with C# timers and using objects

    In the code below, I can't figure out how to use the objects Italy and France with their timers. The code works, but if I create a local object of them, it won't keep the data. Or if I try to create a global object, I end up getting ambiguity errors ( even when I'm just assigning them an instance! ). Please help me, as I am very frustrated with not being able to get it to actually use the same instance of a object in the timers.


    Code:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    
    
    namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
            public int interval = 1;
    
    
            void DeclareObjects();
    
    
            public Form1()
            {
                InitializeComponent();
    
    
                StartTimers.Enabled = true;
                StartTimers.Start();
            }
    
    
            private void StartTimers_Tick(object sender, EventArgs e)
            {
                switch (interval)
                {
                    case 1: GiveTroopsToItaly.Enabled = true; GiveTroopsToItaly.Start(); break;
                    case 2: GiveTroopsToFrance.Enabled = true; GiveTroopsToFrance.Start(); break;
                }
    
    
                if (interval == 2)
                {
                    StartTimers.Enabled = false;
                    StartTimers.Stop();
    
    
                    return;
                }
                else
                {
                    interval++;
                }
            }
    
    
            private void GiveTroopsToItaly_Tick(object sender, EventArgs e)
            {
    
            }
    
    
            private void GiveTroopsToFrance_Tick(object sender, EventArgs e)
            {
            }
    
    
            private void DeclareObjects()
            {
                Country Italy;
                Country France;
    
    
                Italy = new Country(ItalyTroopCount, 0);
                France = new Country(FranceTroopCount, 0);
            }
        }
    }

  2. #2
    Registered User
    Join Date
    Jan 2013
    Posts
    55
    It also keeps trying to tell me that a class is a label!

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    where is country declared? post your actual errors so we can see them.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  4. #4
    Registered User
    Join Date
    Jan 2013
    Posts
    55
    It's declared in the public void method DeclareObjects();.

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by BatchProgrammer View Post
    It's declared in the public void method DeclareObjects();.
    ok, now tell me where the class Country is declared.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  6. #6
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    You shouldn't make them locals - the instant DeclareObjects() finishes, your objects go out of scope and are marked for garbage collection.

    They shouldn't be globals, either. You could make them as fields or properties of the Form1 class, or you could create a separate class (to separate business logic from UI).
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A problem with pointers and objects.
    By ihorse in forum C++ Programming
    Replies: 3
    Last Post: 06-06-2010, 04:40 PM
  2. Problem with translations and timers?
    By Jake.c in forum Game Programming
    Replies: 1
    Last Post: 09-19-2009, 11:32 AM
  3. Problem with pointer to vector objects
    By ryeguy in forum C++ Programming
    Replies: 5
    Last Post: 02-11-2008, 07:42 AM
  4. System.Timers.Timer problem
    By Rune Hunter in forum C# Programming
    Replies: 2
    Last Post: 01-29-2008, 02:29 PM
  5. Replies: 4
    Last Post: 10-16-2003, 11:26 AM

Tags for this Thread