2014-01-09 89 views
2

我有一個Analytics(分析)類舉行了下列靜態方法:靜態成員實例引用問題

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

在第二類我試圖填補這一列如下:

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

然而當我這樣做,我得到以下錯誤:

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

可能有人請賜教,我怎麼能打敗這個犯錯要麼?

回答

2

使用class名稱而不是實例。應該使用類名稱訪問靜態成員。

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

0

直接使用

Analytics.buttonHit(event_NameString, event_ActionString, event_Label); 

因爲反對它們與類相關的靜態成員不是隻涉及

+2

這是C++符號。在C#中使用'.'而不是'::'。 –

+0

感謝您的糾正 – Jai