locked
How can i initialise an enum to null ? RRS feed

  • Question

  • User1253338400 posted

    Hi ,

    I have the folllowing:

    public enum EntityObjectType{ SObject,Number, Text, DateTime,Currency}

    public virtual EntityObjectType EntityType
    {
    get { return _entityType; }
    set { _entityType = value; }
    }

    I want to do a few chekc as follows:

    !string.IsNullOrEmpty(EntityType) <--- that gives error 

    and initialize as follows:

    _entityType = null; <--- cannot convert null to EntityObjectType because its a non-nullable value type 

    How can i do these ?

    thanks

    Wednesday, September 4, 2019 5:40 AM

Answers

  • User-719153870 posted

    Hi robby32,

    _entityType = null; <--- cannot convert null to EntityObjectType because its a non-nullable value type 

    How can i do these ?

    As the error message said, a non-nullable value type can not set as null.

    However, this problem can be solved, you can find a similar thread here.

    In this thread, there are two ways offered:

    One is that you can add a default enum value in your EntityObjectType like "none", and do

    _entityType = EntityObjectType.none;

    Another is that make your _entityType a nullable value with a "?" behind it:

            public EntityObjectType? _entityType;
    
            public virtual EntityObjectType? EntityType
            {
                get { return _entityType; }
                set { _entityType = value; }
            }
    
            _entityType = null;

    Hope these can help you.

    Best Regard,

    Yang Shen

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, September 4, 2019 7:14 AM

All replies

  • User-719153870 posted

    Hi robby32,

    _entityType = null; <--- cannot convert null to EntityObjectType because its a non-nullable value type 

    How can i do these ?

    As the error message said, a non-nullable value type can not set as null.

    However, this problem can be solved, you can find a similar thread here.

    In this thread, there are two ways offered:

    One is that you can add a default enum value in your EntityObjectType like "none", and do

    _entityType = EntityObjectType.none;

    Another is that make your _entityType a nullable value with a "?" behind it:

            public EntityObjectType? _entityType;
    
            public virtual EntityObjectType? EntityType
            {
                get { return _entityType; }
                set { _entityType = value; }
            }
    
            _entityType = null;

    Hope these can help you.

    Best Regard,

    Yang Shen

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, September 4, 2019 7:14 AM
  • User1253338400 posted
    Perfect thanks
    Wednesday, September 4, 2019 8:32 AM