0

I'm creating a basic C# game but code is not compiling, with the following error

CS0120: An object referenceis required for the non-static field, method or property 'MainMenu.timer1'

This is the code:

try
{
    Ping myPing = new Ping();
    String host = "www.google.com";
    byte[] buffer = new byte[32];
    int timeout = 4000;
    PingOptions pingOptions = new PingOptions();
    PingReply reply = myPing.Send(host, timeout, buffer, pingOptions);
}
catch (Exception)
{
    MessageBox.Show("You aren't connected a internet or connection is to slow. Please check your connection.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
    MainMenu.timer1.Enabled = false;
}

Here's the timer1's code:

public void timer1_Tick(object sender, EventArgs e)
{

    if (toolStripProgressBar1.Value <= 100)
    { 
        toolStripProgressBar1.Value += 1;
        string 123;
        123 = label3.Text;
        int 1, 2, answ;
        sayi1 = Convert.ToInt32(toolStripStatusLabel1.Text);
        sayi2 = Convert.ToInt32(123);
        answ = 1 + 2;
        string ans;
        ans = answ.ToString();
        toolStripStatusLabel1.Text = ans;
        external = ans;
        this.Text = "Game (%" + ans + ") - Wait";      
    }
    
    if (toolStripProgressBar1.Value >= 100)
    {
        System.Diagnostics.Process.Start(@"bin/MOOperator.exe");
        timer1.Stop();
        Application.Exit();
    }
}

I'm beginner at C# and I don't know much, thanks in advance for solutions.

2 Answers2

0
MainMenu.timer1.Enabled = false;

Presumably MainMenu is a class and not a variable. You can't access a member directly on a class unless that member is static. As the error states, you need to reference an instance of MainMenu, not the class itself.


In a traditional sense, the difference can be illustrated as follows...

Accessing a static member on a class directly:

SomeClass.SomeMember();

Accessing a non-static member on an instance of a class:

var someInstance = new SomeClass();
someInstance.SomeMember();

Semantically this is a very important distinction. static isn't just a keyword to throw around until something works, it changes the fundamental meaning of what it's applied to.

As a semantic example, consider a class Person. If you want to ask a Person for some information, such as HairColor, then you'd need an instance. Because any given instance of a person can have their own hair color, independent of other instances. So you'd never call Person.HairColor, you'd call someInstanceOfPerson.HairColor.


You do reference the non-static member elsewhere here:

timer1.Stop();

So, is timer1 a member of the currently executing class? If so, just be consistent with that:

timer1.Enabled = false;

Otherwise, if this code is running somewhere else (somewhere outside of the class which defines timer1, then you need an instance of that class. Without knowing more about your code, the absolute simplest would be as above:

var mainMenu = new MainMenu();
mainMenu.timer1.Enabled = false;

However, I don't know anything about your MainMenu class or its timer1 member. It may be private, you may have another instance of that class already and not want to operate on a new one, etc.

The point, however, remains the same. You need to identify which MainMenu (which instance of it) you want to reference. You can't just reference the general class itself.

David
  • 208,112
  • 36
  • 198
  • 279
-2

Declare timer1 this way: public static Timer timer1;

Clay Ver Valen
  • 1,033
  • 6
  • 10
  • 1
    The "make every static" approach to solving problems generally creates more problems than it solves. – David May 18 '16 at 17:26
  • @David - Agreed. `static` has it's uses, but it can also be very, very dangerous and lead to lots of...."entertaining" bugs to track down and fix. – Tim May 18 '16 at 17:28
  • He didn't ask for best practices, he asked for a solution. If we get into best practices then we could start with him trying declare a string variable with the name 123, which won't even compile. – Clay Ver Valen May 18 '16 at 17:33
  • 1
    @ClayVerValen: Throwing around keywords just to get one line of code to compile, at the expense of making the rest of the system fail, isn't really a solution. – David May 18 '16 at 17:38
  • Given that it won't compile, not sure what you might mean by "system." Regardless, given the source code in the post and the question it is safe to say the poster is new to C# and likely OOP. We can't all be software consultants (but we can both have Magenic on our resume - ;-). You can tell Paul I said "Hi". – Clay Ver Valen May 18 '16 at 17:46