473,325 Members | 2,308 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,325 software developers and data experts.

static methods and the 'new' keyword

Hi,

I have the following code –

public class Class1

{

public Class1()

{

}
public static void Do ()

{

}

}
public class Class2 : Class1

{

public Class2()

{

}
public static void Do ()

{

}

}
The compiler warns me that the Do static method in Class2 must have the new
keyword. It also adds one implicitly.
I'm trying to figure out the reason for that. These are class methods, not
instance methods, virtual/override are not an option, why is new needed?

Thanks,
Danny
Nov 15 '05 #1
3 6611
Danny Din wrote:

I'm trying to figure out the reason for that. These are class methods, not
instance methods, virtual/override are not an option, why is new needed?


Same reason it's needed any other time: you have a method with the exact
same signature in the base class.

--
There are 10 kinds of people. Those who understand binary and those who
don't.

http://code.acadx.com
(Pull the pin to reply)
Nov 15 '05 #2
"Danny Din" <yo****@hotmail.com> wrote:

<snip>
The compiler warns me that the Do static method in Class2 must have the new
keyword. It also adds one implicitly.
I'm trying to figure out the reason for that. These are class methods, not
instance methods, virtual/override are not an option, why is new needed?

Thanks,
Danny

The full warning message gives you a hint:

class1.cs(16,28): warning CS0108: The keyword new is required on 'StaticDo.Class2.Do()' because it hides inherited member
'StaticDo.Class1.Do()'

i.e.

public class Class1 {
public Class1() {}
public static void Do () {}
}
public class Class2 : Class1 {
public Class2() {}
// use new key word in the method declaration
public static new void Do () {}
}

in essence the compiler is requesting that with the "new" keyword you tell it that you are aware that you will be hiding the Class1
version of Do() in Class2 - i.e. "yes - go ahead hide the ancestor version - I know what I'm doing".
Usually when you try to override a method in its descendent the ancestor method needs to the marked as "virtual" while the deriving
method is marked as "override" e.g.:

public class Class1 {
public virtual void Do () {}
}
public class Class2 : Class1 {
public override void Do () {}
}

As you have discovered "virtual" and "override" are restricted to instance methods.

http://msdn.microsoft.com/library/en...fVirtualPG.asp
http://msdn.microsoft.com/library/en...OverridePG.asp
http://msdn.microsoft.com/library/en...rfStaticPG.asp

By declaring the methods "static" the Do() methods become class methods, i.e. the method does not act on any particular instance
created by the class.

I suspect that the C# equivalent of the "virtual function pointer table (vtable)" is stored in the class instance (object), not the
class, which would explain why class methods cannot use "virtual" and "override".

So why are you trying redefine a class function? Polymorphism applies to instances (objects), not classes.

As you can have to access the class method only through the class anyway give the methods different names - that way the Class2.Do()
will not hide Class1.Do().


Nov 15 '05 #3
Explicit programming is good. This way the compiler helps you to make
sure you know what you're doing..

"Danny Din" <yo****@hotmail.com> wrote in message news:<e0**************@TK2MSFTNGP11.phx.gbl>...
Hi,

I have the following code ?

public class Class1

{

public Class1()

{

}
public static void Do ()

{

}

}
public class Class2 : Class1

{

public Class2()

{

}
public static void Do ()

{

}

}
The compiler warns me that the Do static method in Class2 must have the new
keyword. It also adds one implicitly.
I'm trying to figure out the reason for that. These are class methods, not
instance methods, virtual/override are not an option, why is new needed?

Thanks,
Danny

Nov 15 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: baylor | last post by:
In C#, an interface cannot mark any method as static. i'm told the ILASM supports it but i've never tested that Two questions. First, why? OK, i've heard the reason about interfaces being...
33
by: Chris Capel | last post by:
What is the rationale behind the decision not to allow abstract static class members? It doesn't seem like it's a logically contradictory concept, or that the implementation would be difficult or...
3
by: Jay | last post by:
Why are there static methods in C#. In C++ static was applied to data only (I believe) and it meant that the static piece of data was not a part of the object but only a part of the class (one...
4
by: Aamir Mahmood | last post by:
Hi all, I have a question. Can static members (methods and nested types) be made virtual in a class? So that they can be overridden in the child classes. For example: -------------------...
4
by: Ant | last post by:
Hi I'm quite new to C#. I've been playing around with variables declared outside the scope of a method with & without the static keyword. What difference does declaring variables in either fashion...
2
by: Steve | last post by:
C# I am new to C#, and new to the whole OO concepts, and so I am sure this is a very stupid question, but I'll ask anyway. When should my methods be static? I have several "Business Objects" in...
5
by: Tina | last post by:
I'm using C# 1.1.............. I add new Class item to my project. It's created as class Math I change it to public static class Math I get an error saying that "The modifier 'static' is...
4
nomad
by: nomad | last post by:
Hello Everyone... I have rewrite my code and I ran into some problems. I have two problems. 1. What does this mean "Type safety: The method add(Object) belongs to the raw type ArrayList....
1
by: sandyw | last post by:
I having trouble with my project and need a little help please "The method inputEmployee cannot be declared static; static methods can only be declared in a static or top level type" Part of my...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.