1

Lets say for maintenance and DataContract serialization i need to add the default value 0 to an existing enum where it wasn't present.

public enum ExistingEnum { Value1 = 1, Value2 = 2, Value3 = 3 }

Becomes:

public enum ExistingEnum { None = 0, Value1 = 1, Value2 = 2, Value3 = 3 }

All the properties that relied on taking the Value1 as default value are now causing a chain of problems and related Exceptions. Is there a way, like an attribute, to impose Value1 as the default value, again? Something similar to:

public enum ExistingEnum
{
    None = 0,
    [Default] Value1 = 1,
    Value2 = 2,
    Value3 = 3
 }

Thanks in advance

Manfred Radlwimmer
  • 13,257
  • 13
  • 53
  • 62
strongmmc
  • 137
  • 12
  • 1
    So you want to add a default which shouldn't be the default? Why not add none as a value higher than 0? Can't you use some kind of converter to let the serializer work as you want? – Patrick Hofman May 24 '17 at 13:20
  • 1
    Can you clarify what you mean by the default value? if I do `Console.WriteLine(default(ExistingEnum))` on the precchanged enum it just prints `0` which is what I would expect the default value to be. The fact it doesn't have any meaning doesn't make a difference. See for example https://stackoverflow.com/questions/529929/choosing-the-default-value-of-an-enum-type-without-having-to-change-values . If your code was defaulting to `Value1` before you must have some code to do that. Perhaps if you share this code we can help you modify it (or maybe you can do it yourself once you find it). – Chris May 24 '17 at 13:25
  • c# requires all varibles to be declared so set default in the declaration : Existing myVaraible = Existing.Value1;. I would change name of Value1 to Default. – jdweng May 24 '17 at 13:26
  • The default value for an Enum is 0 and this can't be changed, so you might need to change what your enums represent – David Wilson May 24 '17 at 13:26
  • Afaik: The default value of an enum is always `0` regardless of if that value has a matching enum member. – Manfred Radlwimmer May 24 '17 at 13:27
  • @PatrickHofman because SOAP serialization fails when encountering an enum without a 0 value – strongmmc May 24 '17 at 13:28
  • @strongmmc That sounds highly unlikely. I've used plenty of enums with defined 0 values. Are you sure the problem isn't somewhere else? Did you forget to update the definition and `0` is simply unrecognized? The same thing would happen with other serialization methods like the XmlSerializer – Manfred Radlwimmer May 24 '17 at 13:30
  • @ManfredRadlwimmer i didn't explain myself. When i have an enum starting from 1, SOAP serialization fails. When i have an enum starting from 0, SOAP serialization succeedes. – strongmmc May 24 '17 at 13:31
  • That's to be expected (if you don't initialize the values), what's the question? – Manfred Radlwimmer May 24 '17 at 13:33

2 Answers2

3

I should stop asking things on StackOverflow. Everytime I try to keep it simple and easy to understand, it seems nobody reads the the question. I asked one simple thing, which was answered by a MSDN page I wasn't able to find in the past 45 minutes, but that I found now: System.ComponentModel.DefaultValueAttribute

If I have an enum that goes from 0 to 3 and I need 1 to be taken as default value when instantiating a variable of that enum type, I need to use this attribute class.

[DefaultValue(typeof(ExistingEnum),"Value1 ")]
public enum ExistingEnum
{
    None = 0,
    Value1 = 1,
    Value2 = 2,
    Value3 = 3
}

Thanks anyway to everyone who spent its time answering this stupid question

Chris
  • 27,210
  • 6
  • 71
  • 92
strongmmc
  • 137
  • 12
  • 2
    As long as you share the solution (as you did) it might help others with the same problem. You might want to update the question however, without the information you provided in the comments I didn't have any idea what you wanted either. – Manfred Radlwimmer May 24 '17 at 13:45
  • 1
    It is worth noting that this attribute only works for things that support it. eg if you did `Console.WriteLine(default(ExistingEnum);` then it would still print out `None` because the compiler doesn't do anything with it. Particularly your comment "taken as the default value when instantiating a variable of that enum type" is categorically wrong. See https://stackoverflow.com/questions/2329868/net-defaultvalue-attribute for more discussion on it. (continued) – Chris May 24 '17 at 14:22
  • It is quite likely that your serializer will be paying attention to that though so when it creates an object where the value hasn't been defined then it can set the value after it has been created. So if for example you have some XML where the value doesn't appear then it can use your annotation to choose what the default may be. This is obviously a different situation from when code you are writing creates an enum itself which is what I have commented on above. – Chris May 24 '17 at 14:23
-1

Try this

      public enum Existing
       {
           Default = 1,
           None = 0,
           Value1 = 1,
           Value2 = 2,
           Value3 = 3
       }
jdweng
  • 33,250
  • 2
  • 15
  • 20
  • 2
    Have you tried using `Console.WriteLine(default(ExistingEnum));` to see what the default value of that enum is? Its not the one you've called "Default". – Chris May 24 '17 at 13:30
  • Chris : It should be Console.WriteLine(Existing.Default.ToString()); – jdweng May 24 '17 at 13:34
  • @jdweng OP doesn't want a value *named* Default that's not `0`, he needs a **default value** that's not `0`. – Manfred Radlwimmer May 24 '17 at 13:35
  • The question is : "Somethin similar to". My solution meets the requirments. – jdweng May 24 '17 at 13:36
  • @jdweng The problem is that his uninitialized enums can't be serialized because they are `0`. Your answer doesn't solve that problem at all. He already stated, that defining `None = 0` as an enum member circumvents the problem but he wants a better solution (his attempt with the `[Default]` attribute). – Manfred Radlwimmer May 24 '17 at 13:37
  • @jdweng: Perhaps you could add somethign to your answer to clarify the advantages of this solution? I'm not sure why the OP would update all his code to `Existing.Default` rather than `Existing.Value1`... Presumably the idea is that if next week he changes his mind on what the default is that he could change the value of Default in the enum? – Chris May 24 '17 at 14:33