静态成员实例引用问题 [英] Static member instance reference issue

查看:25
本文介绍了静态成员实例引用问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Analytics 类中保存了以下静态方法:

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

在第二节课中,我尝试按如下方式填写:

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

但是,当我这样做时,我收到以下错误:

<块引用>

错误 CS0176:无法使用实例引用访问静态成员 `Analytics.buttonHit(string, string, string)',请改为使用类型名称对其进行限定

有人能告诉我如何克服这个错误吗?

解决方案

使用 class 名称而不是实例.应该使用类名访问静态成员.

Analytics.buttonHit(event_NameString, event_ActionString, event_Label);

<块引用>

一个静态函数成员(方法、属性、事件、运算符或构造函数)不对特定实例进行操作,它是一个在这样的函数成员中引用此编译时错误,MSDN

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?

解决方案

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

这篇关于静态成员实例引用问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆