0

I am getting this error and I don't know why.

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

Why do I need to have an object reference here? My code is as below:

public string GetChanges()
    {
        string changelog = "";

        MySqlConnection connection = new MySqlConnection("server=127.0.0.1;uid=root;pwd=pass;database=data");
        try
        {
            connection.Open();

            MySqlCommand cmd = new MySqlCommand("SELECT `change_log` FROM version WHERE ID = '1'", connection);

            MySqlDataReader reader = cmd.ExecuteReader();
            while (reader.Read())
            {
                if (!reader.IsDBNull(0))
                {
                    changelog = reader.GetString(0);
                }
            }

            connection.Close();
        }
        catch
        {
            //MessageBox.Show(e.Message, "Warning!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
        }

        return changelog;
    }

I am calling the above function like so:

string changelog = GetChanges();

Why is an object reference required in this case? I cannot use static because I am creating a Web Service which doesn't work with static methods. How can I alter this to use objects?

Thanks

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
nerdalert
  • 441
  • 1
  • 5
  • 22
  • 1
    Where exactly are you calling `getChanges()` from? – Rhumborl Mar 01 '16 at 10:00
  • 1
    Your `GetChanges` method is an instance method _by default_. You _can't_ call it without the type name that belongs. Make it `static` or call it with it's type like `string changelog = MyType.GetChanges();` – Soner Gönül Mar 01 '16 at 10:00
  • Where are you calling that `GetChanges` method? It sounds to me you should separate the logic of the method from the web service interface, and call the logic instead of the service from your code. Calling a web service method directly smells - you're basically trying to fudge the interface. – Luaan Mar 01 '16 at 10:02
  • The code I posted isn't yet in my Web Service actually. I was testing it initially by having the method in the Program.cs of a winforms project, and calling it from there as well. But now that I put the method in my web service, and call it from the winforms project there seems to be no issues with objects. Out of curiosity Luaan, why does calling a web method directly smell? I have the above function in the web method, and am calling it from another project like so: MyService.GetChanges(); after creating a WebService object. Am I calling it directly like this? – nerdalert Mar 01 '16 at 10:22

3 Answers3

2

Since your GetChanges is not a static method, it cannot be called without the instance of the object which has that method:

public class MyClass {
    public string GetChanges(){
        ....
        return str;
    }
}

Then you can call it like this:

MyClass insMyClass = new MyClass(); //note the instance of the object MyClass
string str = insMyClass.GetChanges();

Or, if you declare it as static, you have to use the class name to call:

public static string GetChanges(){ //note the static here
    ....
    return str;
}

Call it like this:

string str = MyClass.GetChanges(); //note the MyClass is the class' name, not the instance of the class

Only if you call GetChanges in the class itself, then it is okay:

public class MyClass {
    public string GetChanges(){
        ....
        return str;
    }

    public void Foo(){
        string somestr = GetChanges(); //this is ok
    }
}
Ian
  • 30,182
  • 19
  • 69
  • 107
2

Your call is ok only inside your class. Outside it, not.

You have to define it as:

public class MyClass{
    public static string GetChanges(){...}
 }

and call it

MyClass.GetChanges();

or create an instance of the class containing the GetChanges method.

Example:

public class MyClass
    public string GetChanges(){....}

then

var mc = new MyClass();
mc.GetChanges();
Piero Alberto
  • 3,823
  • 6
  • 56
  • 108
2

You cannot invoke a non static method from a static method.

You need to instantiate an object from there class where GetChanges() is declared.

Foo f = new Foo();
f.GetChanges();
Harry
  • 3,031
  • 7
  • 42
  • 67