4

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'

Community
  • 1
  • 1
DeadAlive
  • 58
  • 1
  • 1
  • 9
  • 3
    Yes, it would give you that error. Now, how much do you understand about the differences between static and instance members, and return values? – Jon Skeet Jul 04 '12 at 20:45
  • Winforms is frequently the nemesis of a budding .NET programmer. It is deceptively simple to make something work, the designer is very helpful to make something half-decent looking going quick. Inevitably, you'll run into the wall of Object Oriented Programming, you *really* need to understand the difference between a type and an object to scale it. Good books aplenty, certainly something you can learn in a school. Not here. – Hans Passant Jul 04 '12 at 21:10
  • How this has been upvoted 3 times is beyond my comprehension, it is basic knowledge – Danny Watson Dec 29 '16 at 21:20

3 Answers3

4

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()
{
   ...
}
Tigran
  • 61,654
  • 8
  • 86
  • 123
  • i removed it after that public string iptrace() { ... } it saying 'geoIP.Form1.iptrace()': not all code paths return a value – DeadAlive Jul 04 '12 at 22:25
  • 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`. – Tigran Jul 05 '12 at 05:35
0

Remove static from iptrace() method declaration.

You are trying to access non-static (instance) member textBox1 from static method.

Edit:

Also, change the method declaration to public void iptrace(). Since you don't have return <some string value>; statement(s) (and you don't need any in your case), the C# compiler will object. By having void return type, you are stating that the method doesn't return any value.

Edit 2:

You should also change method access to private void iptrace(). No need for it to be public.

And finally, check .NET Guidelines for Names. For instance, methods and namespaces should be Pascal Cased.

Miroslav Popovic
  • 12,100
  • 2
  • 35
  • 47
0

Change textBox1.Text to be Form1.ActiveForm.textBox1.Text

Chris Laplante
  • 29,338
  • 17
  • 103
  • 134