473,406 Members | 2,705 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

newbie inheritance question

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
9 1260
"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
"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
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
"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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: KK | last post by:
Windows Forms Inheritance, Incomplete? I was playing around with Windows Forms and found out this Forms Inheritance feature. The moment I saw that, I felt this can be used effectively if the...
2
by: KK | last post by:
** Posting it here cause after couple of days no body responded.** I was playing around with Windows Forms and found out this Forms Inheritance feature. The moment I saw that, I felt this can...
8
by: __PPS__ | last post by:
Hello everybody, today I had another quiz question "if class X is privately derived from base class Y what is the scope of the public, protected, private members of Y will be in class X" By...
22
by: Matthew Louden | last post by:
I want to know why C# doesnt support multiple inheritance? But why we can inherit multiple interfaces instead? I know this is the rule, but I dont understand why. Can anyone give me some concrete...
45
by: Ben Blank | last post by:
I'm writing a family of classes which all inherit most of their methods and code (including constructors) from a single base class. When attempting to instance one of the derived classes using...
6
by: VR | last post by:
Hi, I read about Master Pages in ASP.Net 2.0 and after implementing some WinForms Visual Inheritance I tryed it with WebForms (let's say .aspx pages, my MasterPage does not have a form tag itself...
5
by: Noah Roberts | last post by:
Is there anything that says that if you virtually inherit from one class you have to virtually inherit from anything you inherit from?
3
by: RSH | last post by:
I have a simple question regarding inheritance in a web form. I have a DropDownList in an aspx form. It is called DropDownList1 I have a class that will be overriding the render event so I...
8
by: RSH | last post by:
Hi, I am working on some general OOP constructs and I was wondering if I could get some guidance. I have an instance where I have a Base Abstract Class, and 4 Derived classes. I now need to...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.