Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# "An object reference is required for the non-static field, method, or property"

Tags:

c#

.net

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.

like image 917
David Brewer Avatar asked Jan 27 '11 15:01

David Brewer


1 Answers

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

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

like image 180
Jon Skeet Avatar answered Nov 08 '22 10:11

Jon Skeet