I thought I’d share a little method I needed to create yesterday that casts one enum to another. For one reason or another, the system I’m working on at work has two different ways of identifying the type of an address, we’ve created enums for them: AddressCode and AddressType. The AddressCode is a short 4 character value that’s used in the database (I know its not relational but this is a legacy system and it’s too late to change it), the AddressType is a more verbose description. Ultimately though they both boil down to the same thing but in some cases the Type is needed and in others the Code. Yesterday I found myself needing to translate an AddressCode to its corresponding AddressType and so came up with the following method.
public static IAddress.AddressType CastAddressCodeAsAddressType(IAddress.AddressesCode addresscode)
{
switch (addresscode) {
case IAddress.AddressesCode.PRIV:
return IAddress.AddressType.HomeAddress;
case IAddress.AddressesCode.BUS1:
return IAddress.AddressType.BusinessAddress;
case IAddress.AddressesCode.DELV:
return IAddress.AddressType.DeliveryAddress;
case IAddress.AddressesCode.NULL:
return IAddress.AddressType.OtherAddress;
default:
return IAddress.AddressType.OtherAddress;
}
}
and in VB.NET
Public Shared Function CastAddressCodeAsAddressType(ByVal addresscode As IAddress.AddressesCode) As IAddress.AddressType
Select Case addresscode
Case IAddress.AddressesCode.PRIV
Return IAddress.AddressType.HomeAddress
Case IAddress.AddressesCode.BUS1
Return IAddress.AddressType.BusinessAddress
Case IAddress.AddressesCode.DELV
Return IAddress.AddressType.DeliveryAddress
Case IAddress.AddressesCode.NULL
Return IAddress.AddressType.OtherAddress
Case Else
Return IAddress.AddressType.OtherAddress
End Select
End Function
It is a quite basic in that it only allows a one-way conversion, all it does is look up the AddressType that corresponds with the AddressCode passed in using a switch or Select Case statement. I could have used a Dictionary<IAddress.AddressCode, IAddress.AddressType>, and to be honest I’m not sure why I didn’t – as a Generics driven method it would be easier to extend / enhance…
I have a similar method that goes in the other direction – not pretty or clever but quick and dirty.
Here’s a quick MbUnit test for the method
[Test()]
[Row(IAddress.AddressesCode.PRIV, IAddress.AddressType.HomeAddress)]
[Row(IAddress.AddressesCode.BUS1, IAddress.AddressType.BusinessAddress)]
[Row(IAddress.AddressesCode.DELV, IAddress.AddressType.DeliveryAddress)]
[Row(IAddress.AddressesCode.NULL, IAddress.AddressType.OtherAddress)]
public void CastAddressCodeAsAddressType_WithCodeEnums_ReturnsCorrespondingTypeEnum(IAddress.AddressesCode addressCode, IAddress.AddressType addressType)
{
IAddress.AddressType returnedEnum = Address.CastAddressCodeAsAddressType(addressCode);
Assert.AreEqual(addressType, returnedEnum);
}
I won’t post the VB.NET equivalent because besides changing the brackets there’s nothing else that needs changing. I’m using RowTests because I’m essentially just testing the same thing but with a fixed set of possible outcomes and its neater than having 4 tests.
40c71fd6-627c-40a7-a048-a0ae91ea89d7|0|.0
Permalink |
Comments (0)