1

I have created the following class based on a Singleton Pattern:

public sealed class UTrans
{
    private static volatile UTrans _instance;
    private static readonly object syncRoot = new Object();
    private UTrans(){}

    private static UTrans Instance
    {
        get
        {
            if (_instance == null)
            {
                lock (syncRoot)
                {
                    if (_instance == null)
                        _instance = new UTrans();
                }
            }
            return _instance;
        }
    }
}

Within this Class I have created an enum property and string property

    private static MethodType _method; // Alphabectic 1 - 50
    public static MethodType Method
    {
        get {  return _method; }
        set { _method = value; }
    }
    private static string _uName;
    public static string UserName
    {
        get { return _uName; }
        set { _uName = value; }
    }

I also have some methods that take a number of arguments in this class. When I call the class instance in code, if the user assigns values to the properties; those values will be used. Otherwise, the values passed as arguments will be used.

This works fine in the case of the UserName property by checking for a null value on the property:

var un = UserName ?? user;

However I cannot perform the same check for the enumeration property because it seems that the property automatically assumes the 1st value of the enumeration if one is not assigned.

I tried to circumvent the issue by assigning the 1st value of the enum as "Unspecified". Then I may proceed to code as such:

var processMethod = TranslateMethodType(Method) == "Unspecified" ? method : Method;

Where TranslateMethodType is a private method that converts the selected enumeration to a equivalent string value.

I don't believe this is the most elegant approach to the issue though and would like some feedback on possible alternatives to this issue.

Is there a way to check that a value has not been set for the MethodType property by the user of the class without having to add an "Unspecified" value as the first value since this value is ONLY there as a way to indicate no value was set?

Obviously, this may not be the case if the user decides to use the value and it would yield undesirable results to construct the code this way.

Can a enumeration Method be marked as nullable???

Mark
  • 1,667
  • 2
  • 24
  • 51
  • 1
    Sidenote: I recommend using `Lazy` over the double check lock pattern in .net 4. See http://csharpindepth.com/Articles/General/Singleton.aspx – CodesInChaos Aug 26 '12 at 23:14
  • 1
    Possible duplicate http://stackoverflow.com/questions/1795657/c-sharp-enums-nullable-or-unknown-value – Patrick Aug 26 '12 at 23:17
  • Is question somehow specific for the singleton pattern or is it about nullable enums? – Patrick Aug 26 '12 at 23:22
  • Good point Patrick. I should have made the question more specific to nullable enums since the Singleton Pattern is not exactly relevant to the question. But your sidenote is greatly appreciated which made my lack of cooth for phrasing the question slightly slanted in my favor for your 1st response regarding the Lazy method. ;-) Much appreciated! – Mark Aug 26 '12 at 23:47

3 Answers3

2

Can a enumeration Method be marked as nullable???

Yes.

public Nullable<MyEnum> Bob(){
    //stuff
}

var bob = Bob() ?? MyEnum.Default;
Josh
  • 44,706
  • 7
  • 102
  • 124
  • @Mark - No problems man. Just remember that any ValueType can be wrapped in a Nullable – Josh Aug 26 '12 at 23:46
1

Yes, you can make the Method field/property nullable.

private static MethodType? _method; // Alphabectic 1 - 50
public static MethodType? Method
{ 
    get {  return _method; } 
    set { _method = value; }
} 

Then you can do

var processMethod = Method == null ? method : Method.Value;

or

var processMethod = Method.HasValue ? Method.Value: method;
Andrew Cooper
  • 32,176
  • 5
  • 81
  • 116
0

You can mark it as nullable however what you're experiencing is expected behaviour.

Value types that are members of a reference type are initialized to 0. This is why you should always provide a value of 0 for your enums. If you don't, then users of your class will never know that the class has invalid state.

Simon Whitehead
  • 63,300
  • 9
  • 114
  • 138