Firedragon
Firedragon

Reputation: 3733

Adding constructor (or function) to enum

I am not sure if a constructor is exactly what I am looking for but if I explain what I am trying to do hopefully someone can tell me if I am trying to do is a silly idea or whether there are ways to do it.

So I have an enum:

public enum MessageType
{
    Normal, 
    Error, 
    Chat, 
    Groupchat, 
    Headline
}

This enum is basically a wrapper for the jabber.net MessageType. So I want to create my enum from this. So at the moment I have a function like this:

private MessageType ConvertMessageType(JabberMessageType jabberType)
{
    MessageType type = MessageType.Error;

    switch (jabberType)
    {
        case JabberMessageType.normal:
            type = MessageType.Normal;
            break;

        //etc
    }

    return type;
}

So I have to use enum MessageType type = ConvertMessageType(JabberMessageType.groupchat);

What I would like though is to be able to do something like:

enum MessageType type = MessageType(JabberMessageType.groupchat);
// or 
enum MessageType type = MessageType.FromJabberJid(JabberMessageType.groupchat);

So that the conversion belongs with the enum rather than being a method outtside of.

Upvotes: 13

Views: 14965

Answers (4)

nawfal
nawfal

Reputation: 73283

You can write a simple one-liner which require less tweaking in future (in case further enum values are added), but this assumes, the enum names are the same in both types:

private MessageType ToMessageType(this JabberMessageType jabberType)
{
    return Enum.Parse(typeof(MessageType), jabberType.ToString(), true);
}

This is some kind of a mapping functionality, from one type to another.

Upvotes: 0

Connell
Connell

Reputation: 14421

You can't do this. An Enumeration Type translates to an int (or byte, sbyte, short, uint, long or ulong if specified) value and is not technically a class.

An enumeration type (also named an enumeration or an enum) provides an efficient way to define a set of named integral constants that may be assigned to a variable.

One solution would be to put a static method to do this in a Utilities class.

MessageType type = Utilities.MessageTypeFromJabberJid(JabberMessageType.groupchat);

Upvotes: 4

Johannes Kommer
Johannes Kommer

Reputation: 6451

Why not create an extension method to do this for you?

public static class ExtensionMethods
{
    public static MessageType ConvertMessageType(this JabberMessageType jabberType)
    {
        switch(jabberType)
        {
            case JabberMessageType.normal:
                return MessageType.Normal;
            // Add rest of types here.
            default:
                return MessageType.Error;
        }
    }
}

Example usage:

var type = JabberMessageType.normal; // JabberMessageType
var messageType = type.ConvertMessageType(); // Converted to your custom MessageType

Upvotes: 14

dowhilefor
dowhilefor

Reputation: 11051

How about a class where your enum is nested? Something like this

public static class Message
{
    public enum Types
    {

    }

    public static Message.Types ConvertMessageType(Message.Types jabberType)
    {
    }
}

Upvotes: 1

Related Questions