473,761 Members | 10,365 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

static virtual/override

Don't know if there are any Delphi/Object Pascal converts
here, but being one, I sorely miss the feature that permits
class (e.g., static) methods to be virtual and be overridden
in derived classes.

Related to that, given:

public class Foo
{
static void SomeMethod()
{
// How can I tell what derived class this
// static method was called through?
}
}

public class Bar : Foo
{
/...
}

Bar ABar = new Bar();
ABar.SomeMethod ();

In the above, I want the method implementation
in Foo() to determine the class it was called through
(in this case, Bar).

Any suggestions are welcome
--
Tony M.
Nov 17 '05 #1
2 1740
Tony Maresca wrote:
Don't know if there are any Delphi/Object Pascal converts
here,
I see quite a few, actually.
I sorely miss the feature that permits
class (e.g., static) methods to be virtual and be overridden
in derived classes.
Why? Just curious.
public class Foo
{
static void SomeMethod()
{
}
}

public class Bar : Foo
{
}

Bar ABar = new Bar();
ABar.SomeMethod ();
This won't compile ... you want Bar.SomeMethod( ) ....
In the above, I want the method implementation
in Foo() to determine the class it was called through
(in this case, Bar).


I'm drawing a blank. System.Reflecti on.MethodBase.G etCurrentMethod ()
returns the current method, but when you call Foo.SomeMethod as
Bar.SomeMethod( ), the MethodBase shows Foo for both the DeclaringType
and the ReflectedType.

Does it HAVE to be a static method?

--

www.midnightbeach.com
Nov 17 '05 #2
Tony Maresca wrote:
Don't know if there are any Delphi/Object Pascal converts
here, but being one, I sorely miss the feature that permits
class (e.g., static) methods to be virtual and be overridden
in derived classes.
This is not possible in C#. From the MSDN docs at
http://shrinkster.com/7ir: You cannot override a non-virtual or static
method. The overridden base method must be virtual, abstract, or override.
Related to that, given:

public class Foo
{
static void SomeMethod()
{
// How can I tell what derived class this
// static method was called through?
}
}

public class Bar : Foo
{
/...
}

Bar ABar = new Bar();
ABar.SomeMethod ();

In the above, I want the method implementation
in Foo() to determine the class it was called through
(in this case, Bar).


This doesn't make any sense to me in this exact syntax, because you
can't even call the static method on the instance of Bar. You could call
Bar.SomeMethod( ), which is really the same thing as calling
Foo.SomeMethod( ).

You can't find out in Foo that the static method was called via the
class Bar - in fact, the code that is generated for a call to
Bar.SomeMethod( ) is exactly the same as that generated for a call to
Foo.SomeMethod( ). Try compiling a test program where you call the Foo
static method via the derived class and decompile the program in
Reflector. You'll see that the disassembled code shows a call to the
static method via the base class, not the derived class.

So, as you were asking for suggestions: tell us more about the purpose
of the class structure you are trying to create and we might be able to
suggest alternatives.

Oliver Sturm
--
omnibus ex nihilo ducendis sufficit unum
Spaces inserted to prevent google email destruction:
MSN oliver @ sturmnet.org Jabber sturm @ amessage.de
ICQ 27142619 http://www.sturmnet.org/blog
Nov 17 '05 #3

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

Similar topics

6
5834
by: Dumitru Sipos | last post by:
Hello everybody! is there possible to have a function that is both static and virtual? Dumi.
3
6655
by: Danny Din | last post by:
Hi, I have the following code – public class Class1 { public Class1()
17
51467
by: Tom | last post by:
This is not intuitivelly clear.
2
9679
by: Pavils Jurjans | last post by:
Hello, I wanted to get some feedback on why there are not allowed virtual static members in C#. Say, I have the Parent class, that hosts number of protected methods, that occasionaly make calls to the virtual methods. When I create the Child class, inheritting from the Parent class, and declare all of the Parent's virtual methods with 'override', so that when these methods are called from within the Parent protected methods, they...
4
2299
by: shooter | last post by:
How does one make a function in a base class slect the appopriate version of an overriden static variable. Code-splination follows: public class letter { public static int PostCodeDigits = 4; public void grow()
14
33286
by: Dave Booker | last post by:
It looks like the language is trying to prevent me from doing this sort of thing. Nevertheless, the following compiles, and I'd like to know why it doesn't work the way it should: public class ComponentA { static string s_name = "I am the root class."; public string Name { get {return s_name;} } }
5
8864
by: none | last post by:
I'd like to create a new static property in a class "hiding" the property present in a base class. Since this needs to happen at runtime I tried doing this via DynamicMethod. But obviously the created methods are not "registered" and only available through the DynamicMethod class. So a method lookup finds the origin property. A little test: public class DerivedClass : BaseClass {
15
3504
by: Philipp | last post by:
Hello I don't exactly understand why there are no static virtual functions. I would have liked something like this: class Base{ static virtual std::string getName(){ return "Base"; } }
8
2888
by: crjjrc | last post by:
Hi, I've got a base class and some derived classes that look something like this: class Base { public: int getType() { return type; } private: static const int type = 0; };
0
9531
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9345
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10115
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...
0
9957
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9905
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
9775
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
7332
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
6609
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
5373
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.