12

I have an enum

private enum TimeUnit
{      
  Day,
  Month,
  Year
}

And I'm populating a description with:

return string.Concat(unit, "(s)");

Where unit is a TimeUnit. Most of the time this works fine and displays "Days(s)" however on a particular server it's displaying as "1(s)"

What would cause this?

Liath
  • 9,913
  • 9
  • 51
  • 81
  • 3
    my guess would be different versions of .Net running? you solve it by just adding unit.ToString() – Marthin Feb 27 '13 at 12:30
  • 1
    I would be intrigued to know what makes this display differently. If it's the .NET version, when was it changed, and why? – Øyvind Bråthen Feb 27 '13 at 12:32
  • 1
    @Marthin I believe you're probably right with the .NET version (I didn't want to put my answer in the question) however string.Concat should cause an implicit ToString( ) so I'm skeptical that simply adding it will change without giving formatting information. – Liath Feb 27 '13 at 12:34
  • 1
    @Marthin - Not sure it would change anything as string.Concat does the ToString() already. – Simon Mourier Feb 27 '13 at 12:37
  • @Liath you are correct. it should force a ToString() but since it doesn´t I would still give it a try =) – Marthin Feb 27 '13 at 12:37
  • @Simon: I would presume that the [`String.Concat(object, object)` overload](http://msdn.microsoft.com/en-us/library/kbseaaft.aspx) is used. But calling `ToString` on a boxed enum should nevertheless invoke the correct method. – vgru Feb 27 '13 at 12:39
  • @groo - Yes that overload is used. Check with reflector, you'll see the ToString() is done in there, so I don't see what would add an extra ToString() – Simon Mourier Feb 27 '13 at 12:45
  • 2
    @Liath - is the problem is really with 1 ? or can it be another number? because different versions of the same enum type can cause this. For example if someday you add a 4th value and don't deploy the corresponding dll, you will see a '3(s)' displayed where the new dll is not deployed. – Simon Mourier Feb 27 '13 at 12:49
  • @SimonMourier it's all the numbers, because of our deployment procedure not worried about out of sync DLLs – Liath Feb 27 '13 at 13:32
  • 1
    @liath - ok, so different versions of an enum can really explain why it's displayed as numbers. One given version can simply not have the corresponding name-per-value defined, but all this compiles fine because enums are backed by integral types (int, etc.) and the value is blitted across methods calls without any check. – Simon Mourier Feb 27 '13 at 14:10

4 Answers4

10

Try using Enum.GetName()

it also has the advantage of being safer since it requires:

  • The value you passed in isn't null.
  • The value you passed in is of a type that an enumeration can actually use as it's underlying type, or of the type of the enumeration itself. It uses GetType on the value to check this.
Community
  • 1
  • 1
happygilmore
  • 3,008
  • 4
  • 23
  • 37
4

You should format appropriately using ToString:

return string.Concat(unit.ToString("F"), "(s)");
Grant Thomas
  • 44,454
  • 10
  • 85
  • 129
0

Because Enum is digital enumeration optionnalyexpressed with strings (consider them alias)

Nogard
  • 1,779
  • 2
  • 18
  • 21
  • 2
    Yes, but this doesn't explain why two different boxes should perform the ToString operation in a different way. – Liath Feb 27 '13 at 12:32
0

MSDN says:

This method works as if the general format character, "G", were specified. That is, if the FlagsAttribute is not applied to this enumerated type and there is a named constant equal to the value of this instance, then the return value is a string containing the name of the constant. If the FlagsAttribute is applied and there is a combination of one or more named constants equal to the value of this instance, then the return value is a string containing a delimiter-separated list of the names of the constants. Otherwise, the return value is the string representation of the numeric value of this instance.

So ideally in your case it should work fine (considering FlagsAttribute is not applied and there is a named constant equal to the value of this instance). Can you mention the differences between the boxes?

Also, try giving explicit numbers.

private enum TimeUnit
{      
  Day = 1,
  Month = 2,
  Year = 3
}
publicgk
  • 3,170
  • 1
  • 26
  • 45
  • 1
    Giving then explicit values won't make a difference. – vgru Feb 27 '13 at 12:43
  • @Groo, I agree. However, based on MSDN description it *seems* that he's running into case 3 (otherwise, the return value is the string representation of the numeric value of this instance). So, maybe the unit variable is having a value outside the enum range (although 1 as per `1(s)` should be within). With limited info, it's guesswork. – publicgk Feb 27 '13 at 12:49