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

Overriding a enumerator

Hi,

Say in the base class they have a vitual method that Gets Days.
Well, in the base class, only Monday through Friday are defined.
Now, I have to extend the days to include Saturday and Sunday.

Expand|Select|Wrap|Line Numbers
  1. public BaseClass
  2. {
  3.     public enum Days
  4.    {
  5.       Monday,
  6.       Tuesday,
  7.       Wednesday,
  8.       Thursday,
  9.       Friday,
  10.    }
  11.     public virtual Days_Enum GetDays(int i)
  12.     {
  13.           //Method to return enumerator i.e. return Days.Monday;
  14.     }
  15.  
  16. Class Parent : BaseClass
  17. {
  18.  
  19.     public override Days_Enum GetDays(int i)
  20.     {
  21.            //Method to return days, including weekend . i.e. Days.Saturday
  22.     }
  23. }
  24.  
TIA,
Jan 28 '08 #1
8 1872
r035198x
13,262 8TB
Hi,

Say in the base class they have a vitual method that Gets Days.
Well, in the base class, only Monday through Friday are defined.
Now, I have to extend the days to include Saturday and Sunday.

<CODE>
public BaseClass
{
public enum Days
{
Monday,
Tuesday,
Wednesday,
Thursday,
Friday,
}
public virtual Days_Enum GetDays(int i)
{
//Method to return enumerator i.e. return Days.Monday;
}
}

Class Parent : BaseClass
{

public override Days_Enum GetDays(int i)
{
//Method to return days, including weekend . i.e. Days.Saturday
}
}
</CODE>

TIA,
So what have you done so far? What is the current implementation like and what conditions determine which day will be returned?
Here's an extract that should help you along ...
Straight from the C# language spec


Each enum type has a corresponding integral type called the underlying type of the enum type. An enum type that does not explicitly declare an underlying type has an underlying type of int. An enum type’s storage format and range of possible values are determined by its underlying type. The set of values that an enum type can take on is not limited by its enum members. In particular, any value of the underlying type of an enum can be cast to the enum type and is a distinct valid value of that enum type.
Jan 28 '08 #2
So what have you done so far? What is the current implementation like and what conditions determine which day will be returned?
Here's an extract that should help you along ...
Straight from the C# language spec

That is not important and irrelevant.
What is important is how to override the enumerator.
I did stumble upon using the 'new' keyword, but that doesn't seem to work properly. I even tried adding Parent.Weekday to no avail.
Here is some executable code that doesn't work.



Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4.  
  5. namespace Enum3
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.               clsBaseClass First = new clsBaseClass();
  12.               clsNewClass Second = new clsNewClass();
  13.               First.ShowEnum(clsBaseClass.Weekdays.Fri);
  14.               Second.ShowEnum(clsNewClass.Weekdays.Fri);
  15.         }
  16.  
  17.         public class clsBaseClass
  18.         {
  19.             //  Define the enum type
  20.             public enum Weekdays
  21.             {
  22.                 Sun, Mon, Tues, Wed, Thurs, Fri, Sat
  23.             }
  24.  
  25.             public virtual void ShowEnum(Weekdays days)
  26.             {
  27.                 Console.WriteLine("Day Value {0}", days.ToString());
  28.             }
  29.  
  30.         }
  31.         class clsNewClass : clsBaseClass
  32.         {
  33.             public new enum Weekdays
  34.             {
  35.                 Sun = 1, Mon, Tues, Wed, Thurs, Fri, Sat
  36.             }
  37.             public override void ShowEnum(Weekdays days)
  38.             {
  39.                 Console.WriteLine("Day Value {0}", days.ToString());
  40.             }
  41.         }
  42.     }
  43. }
  44.  
Error 1 'Enum3.Program.clsNewClass.ShowEnum(Enum3.Program. clsNewClass.Weekdays)': no suitable method found to override C:\Documents and Settings\CSharpProgrammer\Local Settings\Application Data\Temporary Projects\Enum3\Program.cs 37 34 Enum3



Thanks,
Jan 28 '08 #3
r035198x
13,262 8TB
That is not important and irrelevant.
....
The point I was trying to make is that values of the enumeration type are not restricted to the values of the named constants.
Jan 28 '08 #4
The point I was trying to make is that values of the enumeration type are not restricted to the values of the named constants.
Thanks, but I knew that.
I was just trying not really show too much detail and distract from the main content.

Oh well.
Thanks anyways,
Jan 28 '08 #5
Plater
7,872 Expert 4TB
EDIT:
ok I reproduced it.

The error is because your override function is incorrect.
To "override" a function, you need to use an identical declaration, which for the derrived class would be:
Expand|Select|Wrap|Line Numbers
  1. public override void ShowEnum(clsBaseClass.Weekdays days)
  2. {
  3. //...
  4. }
  5.  
Jan 28 '08 #6
EDIT:
ok I reproduced it.

The error is because your override function is incorrect.
To "override" a function, you need to use an identical declaration, which for the derrived class would be:
Expand|Select|Wrap|Line Numbers
  1. public override void ShowEnum(clsBaseClass.Weekdays days)
  2. {
  3. //...
  4. }
  5.  
Well, they are in fact identical. I guess it is must the signature that makes them different.
So, I suppose that means there is no way to override or add Enumerators without modifying the base class.

I wonder what would be a better way do that, without using numerators?

I just want to add more enumerators like Saturday and Sunday to an already existing Enumerator without having to modify the base class.
Jan 28 '08 #7
Plater
7,872 Expert 4TB
They are NOT identical.
In your code:
The base class signature is using the base class's enum.
The derived class signature is using the derived class's enum.

That is different.
Jan 28 '08 #8
r035198x
13,262 8TB
Well, they are in fact identical. I guess it is must the signature that makes them different.
So, I suppose that means there is no way to override or add Enumerators without modifying the base class.

I wonder what would be a better way do that, without using numerators?

I just want to add more enumerators like Saturday and Sunday to an already existing Enumerator without having to modify the base class.
You cannot extend enums. You can inherit them from base classes however. If you inherit an enum from a base class you take it as it was. You can, however, use it differentlty in the inheriting class. i.e you can make it use values that are not being used in the base class(which is what was being explained in the specs above).
Jan 29 '08 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Gordon Rundle | last post by:
It drives me nuts that I can't use foreach with an enumerator instance. I would like the following to be functionally identical: foreach (Object o in MyCollection) ... foreach (Object o in...
7
by: tony collier | last post by:
if i have a string eg string day="monday"; and an enumerator enum days {monday=0,tuesday, wednesday}
2
by: Stephanie Stowe | last post by:
Hi. I am trying to understand the weird System.DirectoryServices object model. I have a DirectoryEntry object. I want to enumerate through the PropertyCollection. So I looked at GetEnumerator....
11
by: rawCoder | last post by:
Hi, Which is better in terms of performance. Iterating over Enumerator ForNext loop (using indexer) ForEach loop Thanx rawCoder
5
by: toldyouso | last post by:
The following script works in IE but in Firefox errors with the msg Enumerator is not defined. <html> <head> <meta name="vs_defaultClientScript" content="JavaScript"> <script...
3
by: ajmastrean | last post by:
I have a bunch of enumerators. I need to pass them into a function that is designed to write all values of any enumerator to the console. For instance... public enum Days : int { Sunday = 1,...
7
by: csharpula csharp | last post by:
Hello, I have methods which refer to some class enumerator .Should I make it pulic or encapsulate it? *** Sent via Developersdex http://www.developersdex.com ***
6
by: Michael C | last post by:
I'm reading about Iterators and the article I'm reading states that an enumerator must load all of the objects into memory, which is obviously a big waste if there are a large number of objects and...
3
by: Dave | last post by:
I'm calling string.Split() producing output string. I need direct access to its enumerator, but would greatly prefer an enumerator strings and not object types (as my parsing is unsafe casting...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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
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...
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,...
0
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...

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.