473,626 Members | 3,965 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Why can't I seal a single indexer?

I'm having a strange problem with sealing virtual indexers.

Looks like a compiler error to me - or have I overlooked
some obscure statement in the specs?

I have two virtual indexers in the same class, one of them
by string, the other by int.

The weird thing is, if I seal one of them, I can't override the
other. Here is the code.

--------------------------------------------------------------------

namespace FreakShow
{
class Program
{
static void Main(string[] args)
{
C c = new C();
System.Diagnost ics.Debug.Print (c["test"]);
System.Diagnost ics.Debug.Print (c[123]);
}
}

abstract class A
{
public abstract string this[int arg] { get; }
public abstract string this[string arg] { get; }
}

abstract class B : A
{
public /*sealed*/ override string this[string arg]
{
get { return "B:" + arg.ToUpper(); }
}
}

class C : B
{
public override string this[int arg]
{
get { return "C:" + (arg * 2); }
}
}
}
--------------------------------------------------------------------
As expected, the debug output shows:

B:TEST
C:246

Now, if I remove the comment marks around sealed, I get this
compiler error:

error CS0239: 'FreakShow.C.th is[int].get':
cannot override inherited member 'FreakShow.A.th is[int].get'
because it is sealed

I don't think I sealed it??? What's going on?

I'm using C# 2.0.
Jul 7 '06 #1
6 1940
Ole,

But that's exactly what you did on B! From the "C# Programmers
Reference":

The sealed modifier can be applied to classes, instance methods and
properties. A sealed class cannot be inherited. A sealed method overrides a
method in a base class, but itself cannot be overridden further in any
derived class. When applied to a method or property, the sealed modifier
must always be used with override (C# Reference).

You sealed the overriden indexer, and killed any ability that derived
classes had to override that inherited member.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Ole Nielsby" <ol*********@sn ailmail.dkwrote in message
news:uF******** ******@TK2MSFTN GP05.phx.gbl...
I'm having a strange problem with sealing virtual indexers.

Looks like a compiler error to me - or have I overlooked
some obscure statement in the specs?

I have two virtual indexers in the same class, one of them
by string, the other by int.

The weird thing is, if I seal one of them, I can't override the
other. Here is the code.

--------------------------------------------------------------------

namespace FreakShow
{
class Program
{
static void Main(string[] args)
{
C c = new C();
System.Diagnost ics.Debug.Print (c["test"]);
System.Diagnost ics.Debug.Print (c[123]);
}
}

abstract class A
{
public abstract string this[int arg] { get; }
public abstract string this[string arg] { get; }
}

abstract class B : A
{
public /*sealed*/ override string this[string arg]
{
get { return "B:" + arg.ToUpper(); }
}
}

class C : B
{
public override string this[int arg]
{
get { return "C:" + (arg * 2); }
}
}
}
--------------------------------------------------------------------
As expected, the debug output shows:

B:TEST
C:246

Now, if I remove the comment marks around sealed, I get this
compiler error:

error CS0239: 'FreakShow.C.th is[int].get':
cannot override inherited member 'FreakShow.A.th is[int].get'
because it is sealed

I don't think I sealed it??? What's going on?

I'm using C# 2.0.


Jul 7 '06 #2
Nicholas Paldino [.NET/C# MVP] wrote:
Ole,

But that's exactly what you did on B! From the "C# Programmers
Reference":

The sealed modifier can be applied to classes, instance methods and
properties. A sealed class cannot be inherited. A sealed method overrides
a method in a base class, but itself cannot be overridden further in any
derived class. When applied to a method or property, the sealed modifier
must always be used with override (C# Reference).

You sealed the overriden indexer, and killed any ability that derived
classes had to override that inherited member.

Hope this helps.

Hi Nicholas,

He sealed the indexer that expects a string parameter, not the indexer that
expects an integer parameter.

If you leave out the override in the class 'C' the compiler moans that the
abstract member has not been implemented. When you override the correct
member, the compiler moans that it's sealed.

--
Hope this helps,
Tom Spink
Jul 7 '06 #3
Looks like a compiler error to me - or have I overlooked
some obscure statement in the specs?
If the "obscure statement" is in the spec, I couldn't find it either - the
spec is long and complex though so I could have missed it. Perhaps this is
worth opening as a potential bug at the Microsoft product feedback center?

Mark
Jul 7 '06 #4
I wrote:
I have two virtual indexers in the same class, one of them
by string, the other by int.

The weird thing is, if I seal one of them, I can't override the
other.
(code example at the end of this post.)

Tom Spink <ts****@gmail.c omadded:
If you leave out the override in the class 'C' the compiler moans
that the abstract member has not been implemented. When you
override the correct member, the compiler moans that it's sealed.
I spent some time with the C# 1.2 specs and I'm now quite sure
this is an error.

(I use the C# 2.0 compiler but these topics are in the C 1.2 specs.)

In § 1.6.6.3:
Indexers can be overloaded, meaning that a class can declare
multiple indexers as long as the number or types of their
parameters differ.

§ 3.6 says the same, in more words.

Chapter 10 details the differences between methods, properties
and indexers and tells that get/set are treated toghether for a
particular signature, but apart from that, indexers are supposed
to be just like instance methods; nothing indicates you cannot
seal different-signatured indexers independently, and it makes
no sense at all that the sealed modifier should have another
granularity than that of the the override modifier.

So, it's a bug.

Perhaps not a catastrophic one, since I can have it work by
outcommenting the sealed modifier, as shown, and if I
really needed to expose it yet prevent overrides, I could
make it nonvirtual and implement it by a virtual method.

What bothers me most is, I like to use the sealed modifier
for refactoring.

In terms of the example: I found that I had done the same
override in classes C, C1, C2... (not presented here) and
decided to move it up to the B class. Sealing it is a neat way
of ensuring I got rid of all the downtown overrides.

(In the real scenario, the indexer signatures are like:
this[Q arg]
this[Q[] args]
Q being a base class of A, and this[Q arg] is declared in
class B, and is the same as this[Q[] args] with a single-element
array. The B class and its C subclasses only support
single-element indexes and are optimized for this by having
an indexer that allows to bypass the single-element array wrap.
I know the paramarray keyword could be used to get the
same syntax, but this would still imply a wasteful wrapping.)

Here comes the code again (copied from my first post).
--------------------------------------------------------------------

namespace FreakShow
{
class Program
{
static void Main(string[] args)
{
C c = new C();
System.Diagnost ics.Debug.Print (c["test"]);
System.Diagnost ics.Debug.Print (c[123]);
}
}

abstract class A
{
public abstract string this[int arg] { get; }
public abstract string this[string arg] { get; }
}

abstract class B : A
{
public /*sealed*/ override string this[string arg]
{
get { return "B:" + arg.ToUpper(); }
}
}

class C : B
{
public override string this[int arg]
{
get { return "C:" + (arg * 2); }
}
}
}

--------------------------------------------------------------------
As expected, the debug output shows:

B:TEST
C:246

Now, if I remove the comment marks around sealed, I get this
compiler error:

error CS0239: 'FreakShow.C.th is[int].get':
cannot override inherited member 'FreakShow.A.th is[int].get'
because it is sealed
Jul 7 '06 #5
Ole Nielsby wrote:
I wrote:
>I have two virtual indexers in the same class, one of them
by string, the other by int.

The weird thing is, if I seal one of them, I can't override the
other.

(code example at the end of this post.)

Tom Spink <ts****@gmail.c omadded:
>If you leave out the override in the class 'C' the compiler moans
that the abstract member has not been implemented. When you
override the correct member, the compiler moans that it's sealed.
<snippedy-doo-dah>

I have to agree, it is bizarre.

--
Hope this helps,
Tom Spink
Jul 7 '06 #6
Ole Nielsby wrote:
I wrote:
>I have two virtual indexers in the same class, one of them
by string, the other by int.

The weird thing is, if I seal one of them, I can't override the
other.

(code example at the end of this post.)
UPDATE: You may be interested to know that the Mono C# Compiler (GMCS) does
not have a problem with this. It compiles correctly, and produces a CLI
compliant assembly.

--
Hope this helps,
Tom Spink
Jul 7 '06 #7

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

Similar topics

1
2832
by: Dotnetjunky | last post by:
Hi all Is it correct that there's a link between indexer and Items property in C# ? If yes, please show me what is it ? thanks in advance. -- Dotnetjunky
1
1635
by: Iulian Ionescu | last post by:
I have the following problem. I derived a class from ArrayList to provide some additional events I need. Then I derived 2 different classes from this one and each of them defines an indexer that returns the specified object. For example, the list for Group types will have: public new Group this { get {return (Group)base;}
5
3761
by: Michel Walsh | last post by:
Hi, Looking for the syntax for a static indexer. For a non static 'access', the following would do: public class whatever { static Hashtable myHashtable = null;
3
4117
by: Sam Martin | last post by:
Basically, i've got a class called RegularExpressions which contains a private Hashtable of RegExps used in my apps. I obviously don't want to give full public access to the Hashtable, so I'd like to define a static indexer so that the following syntax is possible e.g. ns.RegularExpressions I've got a static method that does this, but i want to know if it's possible
7
4615
by: Steph | last post by:
I'm learning C#. I need to implement an indexer using an array list. I have the following code and I'm getting an error "Inconsistent accessibility: indexer return type CRegs is less accessible than indexer Whouse.this. Would someone please tell me why? public class Whouse : IEnumerable public CRegs this // Indexer { get
5
17661
by: rwoo_98 | last post by:
I am trying to pass a reference to a method --SomeMethod. This table resides in a dataset and I refer to the datatable in the dataset using an indexer. However when I attempt to do this I get the error message, "A property or indexer may not be passed as an out or ref parameter?" Here is the code SomeMethod(ref ds_User.Tables);
17
2078
by: SemSem | last post by:
i want to know waht is an index and how we use it with a simple example including the main of the program . thanx -- Islam Khalil,
32
9429
by: Michael C | last post by:
I want to inherit from Bitmap to add a property but I can't because it's sealed. Is there any reason to seal a class? Thanks, Michael
0
1004
by: raja | last post by:
Fault Seal Analysis Analyse Reservoir Fault, Lateral & Top Seal quickly and efficiently http://finance4u.synthasite.com/
0
8266
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
8199
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,...
1
8365
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
8505
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
6125
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
5574
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
4198
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2626
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1511
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.