Static member instance reference issue [duplicate]

折月煮酒 提交于 2019-12-12 12:39:12

问题


I have the following static method held in an Analytics class:

 public static void buttonHit(string eventName, string eventAction, string description)
{
    gua.sendEventHit(eventName,eventAction,description,1);
}

In a second class I'm trying to fill this out as follows:

Analytics analytics;
void buttonEventAnalytic()
{   
   analytics.buttonHit(event_NameString, event_ActionString, event_Label);  
}

However when I do so I get the following error:

error CS0176: Static member `Analytics.buttonHit(string, string, string)' cannot be accessed with an instance reference, qualify it with a type name instead

Could someone please enlighten me as to how I can defeat this error?


回答1:


Use class name instead of instance. Static members are supposed to be accessed with class name.

Analytics.buttonHit(event_NameString, event_ActionString, event_Label);  

A static function member (method, property, event, operator, or constructor) does not operate on a specific instance, and it is a compile-time error to refer to this in such a function member, MSDN




回答2:


use directly

Analytics.buttonHit(event_NameString, event_ActionString, event_Label);  

because static member are not related to object they are associated with class only



来源:https://stackoverflow.com/questions/21019492/static-member-instance-reference-issue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!