开发者

Static property references non-static method

开发者 https://www.devze.com 2022-12-27 17:19 出处:网络
How can a static property reference a nonstatic method? Example: public static int UserID { get { return GetUserID();

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 requ开发者_运维技巧ired for he non-static field, method or property "GetUserID()"


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)).


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.


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?


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.


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;
    }

}
0

精彩评论

暂无评论...
验证码 换一张
取 消