1

I have copied a piece of legacy code written in VB.Net which when converted to C# has produced the following:

///<summary>
    ///Test whether SSAC should be serialized
    ///</summary>
    public virtual bool ShouldSerializeSSAC()
    {
        if (_shouldSerializeSSAC)
        {
            return true;
        }
        return (_sSAC != (SSAC_Type)null);
    }

It is throwing an error stating it cannot convert because it is a non-nullable type. The code for the SSAC is as follows:

[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.6.81.0"), System.SerializableAttribute()]
public enum SSAC_Type
{

    ///<remarks/>
    A,

    ///<remarks/>
    B,

    ///<remarks/>
    C,

    ///<remarks/>
    D,

    ///<remarks/>
    E,

    ///<remarks/>
    F,

    ///<remarks/>
    G,

    ///<remarks/>
    H,

    ///<remarks/>
    J
}

In VB.Net this function was as follows:

Public Overridable Function ShouldSerializeSSAC() As Boolean
        If _shouldSerializeSSAC Then
            Return True
        End If
        Return (_sSAC <> CType(Nothing, SSAC_Type))
    End Function

While the SSAC class was as follows:

 <System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.6.81.0"),
 System.SerializableAttribute()>
Public Enum SSAC_Type

    '''<remarks/>
    A

    '''<remarks/>
    B

    '''<remarks/>
    C

    '''<remarks/>
    D

    '''<remarks/>
    E

    '''<remarks/>
    F

    '''<remarks/>
    G

    '''<remarks/>
    H

    '''<remarks/>
    J
End Enum

How can I update the C# code to fix this error?

Jay
  • 3,012
  • 14
  • 48
  • 99

2 Answers2

2

Well, "fix" may not be the best term to use here, since it was probably unintended behavior in the VB code. But, if by "fix", you mean, how do you make it work the same, then the answer is that the C# code should return (_sSAC != SSAC_Type.A). To actually correct it, _sSAC should be declared as SSAC_Type? (nullable), then you could just return _sSAC.HasValue. The reason it worked in VB is because Nothing in VB is not exactly equivalent to null in C#. Nothing actually means the default value for any given type, so for an integer, like an enum, the default is 0.

Steven Doggart
  • 43,358
  • 8
  • 68
  • 105
  • Does this mean that the return (_sSAC != (SSAC_Type)null); always returns the first object in the enum list? – Jay Apr 10 '16 at 19:35
  • No. It returns a Boolean. It returns true if _ssac doesn't equal A (0). – Steven Doggart Apr 10 '16 at 19:39
  • OK I see where I can set the SSAC_Type? as nullable, what do you mean in relation to returning the _sSAC.HasValue? I cannot see a HasValue method only a HasFlag – Jay Apr 10 '16 at 19:47
  • I'm just guessing that that was what the code was unsuccessfully intending to do. – Steven Doggart Apr 10 '16 at 19:50
  • 1
    I think the issue you need to decide is what the VB was _trying_ to do and what you want it to so. It's pretty much nonsense as it was written. So you need to decide if it should check against the value zero, or rewrite the whole thing so that _sSac is Nullable. It's not so much a question of translation as deciding what you need and writing accordingly. – Stuart Whitehouse Apr 10 '16 at 20:05
  • @StuartWhitehouse Thanks for explaining that better than I did. I was driving so I couldn't respond more thoroughly. – Steven Doggart Apr 10 '16 at 21:14
0

'Nothing' when applied to an enum instance is always '0' in VB, whether or not the first value of the enum is explicitly set to anything else. That is, the actual first value of the enum is irrelevant. So the C# equivalent to your method is:

public virtual bool ShouldSerializeSSAC()
{
    if (_shouldSerializeSSAC)
        return true;

    return (_sSAC != 0);
}
Dave Doknjas
  • 6,394
  • 1
  • 15
  • 28
  • thanks Dave ive opted for the changing of the variables to be nullables and returning _sSAC.HasValue – Jay Apr 10 '16 at 20:47