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?

  • `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;`. – Sinatr Aug 17 '16 at 13:05

4 Answers4

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; }
    }
    
Gilad Green
  • 36,708
  • 7
  • 61
  • 95
  • option two looks good, how can I use this in place of line : `inv.Target = OTA_HotelInvCountNotifRQTarget.Null;` –  Aug 17 '16 at 13:00
  • @RSH - please explain. Didn't understant – Gilad Green Aug 17 '16 at 13:01
  • `OTA_HotelInvCountNotifRQTarget? t = null;` using this code gives me error that `OTA_HotelInvCountNotifRQTarget` is a `type` but used like variable. –  Aug 17 '16 at 13:04
  • I replace the code like : `inv.Target = OTA_HotelInvCountNotifRQTarget? t = null;` –  Aug 17 '16 at 13:05
  • ya.. that won't work.. You need to replace the type of `Target` from `OTA_HotelInvCountNot‌​ifRQTarget` to `OTA_HotelInvCountNot‌​ifRQTarget?` – Gilad Green Aug 17 '16 at 13:05
  • @RSH - Did the additional explanation helped you with your question? – Gilad Green Aug 17 '16 at 13:29
  • Another option would be to set it as a Flag Enum and set to 0 but that's not much different to your option 1 and would allow `Test & Production` as a value – MikeT Aug 17 '16 at 16:15
  • @GiladGreen: Thanks! Yes given examples work for me, but in our case I want to do this in a function itself, I can't add extra `class` like you suggested in ex. 2 or extra `Null` value in `Enum` in ex. 1, Is there any way to do it in single line of code or can it be done inside method itself? –  Aug 18 '16 at 05:23
  • @RSH- you can assign -1 because it is not in your email values and then it'll not match to anything - closest to having it null without actually changing it to a nullable type. My personally - I would not do it and would change the class/enum – Gilad Green Aug 18 '16 at 05:26
  • @RSH - In 2 I didn't suggest an extra class - but to change the property in your original class – Gilad Green Aug 18 '16 at 05:54
  • 1
    Thank you so much @Gilad Green :) –  Aug 19 '16 at 16:19
0
public enum OTA_HotelInvCountNotifRQTarget
{    
    Null,  
    Test,      
    Production
}

also answewred here How to set enum to null

Community
  • 1
  • 1
Neil
  • 641
  • 1
  • 7
  • 21
  • 1
    Close, but you probably want Null to be the first entry so it's the default. – adv12 Aug 17 '16 at 12:54
  • 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 –  Aug 17 '16 at 12:55
  • then i suggest you should google, http://stackoverflow.com/questions/4337193/how-to-set-enum-to-null, it was the first google result for `make enum nullable` – Neil Aug 17 '16 at 12:58
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
}
darkhac
  • 577
  • 4
  • 22
0

Or using the "?" Operator for the variable:

public OTA_HotelInvCountNotifRQTarget? Target;

You have to change the assignment to:

inv.Target = null;
mitch
  • 115
  • 3
  • 8