4

See below enum contains two members: Test and Production

public enum OTA_HotelInvCountNotifRQTarget
{      
    Test,      
    Production,
}

I'm looking for the way to add and use Null value in above enum from code like :

inv.Target = OTA_HotelInvCountNotifRQTarget.Null; // Not allowed

Update: I do not want to add extra NULL in above enum, I want to make this dynamic because the above enum is auto generated. and should be remain same.

Is there a way to achieve this in a method itself i.e without creating any Class or adding extra Enum value in enum? like : inv.Target = OTA_HotelInvCountNotifRQTarget.Null;

How can I do this?

1
  • Enum is serialized and presented as numeric (by default int). You can use non-existing value as a sign of invalid entry, e.g. inv.Target = (MyEnum)-1;. Commented Aug 17, 2016 at 13:05

4 Answers 4

5

The underlining values of an enum are int which can't be assigned to null.

If you still want to do so:

  1. Add Null as an option to the enum:

    public enum OTA_HotelInvCountNotifRQTarget
    {
        Null,
        Test,
        Production
    }
    
  2. Have your target a Nullable type:

    Nullable<OTA_HotelInvCountNotifRQTarget> t = null;
    
    //Or in a cleaner way:
    OTA_HotelInvCountNotifRQTarget? t = null;
    
    //And in your class:
    public class YourType
    {
        public OTA_HotelInvCountNotifRQTarget? Target { get; set; }
    }
    
Sign up to request clarification or add additional context in comments.

11 Comments

option two looks good, how can I use this in place of line : inv.Target = OTA_HotelInvCountNotifRQTarget.Null;
@RSH - please explain. Didn't understant
OTA_HotelInvCountNotifRQTarget? t = null; using this code gives me error that OTA_HotelInvCountNotifRQTarget is a type but used like variable.
I replace the code like : inv.Target = OTA_HotelInvCountNotifRQTarget? t = null;
Thank you so much @Gilad Green :)
|
0
public enum OTA_HotelInvCountNotifRQTarget
{    
    Null,  
    Test,      
    Production
}

also answewred here How to set enum to null

3 Comments

Close, but you probably want Null to be the first entry so it's the default.
I want to do this from code, means from where I'm using these enum.. I dont want to add extra value i.e. Null in it
then i suggest you should google, stackoverflow.com/questions/4337193/how-to-set-enum-to-null, it was the first google result for make enum nullable
0

Enum is enum. It value type and you can use Nullable<OTA_HotelInvCountNotifRQTarget> or OTA_HotelInvCountNotifRQTarget? for represent "not set statement"

but if you still want user null as enum value try

public enum OTA_HotelInvCountNotifRQTarget
{
    Null,
    Test,
    Production
}

Comments

0

Or using the "?" Operator for the variable:

public OTA_HotelInvCountNotifRQTarget? Target;

You have to change the assignment to:

inv.Target = null;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.