2017-01-03 4 views
-3

В моей программе мне нужно создать экземпляр класса, который является «Автомобилем», у класса Vehicle есть таймер внутри него, а также логические «vExists», когда таймер попадает в интервал.Экземпляр экземпляра класса экземпляра класса C#

class VehicalGen 
{ 
    private Random rn1 = new Random(); 

    private int agrotime; 
    private bool vExists; 
    //static AgrivationHandler agroTimeHandler; 
    private static Timer agroTimer = new Timer(100); 

    //agrotime: anywhere between 2 and 4 seconds (2000 to 4000) 

    ///Generates the new Vehicles using GetCarType to change the outcome of the later variables 
    public VehicalGen() 
    { 
     this.vtype = RandomGeneration.GetCarType(rn1); 
     switch (this.vtype) 
     { 
      [....] //This is where the details of the vehicle are generated. 
     } 
     this.agrotime = RandomGeneration.GetRandomTime224(rn1); 
     this.vExists = true; 
     this.serviced = false; 
     //agroTimeHandler = new AgrivationHandler(agrotime); 
     agroTimer.Interval = agrotime; 
     agroTimer.Elapsed += AgroTimerElapsed; 
     agroTimer.Start(); 
    } 

    public static void AgroTimerElapsed(object sender, ElapsedEventArgs e) 
    { 
     //here is where I need to set vExists to "False" 
    } 
} 

Однако мне нужно создать конкретный экземпляр класса, насколько я знаю, чтобы сделать это, потому что это не нравится «this.vExists = ложь», это просто ошибки.

+0

Вам следует попытаться написать код и разместить его здесь (см. [MCVE] для руководства). Пытаясь что-нибудь еще (прыгать, петь, бегать), не собирается помогать и, безусловно, не делает пост хорошим вопросом SO. –

ответ

0

Да возможно. Попробуйте следующее:

private void LoadTimer() 
    Timer timer = new Timer 
    timer.Interval = 4000; 
    timer.Enabled = true; 
    timer.Tick += new EventHandler(timer1_Tick); 
    timer.Start(); 
} 

private void timer1_Tick(object sender, EventArgs e) 
{ 
    // do your boolean operation where 
} 
Смежные вопросы