2

I have been trying to fix this for quite a while and I don't know if its my code or if it can't find it in VS. I've literally tried everything and I need help

Error I get:

An object reference is required for the non-static field, method, or property 'WindowsFormsApplication3.Form1.label1' c:\users\zmatar\documents\visual studio 2013\projects\windowsformsapplication3\windowsformsapplication3\form1.cs

Code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net.NetworkInformation;

namespace WindowsFormsApplication3
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        public static void PingTest()
        {
            const int timeout = 120;
            const string data = "[012345678901234567890123456789]";
            var buffer = Encoding.ASCII.GetBytes(data);
            PingReply reply;
            var success = true;    // Start out optimistic!
            var sender = new Ping();

            // Add as many hosts as you want to ping to this list
            var hosts = new List<string> { "www.google.com", "www.432446236236.com" };

            // Ping each host and set the success to false if any fail or there's an exception
            foreach (var host in hosts)
            {
                try
                {
                    reply = sender.Send(host, timeout, buffer);

                    if (reply == null || reply.Status != IPStatus.Success)
                    {
                        // We failed on this attempt - no need to try any others
                        success = false;
                        break;
                    }
                }
                catch
                {
                    success = false;
                }
            }

            if (success)
            {
                label1.ForeColor = System.Drawing.Color.Red;
            }
            else
            {
                label1.ForeColor = System.Drawing.Color.Red;
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            timer1.Start();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            PingTest();
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void menuStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {

        }
    }
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Billyhoe5
  • 85
  • 1
  • 8
  • @TyCobb That would generate a slightly different error. "object reference required" errors are (nearly?) always issues relating to `static` things accessing instance things. – BradleyDotNET Sep 11 '14 at 23:50
  • possible duplicate of [An object reference is required for the nonstatic field, method, or property 'WindowsApplication1.Form1.setTextboxText(int)](http://stackoverflow.com/questions/498400/an-object-reference-is-required-for-the-nonstatic-field-method-or-property-wi) – jordanhill123 Sep 12 '14 at 00:57

1 Answers1

1

label1 is a instance variable. You are trying to set it in a static method.

static methods cannot access instance members without an instance to go to. To fix it, remove static from the method, or store an instance of the class for use later:

public class Form1 : Form
{
   static Form1 instance = null;

   public Form1()
   {
       InitializeComponent();
       instance = this;
   }

   private static void MyMethod()
   {
      if (instance != null)
         instance.label1.Color = Color.White; //Or whatever
   }
}
BradleyDotNET
  • 60,462
  • 10
  • 96
  • 117