Click here to Skip to main content
15,892,965 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello everybody,

I need to make necessitate a class for implement an enum type, what should I do?
I also tried an abstract class and interfac, but its has error
C#
    public abstract class AbstractUserControl
    {
// Error: The modifier 'abstract' is not valid for this item

        public abstract enum MyEnum
        {
            
        }
        public abstract UserControl GetUserControl();
        public abstract void Disposing();
    }

    interface IUserControlFactory
    {
//Error: 'MyEnum': interfaces cannot declare types
        enum MyEnum
        {
            
        }
    }


Any hlp pls...

Thank you in advance.
Posted

Have a look at similar Question-Answer discussions below.

C# enum in interface/base class?

How can I make an abstract enum in a .NET class library?
 
Share this answer
 
v2
Comments
hzawary 26-Nov-11 11:13am    
Very good, but its dirty answer:)
RaisKazi 26-Nov-11 11:21am    
Its not dirty, its reality. :)
An interface can only contain the signatures of methods, delegates, events or indexers. You cannot declare an enum or any other type in an interface - you have to make the declaration outside the interface. That is the language definition I'm afraid!

MDSN interface[^]
 
Share this answer
 
Comments
hzawary 26-Nov-11 10:51am    
Yes I know, but actually I want tell to derived class that its should use an enum type in itself!
OriginalGriff 26-Nov-11 11:01am    
Sorry - you can't! :laugh:
hzawary 26-Nov-11 11:04am    
So, Is there any way to do this?
OriginalGriff 26-Nov-11 11:22am    
Not really, in the way you would want to - RaisKazi does give a link to a sort-of route, but it's a pretty nasty solution to a silly problem. I would be tempted to give one of the mandatory interface methods an enum parameter so they must at least look at them and leave it at that.
You should put your enum outside the interface.
C#
interface IUserControlFactory
{
        
}
enum MyEnum
{

}

Refer link below,
http://social.msdn.microsoft.com/forums/en-US/csharpgeneral/thread/14beb8d1-63d9-42f7-b036-eb9ebb106d08[^]
 
Share this answer
 
Comments
hzawary 26-Nov-11 10:50am    
Yes I know, but actually I want tell to derived class that its should use an enum type in itself!
Nikil S 26-Nov-11 11:59am    
I am afraid, you can't do that.
Ok! So I've implemented this code in the my class for solve the my problem
C#
public class AssisTouchScreen : AbstractUserControl
{
    enum userControlNames : byte
    {
        ClassicUC,
        TouchScreenUC,
    }

    UserControl _UserControl;
    string[] userControlStrings;

    public AssisTouchScreen()
    {
        userControlStrings = new string[]
            {
                userControlNames.ClassicUC.ToString(),
                userControlNames.TouchScreenUC.ToString()
            };
    }

    public override UserControl GetUserControl(string userControlName)
    {
        Disposing();
        switch (userControlName)
        {
            case "ClassicUC":
                _UserControl = new ClassicUC();
                break;
            case "TouchScreenUC":
                _UserControl = new TouchScreenUC();
                break;
            default:
                _UserControl = new TouchScreenUC();
                break;
        }

        return _UserControl;
    }

    public override string[] GetUserControlNames
    {
        get { return userControlStrings; }
    }

    protected override void Disposing()
    {
        _UserControl = null;
    }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900