简体   繁体   中英

C# Fixing error: “An object reference is required for the non-static field, method, or property”

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();
    }

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();
}
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM