newbie inheritance question

B

BillE

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
 
C

Chris Shepherd

BillE said:
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.
 
C

Ciaran O''Donnell

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
 
B

BillE

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).
 
C

Chris Shepherd

BillE said:
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.
 
B

BillE

I understand.
Thanks for your help.
Bill

Chris Shepherd said:
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.
 
J

Jeff Louie

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top