Identical If () and If give different results.

What is the difference in the two code blocks below? I expected them to return the same result, but they do not.

In the case where xml. @Type = "null", I want PatientMetricTypeID (zero integer) to be Nothing.

Block # 1: If ()

In this case, it ends as 0. It seems that Nothing is considered as a whole and is not converted to 0. I can understand why this can happen, but not completely ... I would like to understand exactly how this works, and if there is workaround.

    Dim PatientMetricTypeID As Integer? = If(xml.@Type = "null",
                                 Nothing,
                                 CType([Enum].Parse(GetType(PatientMetricTypes), xml.@Type), Integer))

Block # 2: If

In this case, it ends as "No" - the expected behavior.

    Dim PatientMetricTypeID As Integer?

    If xml.@Type = "null" Then
        PatientMetricTypeID = Nothing
    Else
        PatientMetricTypeID = CType([Enum].Parse(GetType(PatientMetricTypes), xml.@Type), Integer)
    End If
+3
source share
3 answers

If Integer, Integer?.

VB.Net Nothing null; # default(T), T - , .
MSDN:

(Visual Basic)

.

If(..., Nothing, SomeInteger), If Integer, Nothing 0.
If Integer?, Nothing New Integer?().

. .

+8

. :

... = If(xml.@Type = "null", DirectCast(Nothing, Integer?), ...)

Microsoft :

VB.

+1

My suggestion is to use Integer?inside CTypeinsteadInteger

0
source

Source: https://habr.com/ru/post/1781694/


All Articles