0

I have an existing enum

public enum BalanceType // existing enum
{
    Available,
    Phone,
    Commissary,
    Account,
    Reserve,
    Encumber,
    Debt,
    Held,
}

Now I want to create a new enum from it. The new one only contains two fields.

 public class IvrBalanceInfo
{
    public decimal Amount { get; set; }
    public IvrBalanceType Type { get; set; }
    public IvrBalanceInfo(BalanceInfo info, BalanceType type)
    {
        Amount = info.Amount;
        //How to create the enum IvrBalanceType?
    }

    public enum IvrBalanceType // new enum
    {
        Available,
        Phone,
    }
}

My question is how to map it quickly? I mean to convert the old one to new one. The old one is from the third party. There are too much elements. I only need two.

  • 4
    What's the question? – Albireo Dec 17 '15 at 15:39
  • 2
    What do you mean? You can't _create_ an enum (apart from really ugly things like generating assemblies using reflection emit). But more important, you should not have to do such things. What are you actually trying to do? – Tim Schmelter Dec 17 '15 at 15:39
  • Are you asking how to map one `enum` to the other? – juharr Dec 17 '15 at 15:40
  • Uhm, do you mean "how can I convert from `BalanceType` to `IvrBalanceType`"? – Albireo Dec 17 '15 at 15:40
  • just use if else or switch.... – sowen Dec 17 '15 at 15:41
  • 2
    Yes, I wan to convert it. @Albireo –  Dec 17 '15 at 15:41
  • http://stackoverflow.com/questions/1818131/convert-an-enum-to-another-type-of-enum – André Kops Dec 17 '15 at 15:42
  • What do you wish to map `Commissary` etc to in `IvrBalanceType`? – David Arno Dec 17 '15 at 15:43
  • You can do `Enum.TryParse(type.ToString(), true, out whatEver);` – juharr Dec 17 '15 at 15:44
  • @DavidArno, no. The old one is from the vendor. I don't need too much of them. I only need two of them and match them. –  Dec 17 '15 at 15:44
  • Then just do `Type = type == BalanceType.Available ? IvrBalanceType.Available : IvrBalanceType.Phone;` – David Arno Dec 17 '15 at 15:46
  • We seem to have been too hasty in closing this, as you provided clarification via the comments. It's unlikely you'll get the five required re-open votes. I'd recommend you delete this question, then ask again. Make sure you clarify you want to map from one enum to the other, that you are only interested in `Available` and `Phone` and explain what should happen if `type` is of one of the other values. – David Arno Dec 17 '15 at 15:53
  • Enums are integer types so quickest will be `NewEnumValue = OldEnumValue`. Another approach will be using `Enum.IsDefined` for checking if old enum's value defined in the new Enum – Fabio Dec 17 '15 at 15:53
  • @DavidArno, it is open now. Thanks. –  Dec 17 '15 at 16:49

2 Answers2

0

You can try following:

public class IvrBalanceInfo
{
    public decimal Amount { get; set; }
    public IvrBalanceType Type { get; set; }
    public IvrBalanceInfo(BalanceInfo info, BalanceType type)
    {
        Amount = info.Amount;
        Type = (IvrBalanceType)(int)type;
    }

    public enum IvrBalanceType // new enum
    {
        Available,
        Phone,
    }
}

I believe you should check for value or get some default for other values.

Just ensured it compiles, haven't got time to check how it works.

kamil-mrzyglod
  • 4,948
  • 1
  • 20
  • 29
0
public class IvrBalanceInfo
{
   public decimal Amount { get; set; }
   public IvrBalanceType Type { get; set; }
   public IvrBalanceInfo(BalanceInfo info, BalanceType type)
   {
      Amount = info.Amount;
      if (type == BalanceType.Available)
         Type = IvrBalanceType.Available;
      else if(type == BalanceType.Phone)
         Type = IvrBalanceType.Phone

      // here you have to handle the other values and set the default
      // values for the Type Property or it will take the default value
      // if you not set it
   }

   public enum IvrBalanceType // new enum
   {
       Available,
       Phone,
   }
}

the problem with the other solution is that it will throw an exception if the value of the type parameter was not one of the values BalanceType.Available, BalanceType.Phone

Hakan Fıstık
  • 16,800
  • 14
  • 110
  • 131