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

Overriding static members

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() {
}
}

public class Child : Parent{
public new enum AllowedColors = {Black, Blue, Yellow}
public new static void DoSomething() {
}
}
-------------------

Is it allowed in Csharp?

Regards,

-
Aamir
Nov 16 '05 #1
4 8739
Yes, of course; Your code sample is allowed in C#. Although a static member
in C# can't be marked as override, virtual or abstract, it is possible to
hide a base class static method in a derived class by using the keyword new.
The following example might help for a better understanding.

class MyBase{ public static int x = 25; public static void Method()
{ Console.WriteLine("Base static method"); }}class MyDerived :
MyBase{ public new static int x = 50; public new static void Method()
{ Console.WriteLine("Derived static method"); }}class
MyClient{ public static void Main() { MyDerived.Method(); //
Displays 'Derived static method' Console.WriteLine(MyClass.x);//
Displays 50 }}
- Devi J V [MSFT]--
This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm
"Aamir Mahmood" <a@b.c> wrote in message
news:OP**************@TK2MSFTNGP14.phx.gbl...
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() {
}
}

public class Child : Parent{
public new enum AllowedColors = {Black, Blue, Yellow}
public new static void DoSomething() {
}
}
-------------------

Is it allowed in Csharp?

Regards,

-
Aamir

Nov 16 '05 #2
Hi,
You cannot make them virtual.

You can use the code you posted though.

Now depending of what you want to do the code work or not, remember that the
statics method can be called only from the type itself, not an instance.
This mean that there is no way for a client to confusion or to dynamically
select which one to call.

The only purpose I can think of right now of having such a construction is
to allow a derived type to have a method with the same signature.

cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"Aamir Mahmood" <a@b.c> wrote in message
news:OP**************@TK2MSFTNGP14.phx.gbl...
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() {
}
}

public class Child : Parent{
public new enum AllowedColors = {Black, Blue, Yellow}
public new static void DoSomething() {
}
}
-------------------

Is it allowed in Csharp?

Regards,

-
Aamir

Nov 16 '05 #3
At first I was skeptical when I read this, but I tried it. The C#
compiler does indeed require the keyword "new" on static a method in a
derived class that have the same name signature as a static method in
the base class.

This makes absolutely no sense to me. Does anyone know why C# would
require the keyword "new" here? It seems useless to me.

Static methods are associated with classes, not instances, and are
called by using the class name, not an instance variable. There is no
polymorphism involved at all.

So what good does the "new" keyword do, when you already know that in
order to call the method you have to give the class name, so there can
be no confusion?

Nov 16 '05 #4
I guess "new" is required to give the subclass *author* a sense that he's
doing something wrong. Or better, something not desirable.

"Bruce Wood" <br*******@canada.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
At first I was skeptical when I read this, but I tried it. The C#
compiler does indeed require the keyword "new" on static a method in a
derived class that have the same name signature as a static method in
the base class.

This makes absolutely no sense to me. Does anyone know why C# would
require the keyword "new" here? It seems useless to me.

Static methods are associated with classes, not instances, and are
called by using the class name, not an instance variable. There is no
polymorphism involved at all.

So what good does the "new" keyword do, when you already know that in
order to call the method you have to give the class name, so there can
be no confusion?

Nov 16 '05 #5

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

Similar topics

3
by: Philippe Guglielmetti | last post by:
Look at these few lines of code: class A { public: virtual void f() { cout << "A";}}; class B : public A{public: static void f() { cout << "B"; }}; class C : public B{public: void f() { cout <<...
3
by: Cheng Mo | last post by:
When overriding operator new & delte of one class, the method is implicitly declared as static. However, overriding operator new & delete of template cannot be static.The compiler says cannot...
3
by: mpatnam | last post by:
I have an executable which links to a static library (.a). I want to provide a hook by overriding a function part of this static library. Eg: I have a function "int blkstart(int i)" in this static...
4
by: Rafael Veronezi | last post by:
I have some questions about override in inheritance, and virtual members. I know that you can you override a method by two ways in C#, one, is overriding with the new keyword, like: public new...
4
by: Nilesh | last post by:
I am confused about the purpose of 'new' in overriding. Consider following example. <code_snippet> using console = System.Console; public class TestClass { public static void Main() {
1
by: Joel | last post by:
Why does this work: using System; namespace ConsoleApplication1 { class Class1 { static void Main(string args)
6
by: rlvladbob | last post by:
Hi, I've try to access a static method using an instance instead of a class. public class test{ public static void ShowAText(string ThisText) { System.Console.WriteLine("->{0}",ThisText); }
11
by: Kevin Prichard | last post by:
Hi all, I've recently been following the object-oriented techiques discussed here and have been testing them for use in a web application. There is problem that I'd like to discuss with you...
10
by: Sebastian | last post by:
Hi, I'm confronted with a problem that seems not to be solvable. In general: How can I override an interface member of my base class and call the overridden method from my derived class? This...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
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.