473,748 Members | 2,672 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 6654
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.Class 2.Do()' because it hides inherited member
'StaticDo.Class 1.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******* *******@TK2MSFT NGP11.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
2209
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 allocated on the stack but that's not a real reason. Which is OK because this isn't a real question, it's just a complaint dressed up to look like a reason Second question. If a language doesn't support a fairly obvious feature, one has to wonder if...
33
3348
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 near-impossible. It seems like it would be useful. In fact, there's a place in my code that I could make good use of it. So why not? Chris
3
2088
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 copy of data as opposed to multiple instances of the class) C# is now applying the static concept to methods. Why?, I thought that only one copy of the methods were loaded into memory not matter how many instances were created. Is this different...
4
8767
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: ------------------- public class Parent { public enum AllowedColors = {Red, Blue, Green} public static void DoSomething() {
4
1580
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 make? For that matter, why must you declare methods with the static keyword? Thanks for any ideas Ant
2
1419
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 my app, which basically hold key functionality. For example I have an Asset class. In there I have some methods for getting various attributes of an asset. For example I have this: public class Asset {
5
3085
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 not valid for this item. Why? Are static classes not allowed??
4
3135
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. References to generic type ArrayList<E> should be parameterized" at this statement arlist.add(temp); 2. how do I change this "Cannot make a static reference to the non-static field empid" for(String idx : empid)
1
3094
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 code where the problem is occurring. public class Employee { static ArrayList<Employee> arlist; int empid = arlist.size(); /** * displayMatch inputs a keyword from the user. It then iterates through the
0
9561
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9332
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9254
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6799
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6078
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4608
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4879
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3316
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2217
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.