2012-07-12 2 views
3

J'ai une propriété type est Nullable de Integer une valeur par défaut Nothing comme indiqué ci-dessous:Définir la valeur par défaut de la propriété Nullable Nothing ne fonctionne pas comme on le souhaite

Property TestId As Integer? = Nothing 

le code suivant évalue la propriété testid Nothing (comme voulait)

Dim test As RadTreeNode = rtvDefinitionCreate.FindNodeByValue(DefinitionHeaderEnum.Test) 
If test Is Nothing Then 
    definition.TestId = Nothing 
Else 
    definition.TestId = test.Nodes(0).Value 
End If 

mais le code ci-dessous évalue 0 (valeur par défaut pour Integer, même quand est Integer? avec une valeur par défaut Nothing)

Dim test As RadTreeNode = rtvDefinitionCreate.FindNodeByValue(DefinitionHeaderEnum.Test) 
definition.TestId = If(IsNothing(test), Nothing, test.Nodes(0).Value) 

Quel est le problème avec le code ci-dessus? De l'aide??

(plus tard dans le code lorsque l'appel de la propriété, la propriété a 0)

Répondre

2

Ce que vous vous compilez code avec Option Strict Off.

Si vous compiler votre code avec Option Strict On, le compilateur vous donner une erreur, vous dire qu'il ne peut pas convertir String-Integer?, en évitant les suprises lors de l'exécution.


C'est une bizarrerie lors de l'utilisation properties/ternary operator/option strict off en VB.NET.

Consultez le code suivant:

Class Test 
    Property NullableProperty As Integer? = Nothing 
    Public NullableField As Integer? = Nothing 
End Class 

Sub Main() 
    ' Setting the Property directly will lest the ternary operator evaluate to zero 
    Dim b = New Test() With {.NullableProperty = If(True, Nothing, "123")} 
    b.NullableProperty = If(True, Nothing, "123") 

    ' Setting the Property with reflection or setting a local variable 
    ' or a public field lets the ternary operator evaluate to Nothing 
    Dim localNullable As Integer? = If(True, Nothing, "123") 
    Dim implicitLocal = If(True, Nothing, "123") 
    b.NullableField = If(True, Nothing, "123") 
    b.GetType().GetMethod("set_NullableProperty").Invoke(b, New Object() {If(True, Nothing, "123")}) 
    b.GetType().GetProperty("NullableProperty").SetValue(b, If(True, Nothing, "123"), Nothing) 
End Sub 

Une autre différence à considérer:

Dim localNullable As Integer? = If(True, Nothing, "123") 

évaluera à Nothing mais

Dim localNullable As Integer? = If(SomeNonConstantCondition, Nothing, "123") 

évaluera à 0


Vous pouvez créer une méthode d'extension pour effectuer le mauvais travail pour vous.

<Extension()> 
Function TakeAs(Of T, R)(obj As T, selector As Func(Of T, R)) As R 
    If obj Is Nothing Then 
     Return Nothing 
    End If 
    Return selector(obj) 
End Function 

et l'appeler comme

definition.TestId = test.TakeAs(Of Int32?)(Function(o) o.Nodes(0).Value) 
+0

Je pense que 'Option Strict On' ne permet pas ici puisque l'opérateur Si fait une conversion automatique élargissement de Rien à 0 (type de' test.Nodes (0) .Value') - mais la solution devrait fonctionner! –

+0

@PaulB. 'Option Strict On' aiderait ici de la façon qui provoquera une erreur de compilation, mettant en évidence le problème. – sloth

+0

Pour moi 'Dim i As Integer? = If (True, Nothing, 1) 'ne provoque pas d'erreur de compilation mais convertit Nothing en 0 puis 0 en un 0 nul. –

Questions connexes