Problem to override a Property

  • Thread starter Thread starter Thomas Kehl
  • Start date Start date
T

Thomas Kehl

Hello.
I provided a class, which derives from the class DataView.
The only thing, which I would like to do in this class am to overwrite the
Property COUNT.
I wars it however simply not. I get always following message from the
compiler, although COUNT of the DataView (according to MSDN) is virtual.
the element ' System.Data.DataView.Count.get ' cannot be overwritten,
because as virtually, abstract or do not overwrite it is marked
Can someone help me?
Thanks Thomas
 
Thomas Kehl said:
I provided a class, which derives from the class DataView.
The only thing, which I would like to do in this class am to overwrite the
Property COUNT.
I wars it however simply not. I get always following message from the
compiler, although COUNT of the DataView (according to MSDN) is virtual.

I believe this is a bug in the documentation. If you look at the type
with ILDASM or Reflector, you'll see it's not actually virtual.
 
Hi Jon,
when I look into System.Data with the Objectbrowser, I see, that the
definition of the Property is the fallowing:

public virtual new int Count [ get]

I think, I should be able to override it, should'nt I?

regards
thomas
 
Thomas Kehl said:
when I look into System.Data with the Objectbrowser, I see, that the
definition of the Property is the fallowing:

public virtual new int Count [ get]

I think, I should be able to override it, should'nt I?

I think it's taking that from the documentation rather than from the
assembly itself - it's certainly *not* virtual. The IL in ILDASM shows

..method public hidebysig newslot specialname virtual final
instance int32 get_Count() cil managed

and although "virtual" appears there, so does "final". It's all a bit
confusing, but I'm sure the upshot is that it's not virtual, I'm afraid
:(
 
The short answer is that MSDN and the object browser are wrong: the
property is not virtual.

The longer answer is that the DataView.Count property is non-virtual, and
is part of the implementation of the IComponent interface
(IComponent.Count). C# allows non-virtual interface implemenations, but
the CLR requires that all methods which implement interfaces be virtual.
So the compiler emits this in metadata as "newslot virtual final." So it
has the virtual flag set in metadata, but is final so that you cannot
override it.

-Mark


--
Mark Andersen, Visual C# Team
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


--------------------
From: Jon Skeet <[email protected]>
Subject: Re: Problem to override a Property
Date: Thu, 25 Sep 2003 19:28:25 +0100
Message-ID: <[email protected]>
References: <udCWu#[email protected]>
MIME-Version: 1.0
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
X-Newsreader: MicroPlanet Gravity v2.60
Newsgroups: microsoft.public.dotnet.languages.csharp
NNTP-Posting-Host: cpc3-rdng5-6-0-cust143.winn.cable.ntl.com 81.103.154.143
Lines: 1
Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP10.phx.gbl
Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.csharp:187372
X-Tomcat-NG: microsoft.public.dotnet.languages.csharp

Thomas Kehl said:
when I look into System.Data with the Objectbrowser, I see, that the
definition of the Property is the fallowing:

public virtual new int Count [ get]

I think, I should be able to override it, should'nt I?

I think it's taking that from the documentation rather than from the
assembly itself - it's certainly *not* virtual. The IL in ILDASM shows

.method public hidebysig newslot specialname virtual final
instance int32 get_Count() cil managed

and although "virtual" appears there, so does "final". It's all a bit
confusing, but I'm sure the upshot is that it's not virtual, I'm afraid
:(
 
Mark Andersen said:
The short answer is that MSDN and the object browser are wrong: the
property is not virtual.

The longer answer is that the DataView.Count property is non-virtual, and
is part of the implementation of the IComponent interface
(IComponent.Count). C# allows non-virtual interface implemenations, but
the CLR requires that all methods which implement interfaces be virtual.
So the compiler emits this in metadata as "newslot virtual final." So it
has the virtual flag set in metadata, but is final so that you cannot
override it.

So C# does not allow a method to be declared "virtual sealed", but will
generate the equivalent in IL for methods that are not declared virtual.
Interesting :-)
 
Mike Schilling said:
So C# does not allow a method to be declared "virtual sealed", but will
generate the equivalent in IL for methods that are not declared virtual.
Interesting :-)

Yes - as far as I can see, the CLR itself has some very odd terminology
in terms of "virtual" - it requires a method to be "virtual" to end up
being the resolution of a virtual call, even if it's not virtual in the
sense of being overridable (which is the sense the C# specification
uses). I don't think it's helping the discussion on what virtual means
:(
 
Jon Skeet said:
Yes - as far as I can see, the CLR itself has some very odd terminology
in terms of "virtual" - it requires a method to be "virtual" to end up
being the resolution of a virtual call, even if it's not virtual in the
sense of being overridable (which is the sense the C# specification
uses). I don't think it's helping the discussion on what virtual means
:(

If I had to guess (and I do, since I'm too lazy to look it up :-), I'd
suspect that to the CLR "virtual" means "occupies a slot in the virtual
function table". The fact that the only difference between "virtual" and
"override" methods in C# is that the former generates the "newslot" CLR
keyword encourages me in this belief.
 
Back
Top