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

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 1717
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.Reflection.MethodBase.GetCurrentMethod()
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
by: Dumitru Sipos | last post by:
Hello everybody! is there possible to have a function that is both static and virtual? Dumi.
3
by: Danny Din | last post by:
Hi, I have the following code – public class Class1 { public Class1()
17
by: Tom | last post by:
This is not intuitivelly clear.
2
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...
4
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 =...
14
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...
5
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...
15
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
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
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
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: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
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: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
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: 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...

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.