Freddy
Freddy

Reputation: 970

An object reference is required non-static field, method, or property error

I have 2 existing functions. One I can't edit (getServiceNames) and in the second I need to set a string (getDataTableOne). I want to set this string with a function in the same class (getAllExceptNiServiceNames) but he gives me this error because my function i would like to edit is static.

An object reference is required non-static field, method, or property 'Queries.getAllExceptNiServiceNames()'

I can't remove the static property of the function and I also can't make a string object. Whats the best way to fix this?

public static DataSet getDataTableOne()
{
 string serviceNameFilterLineThree = getAllExceptNiServiceNames(); //ERROR
}

public static DataSet getServiceNames()
    {
        DataSet ds = new DataSet();
        string query_select = "select test";

        ds = QualityGate.fillDataset(query_select);
        return ds;
    }

public string getAllExceptNiServiceNames()
{
    string sql = "";
    DataSet ds = getServiceNames();
    int i = 0;
    foreach (DataRow theRow in ds.Tables[0].Rows)
    {   
        if (i != 0)
            sql += "AND ";

        sql += "serviceName = '" + theRow["serviceName"].ToString() + "' ";

        i++;
    }
    return sql;
}

Upvotes: 1

Views: 1900

Answers (3)

Katana314
Katana314

Reputation: 8610

I think Dan's answer might fix your immediate problem, but I'm kind of wondering if it'll help you to have a quick overview of what the static operator means; this confused me early in my CS studies, and I think I fell into a pattern of just declaring everything static because "that's the only way it worked".

In some languages, non-static functions (or "object functions" let's say) are all declared this way:

function Account.withdraw(self, amt)
  self.balance -= amt;
end

That self part is a reference to the object that the method is operating on; so if you have 5 accounts, and you call bobAccount:withdraw(5), only Bob's account loses money, because bobAccount becomes self. (If you're wondering, that language is called Lua). In C#, this would just be declared this way inside the class Account...

public void withdraw(double amt) {
  balance -= amt;
}

Note that there's no more "self". By default, C# methods operate on the instance of the object, and variable names are assumed to refer to variable instances declared as part of the class.

static methods are methods that are not tied to any individual object. This might be fitting if you have an operation that is very closely tied to Accounts, but doesn't just use one particular instance of them - or, if you're creating an object in a special way, as you're doing in getServiceNames(). (These can be known as "factory methods"). So if you have a static method that you want to operate on a specific object, you can either declare that method as static, or call the function on a particular instance of that object (oneObject.myFunction())

So I won't tell you exactly how you should be designing your program, but I hope giving you a better idea of how things are organized gives you a better idea in your own mind of how you want it to be.

Upvotes: 2

Pawan
Pawan

Reputation: 1075

You can not invoke a non static method from a static method. So you have to write

public static  string getAllExceptNiServiceNames()
{
    string sql = "";
    DataSet ds = getServiceNames();
    int i = 0;
    foreach (DataRow theRow in ds.Tables[0].Rows)
    {   
        if (i != 0)
            sql += "AND ";

        sql += "serviceName = '" + theRow["serviceName"].ToString() + "' ";

        i++;
    }
    return sql;
}

This may solve your error.

Upvotes: 1

Dan Puzey
Dan Puzey

Reputation: 34200

You need to declare your method as static:

public static string getAllExceptNiServiceNames() { ... }

Or alternatively create an instance of your class before using the method:

var q = new Queries();
string serviceNameFilterLineThree = q.getAllExceptNiServiceNames();

Upvotes: 6

Related Questions