1

I'm a C#(and programming) newbie. I have a windows store app where I want to perform some actions on a countdown timer's time up. So I tried creating an event like in here and the resulting code is this:

CountDownTimer.cs

public void timer_Tick(object sender, object e)
{
    if (sw.Elapsed < duration)
    {
        Seconds = (int)(duration - sw.Elapsed).TotalSeconds;
        TimeElapsed = String.Format("{0} second(s)", Seconds);
    }
    else
    {
        TimeElapsed = "Times Up";
        timer.Stop();
        Views.EquationView.OnTimesUp();
    }
}

EquationView.xaml.cs

public event System.EventHandler TimesUp;
public void OnTimesUp()
{
    if (TimesUp != null)
    {
        TimesUp(this, EventArgs.Empty);
        submitButton.IsEnabled = true;
        MessageDialog msgDialog = new MessageDialog("Your time's up which counts as a wrong answer. Click start for a new equation");
        msgDialog.ShowAsync();
        //more code
    }
}

But I get the error mentioned in the question title. Searching around MDSN and SO seems I have two options

1) setting OnTimeSUp() to static but then submitBtn.IsEnabled = true; gives me an error. I tried to pass the button as a parameter but it won't do

2) changing Views.EquationView.OnTimesUp(); to this.Views.EquationView.OnTimesUp(); or creating a new instance and then instance.Views.EquationView.OnTimesUp(); still problem

I'd much appreciate any help. I'm also open to suggestions for more elegant and efficient code.

--UPDATE-- Thank's to A.Abramov I no longer get errors but besides showing the message dialog and updating the data model(1st line) the method OnTimesUp() does nothing. When invoked by other methods the other lines do work.

CountDownTimer.cs

    public void timer_Tick(object sender, object e)
    {
        if (sw.Elapsed < duration)
        {
           //code
        }
        else
        {
           //THE CHANGE
            new Views.EquationView().OnTimesUp();
        }
    }

EquationView.xaml.cs

    public void OnTimesUp()
    {
        App.player.AnsweredWronglyAsync();
        AfterWrongAnswer();
        submitButton.IsEnabled = false;
        commandBarResetButton.IsEnabled = false;
        MessageDialog timesUpMsg = new MessageDialog("Time's up!");
        timesUpMsg.ShowAsync();
    }
Community
  • 1
  • 1
D3v
  • 135
  • 1
  • 16
  • In which line exacly do you get this mistake? Just to make sure i`m answering the right question (: – A. Abramov Jan 17 '15 at 18:56
  • It is line Views.EquationView.OnTimesUp(); in CountDownTimer.cs :) – D3v Jan 17 '15 at 18:57
  • Well, The next step is understanding **why** does it do nothing. The way to solve this is breakpoints, if you're familier with them. Debug your project and check if it gets into the function at all. If it does - Does it get stuck somewhere? Does it get to the messageDialog? This way, we can know what exacly is going wrong. – A. Abramov Jan 19 '15 at 22:20
  • I set some breakpoints and all lines appear to execute fine. I got some **"Your step-into request resulted in an automatic step-over of a property or operator"** messages but despite that everything appears fine. I also checked that it **does update my player model** by adding a wrong answer to the total wrong answers. I can't understand though why the buttons are not being enabled/disabled. – D3v Jan 20 '15 at 12:19
  • What do you mean by everything is fine? Does it get into the function? Where does it stop? Does it run the Message-Dialog line at all? – A. Abramov Jan 20 '15 at 20:38
  • The message dialog has been appearing since the beginning. Also I added the line `App.player.AnsweredWronglyAsync();` which updates the data model. By checking the profile of the player after the times up msg I see that it does increase the number of wrong answers. But the lines `AfterWrongAnswer();` `submitButton.IsEnabled = false;` `commandBarResetButton.IsEnabled = false;` do nothing. – D3v Jan 21 '15 at 09:02
  • 1
    it means that OnTimesUp event is automatically triggering. Make sure your delegates / Events are well set Here: http://www.akadia.com/services/dotnet_delegates_and_events.html or here: http://csharpindepth.com/Articles/chapter2/events.aspx or here: https://msdn.microsoft.com/en-us/library/awbftdfh.aspx ; If you still have problems, you might want to consider reprogramming this specific function. If you still face a problem, contact me over private chat, and there i`ll ask you to send me your whole code. Also - this is a different problem. You can open a new thread for it to get more help :) – A. Abramov Jan 22 '15 at 05:03
  • Thank you for the links and the help I'll look into it :) Yes this question has strayed from the original. I'll make a new one if the problem persists. – D3v Jan 23 '15 at 11:03

1 Answers1

2

Well well well. isn't this a typical OOP problem that should be explained more. I guess i`ll be the one to do it.

In your program you are trying to reach Views.EquationView.OnTimesUp(); from the CountDownTimer class. The problem is, that Equation view does not have OnTimesUp() for all the objects, it's object specific - so C# is not sure how to activate the function.

The solution is, as you have described - to create an instance of View.EquationView.OnTimesUp() - this way, the function will trigger from within the object.

You say it's problematic, i say we're here to solve problems. Try this code - if it's wrong, comment on this answer with the compiler message, and we'll see how to fix it :)

public void timer_Tick(object sender, object e)
{
if (sw.Elapsed < duration)
{
    Seconds = (int)(duration - sw.Elapsed).TotalSeconds;
    TimeElapsed = String.Format("{0} second(s)", Seconds);
}
else
{
    TimeElapsed = "Times Up";
    timer.Stop();
    new Views.EquationView().OnTimesUp();
}
}
A. Abramov
  • 1,701
  • 12
  • 37
  • I get no errors but when the time's up nothing happens. I changed the code to this to see if the event fires off but nothing happens maybe it's the event now. `public **static** event System.EventHandler TimesUp;` `public static void OnTimesUp() { if (TimesUp != null) { MessageDialog msgDialog = new MessageDialog("..."); msgDialog.ShowAsync(); } }` – D3v Jan 17 '15 at 19:49
  • If there are no runtime / compliation errors then the problem is not in the OOP (i.e static, etc.) otherwise you'd get a notice. You might want to set breakpoints and see if the code reaches your function at all - or check if the function works properly. – A. Abramov Jan 18 '15 at 11:38
  • Yes my bad now that I'm delving into events I see there are some things missing. Thank you very much for your time! – D3v Jan 18 '15 at 18:17
  • @Α Sorry I temporarily removedthe solution check. I have a new problem and it seems it has to do with the new instance because when I invoke OnTimesUp() only the message dialog code lines seem to work. What should I do concerning the problem and this question as well? – D3v Jan 19 '15 at 16:10
  • Sure (: I do not get what you are meaning by this - edit the main post. this way i can get another try, and maybe others can help you too. – A. Abramov Jan 19 '15 at 16:17