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

Virtual static methods?

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 would use the code that's in the
Child class.
Now, why should I be not allowed to have similar pattern with static
methods - If I have an instance of the Child class, and I call this
protected metjhod whose code is defined in the Parent class, and this code
makes call to this static method, why can't I override the static method
that is called in this scenario, so that the code that resides in the Child
class is called?

Rgds,

Pavils
Nov 16 '05 #1
2 9669
Hi Pavlis,

This has been discussed in this ng several times. You can search google for
those discusion.
Anyways in order polymorphism to work you have to have isntance to an
object. With static method you don't have. This is platform issue. Some
languages (such as Delphi I believe) can do that because for each type there
meta type and there is single instance of it. .NET Type objects are not the
same

--

Stoitcho Goutsev (100) [C# MVP]
"Pavils Jurjans" <pa****@mailbox.riga.lv> wrote in message
news:eA**************@TK2MSFTNGP10.phx.gbl...
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 would use the code that's in the
Child class.
Now, why should I be not allowed to have similar pattern with static
methods - If I have an instance of the Child class, and I call this
protected metjhod whose code is defined in the Parent class, and this code
makes call to this static method, why can't I override the static method
that is called in this scenario, so that the code that resides in the Child class is called?

Rgds,

Pavils

Nov 16 '05 #2
Stoitcho Goutsev (100) [C# MVP] wrote:
Hi Pavlis,

This has been discussed in this ng several times. You can search google for
those discusion.
Anyways in order polymorphism to work you have to have isntance to an
object. With static method you don't have. This is platform issue. Some
languages (such as Delphi I believe) can do that because for each type there
meta type and there is single instance of it. .NET Type objects are not the
same


Delphi for .NET supports virtual class methods and metaclasses, but they
are implemented by the compiler with nested classes and other sneaky tricks.

Jesse
Nov 16 '05 #3

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

Similar topics

2
by: john smith | last post by:
I'm wondering if it's possible to declare a pure virtual member function? Ie is: class A{ public: virtual static void f() const = 0; }; legal? I'm getting compile errors for code that used...
12
by: cppaddict | last post by:
Hi, I know that it is illegal in C++ to have a static pure virtual method, but it seems something like this would be useful when the following 2 conditions hold: 1. You know that every one...
2
by: K | last post by:
Hi all; I don't think this is a VC compiler question, so I am asking this here. I did hunt around the web and Stroustrup before posting, but found no references to this problem. I have a base...
2
by: Tony Johansson | last post by:
Hello Experts! I know that you can't have virtual static methods and I know what a static method is. A static method exist only one time no matter how many object you have. You have this...
32
by: Adrian Herscu | last post by:
Hi all, In which circumstances it is appropriate to declare methods as non-virtual? Thanx, Adrian.
14
by: JPRoot | last post by:
Hi I use the following syntax to have events inherited from base to child classes which works nicely (virtual and override keyword on events). But I am wondering if it is a "supported" way of using...
2
by: Mark | last post by:
I know that you can't have a static virtual property, but is there a way to simulate the same results? I have a base class that I want to extend so that you can change a value and it inherits...
0
by: Peter Morris [Droopy eyes software] | last post by:
I have solved this problem but it involved a static class, reflection, and a lot of code. I wanted to post it here because the solution using virtual class methods takes only a few lines of code....
0
by: akshaycjoshi | last post by:
I am reading a book which says Even though unboxed value types don't have a type object pointer, you can still call virtual methods (such as Equals, GetHashCode, or ToString) inherited or...
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
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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,...
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
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,...
0
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...
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,...

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.