C#“非静态字段,方法或属性需要对象引用" [英] C# "An object reference is required for the non-static field, method, or property"

查看:76
本文介绍了C#“非静态字段,方法或属性需要对象引用"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

上个星期我也遇到过同样的问题,只是父母班的教he:

Having the same issue i was last week only with inheiriting from the parent class:

public ExtendedTime(int Hour, int Minute, String TimeZone) :base(hour, minute)
{

    timeZone = TimeZone;
}//end of ExtendedTime

:基准(小时,分钟)是我遇到此错误的地方.在小时和分钟都说相同的问题.现在通常我会说我只是在缺少某种属性,但是我尝试了一下,但可悲的是它没有任何好处.
在父类中的小时和分钟声明为:

:base(hour,minute) is where i have this error. Says the same problem for both hour and minute. Now usually I would say that i'm missing something far as a property but i tried that and it didn't do any good sadly.
in the parent class hour and minute are declared as following:

    internal int hour;
    internal int minute;

我有设置者和获取者的设置.

And i have setters and getters setup.

推荐答案

您尝试在字段 hour minute 时使用您可能打算使用构造函数参数.调用基类构造函数时,不能使用字段(或任何其他实例成员).

You're trying to use the fields hour and minute when you probably meant to use the constructor parameters. You can't use fields (or any other instance members) when calling a base class constructor.

我个人会更改构造函数参数以使用更常规的名称:

Personally I'd change the constructor parameters to have more conventional names:

public ExtendedTime(int hour, int minute, String timeZone) : base(hour, minute)
{    
    this.timeZone = timeZone;
}

请注意,如果您将字段设为 private 而不是内部字段,则问题将更加明显,因为您一开始将无法访问这些字段:)

Note that if you made your fields private instead of internal, the issue would have been more obvious, as you wouldn't have access to the fields in the first place :)

这篇关于C#“非静态字段,方法或属性需要对象引用"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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