views:

204

answers:

5

How can a static property reference a nonstatic method?

Example:

public static int UserID
{
  get
  {
     return GetUserID();
  }
}

private int GetUserID()
{
  return 1;
}

When I try to compile this, I get the error: "An object reference is required for he non-static field, method or property "GetUserID()"

+2  A: 

You'll just have to create a new instance:

public static int UserID
{
  get
  {
    return new MyClass().GetUserID()     
  }
}

Well, you don't have to create a new instance every time UserId is called -- you can have a static field containing an instance of MyClass instead (which of course would be an approach toward implementing the Singleton pattern).

Although you can read that your static property is calling a method that could be made static, the other method isn't static. Thus, you must call the method on an instance.

Mark Rushakoff
Though technically correct, I'll point out, though, that this is typically a big red flag. Code that constructs a new instance just to return a static property value is typically a sign of a real bad design flaw...
Reed Copsey
+1  A: 

You need somehow to get an instance. Without an instance, it's impossible to call an instance method.

For your case, are you sure that you need GetUserID() to be an instance method? It returns anyway the same value. Or, if your code is just dummy, and you require more logic in GetUserID(), maybe you can tell us what you intend to do?

Vlad
+7  A: 

This doesn't work.

When you define a static property (or static method), you're defining a property that works on the class type, not on an instance of the class.

Instance properties and methods, on the other hand, work upon a specific, constructed, instance of a class. In order to use them, you need to have a reference to that specific instance. (The other way around, however, is fine.)

As an example, think of Fruit, and an "Apple" class. Say the apple class has an instance property that is how ripe the Apple is at this point in time.

You wouldn't as "Apple" to describe how ripe it is, but rather a specific "Apple" (instance). On the other hand, you could have an instance of an apple, and ask it whether it contains seeds (which might be defined on the Apple class itself (static)).

Reed Copsey
What about genetically-modified seedless apples?
Jake
mmm, tasty. Subclass it!
Reed Copsey
A: 

Simply it can't.

If you need to call a static method to invoke an instance method, probably you want a Singleton

Try look at: http://en.wikipedia.org/wiki/Singleton_pattern

Example:

public sealed class Singleton
{
    private static readonly Singleton _instance = new Singleton();

    private Singleton() { }

    public static Singleton Instance
    {
        get
        {
            return _instance;
        }
    }

    public static int UserID
    {
        get
        {
            return _instance.GetUserID();
        }
    }

    private int GetUserID()
    {
        return 1;
    }

}
digEmAll
A: 

An easy way to think about it is the following: a non-static method (or property, because properties are just wrapped-up methods) receives, as a first hidden parameter, a reference to the instance on which they operate (the "this" instance within the called method). A static method has no such instance, hence nothing to give as first hidden parameter to the non-static method.

Thomas Pornin