473,238 Members | 1,823 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,238 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 1256
"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...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...

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.