By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,004 Members | 1,167 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,004 IT Pros & Developers. It's quick & easy.

newbie inheritance question

P: n/a
I have a C# console application.

The class which contains the "static void Main()" method inherits from a
base class.

The base class contains a function GetSomething()

When I try to call this function GetSomething from Main(), I get the error
An object reference is required for the nonstatic field, method, or property
"GetSomething()"

How come?

Thanks
Bill

Jun 27 '08 #1
Share this Question
Share on Google+
9 Replies


P: n/a
"BillE" <be****@datamti.comwrote:
I have a C# console application.
The class which contains the "static void Main()" method inherits from
a base class.
The base class contains a function GetSomething()
When I try to call this function GetSomething from Main(), I get the
error An object reference is required for the nonstatic field, method,
or property "GetSomething()"
How come?
Because the Main method is static. Static methods don't refer to any
particular instance of a class. It sounds a bit weird to have Main in a
derived class like that, but you could try something along these lines:

static void Main() // Main has to be static
{
MainClass m = new MainClass(); // now we have an instance
m.GetSomething();
}

Eq.
Jun 27 '08 #2

P: n/a
"Paul E Collins" <fi******************@CL4.orgwrote:
you could try something along these lines:
Or you could just make the GetSomething method static, so you don't
*need* an instance. That might be more useful for your purposes, but
then you don't need the inheritance at all because static methods are
not inherited.

Eq.
Jun 27 '08 #3

P: n/a
BillE wrote:
I have a C# console application.

The class which contains the "static void Main()" method inherits from a
base class.

The base class contains a function GetSomething()

When I try to call this function GetSomething from Main(), I get the error
An object reference is required for the nonstatic field, method, or property
"GetSomething()"

How come?
Your GetSomething() function is not marked as static in the base class?

Chris.

Jun 27 '08 #4

P: n/a
"Paul E Collins" <fi******************@CL4.orgwrote:
[...] because static methods are not inherited.
Ugh, sorry, I'm apparently half-asleep. Static methods are inherited in
the sense that they're available to the derived class. I meant that
there's only one implementation that cannot be overridden in derived
classes --which isn't relevant here.

Eq.
Jun 27 '08 #5

P: n/a
Static methods are run without an instance of an object being created. The
inherited method not static and there can only run on an instance of the
object as it may use member variables and such things.

If the function doesnt require member variables and is effectively a library
function, make it static too. If it does need other things from the class
then create an instance and call it on that.

HTH
--
Ciaran O''Donnell
http://wannabedeveloper.spaces.live.com
"BillE" wrote:
I have a C# console application.

The class which contains the "static void Main()" method inherits from a
base class.

The base class contains a function GetSomething()

When I try to call this function GetSomething from Main(), I get the error
An object reference is required for the nonstatic field, method, or property
"GetSomething()"

How come?

Thanks
Bill

Jun 27 '08 #6

P: n/a
Thank you.
So everything called from static void Main() must either be static, or an
instance member of another class? Even if the method is in the same class
as staic void Main(), it must still be static, apparently (since I get an
error otherwise).
"BillE" <be****@datamti.comwrote in message
news:Om**************@TK2MSFTNGP03.phx.gbl...
>I have a C# console application.

The class which contains the "static void Main()" method inherits from a
base class.

The base class contains a function GetSomething()

When I try to call this function GetSomething from Main(), I get the error
An object reference is required for the nonstatic field, method, or
property "GetSomething()"

How come?

Thanks
Bill

Jun 27 '08 #7

P: n/a
BillE wrote:
Thank you.
So everything called from static void Main() must either be static, or an
instance member of another class? Even if the method is in the same class
as staic void Main(), it must still be static, apparently (since I get an
error otherwise).
No. Everything called from Main must be static or an instance member of an
object already constructed -- it can be in the same class.

Let's say Main is in SomeClassB, which derives from SomeClassA. SomeClassA has
two methods - SomeStaticMethod and SomeNonStaticMethod. It is perfectly legal to do:

public class SomeClassB : SomeClassA
{

public SomeClassB() : base() { }

public static void main(blah blah)
{
SomeStaticMethod();

// This is where it falls apart:
SomeNonStaticMethod();

// To call it non-statically, you need to do:
SomeClassB obj = new SomeClassB();
obj.SomeNonStaticMethod();
}

}

Chris.

Jun 27 '08 #8

P: n/a
I understand.
Thanks for your help.
Bill

"Chris Shepherd" <ch**@nospam.chsh.cawrote in message
news:%2******************@TK2MSFTNGP02.phx.gbl...
BillE wrote:
>Thank you.
So everything called from static void Main() must either be static, or an
instance member of another class? Even if the method is in the same
class as staic void Main(), it must still be static, apparently (since I
get an error otherwise).

No. Everything called from Main must be static or an instance member of an
object already constructed -- it can be in the same class.

Let's say Main is in SomeClassB, which derives from SomeClassA. SomeClassA
has two methods - SomeStaticMethod and SomeNonStaticMethod. It is
perfectly legal to do:

public class SomeClassB : SomeClassA
{

public SomeClassB() : base() { }

public static void main(blah blah)
{
SomeStaticMethod();

// This is where it falls apart:
SomeNonStaticMethod();

// To call it non-statically, you need to do:
SomeClassB obj = new SomeClassB();
obj.SomeNonStaticMethod();
}

}

Chris.

Jun 27 '08 #9

P: n/a
Bill... I would suggest sticking to instance methods and fields at the
start, in
other words, using object programming. Once you get your feet wet, you
can
learn about when to use static methods and fields. IMHO, the problem
with static
methods and fields is that they hark back to the days of global methods
and
fields.

Regards,
Jeff

*** Sent via Developersdex http://www.developersdex.com ***
Jun 27 '08 #10

This discussion thread is closed

Replies have been disabled for this discussion.