An object reference is required for the nonstatic field, method, or property 'WindowsApplication1.Form1.TextboxText().. same with labels

10,041

your textBox1 control is declared like a member of the class, but used inside static function. That is a problem.

To fix this, you need to declare method non static

public string iptrace()
{
   ...
}
Share:
10,041
DeadAlive
Author by

DeadAlive

C# Developer.

Updated on July 18, 2022

Comments

  • DeadAlive
    DeadAlive almost 2 years

    Possible Duplicate:
    An object reference is required for the nonstatic field, method, or property 'WindowsApplication1.Form1.setTextboxText(int)

    i am creating an IP tracer, which will connect to web and put value(IP) from textbox and will recieve the result. here is the code.

     using System;
        using System.Collections.Generic;
        using System.ComponentModel;
        using System.Data;
        using System.Drawing;
        using System.Linq;
        using System.Text;
        using System.Windows.Forms;
        using System.Xml;
    
        namespace geoIP
        {
            public partial class Form1 : Form
            {
                public Form1()
                {
                    InitializeComponent();
                }
    
                private void button1_Click(object sender, EventArgs e)
                {
                    iptrace();
                }
    
                 public static string iptrace()
                {
                    XmlDocument xmldoc = new XmlDocument();
                    XmlNodeList xmlnode;
                    int i;
    
                    xmldoc.Load("http://freegeoip.net/xml/" + textBox1.Text);
                    xmlnode = xmldoc.GetElementsByTagName("response");
                    for (i = 0; i < xmlnode.Count; i++)
                    {
                        xmlnode[i].ChildNodes.Item(0).InnerText.Trim();
                        label1.Text = "Ip Address: " + xmlnode[i].ChildNodes.Item(0).InnerText.Trim();
                        label2.Text = "Country Code: " + xmlnode[i].ChildNodes.Item(1).InnerText.Trim();
                        label3.Text = "Country Name: " + xmlnode[i].ChildNodes.Item(2).InnerText.Trim();
                        label4.Text = "Region Code: " + xmlnode[i].ChildNodes.Item(3).InnerText.Trim();
                        label5.Text = "Region Name: " + xmlnode[i].ChildNodes.Item(4).InnerText.Trim();
                        label6.Text = "City: " + xmlnode[i].ChildNodes.Item(5).InnerText.Trim();
                        label7.Text = "Zip Code: " + xmlnode[i].ChildNodes.Item(6).InnerText.Trim();
                        label8.Text = "Latitude: " + xmlnode[i].ChildNodes.Item(7).InnerText.Trim();
                        label9.Text = "Longitude: " + xmlnode[i].ChildNodes.Item(8).InnerText.Trim();
                        label10.Text = "Metro Code: " + xmlnode[i].ChildNodes.Item(9).InnerText.Trim();
                    }
    
                }
    
    
     }
    }`
    

    it giving me error

    An object reference is required for the non-static field, method, or property 'geoIP.Form1.textBox1'

  • DeadAlive
    DeadAlive almost 12 years
    i removed it after that public string iptrace() { ... } it saying 'geoIP.Form1.iptrace()': not all code paths return a value
  • DeadAlive
    DeadAlive almost 12 years
    i removed it after that public string iptrace() { ... } it saying 'geoIP.Form1.iptrace()': not all code paths return a value
  • sgoodman
    sgoodman almost 12 years
    Your method declaration is public string iptrace() {...}. You need to return a string value from it. But since you are just processing and don't expect anything returned, change the declaration to public void iptrace() {...}.
  • DeadAlive
    DeadAlive almost 12 years
    done, have you got any logical error in this source?
  • DeadAlive
    DeadAlive almost 12 years
    done, have you got any logical error in this source?
  • sgoodman
    sgoodman almost 12 years
    Added few more tips to the answer. Don't see any logical error.
  • DeadAlive
    DeadAlive almost 12 years
    then why its not showing result :(
  • sgoodman
    sgoodman almost 12 years
    @jammyhunt Do you get an error? Is the method iptrace() called at all? Check if the button1_Click event handler is correctly bound to button1's Click event.
  • DeadAlive
    DeadAlive almost 12 years
    i found the bug :D xmlnode = xmldoc.GetElementsByTagName("response"); it should be Response insted of responce
  • Tigran
    Tigran almost 12 years
    You defin your function with rturn type string, so return a string value at the end. If you dont't need a string define function wih return type void.