Alguien tiene alguna idea de por qué esto no funciona (C# o VB.NET u otro lenguaje .NET no importa). Este es un ejemplo muy simplificado de mi problema (lo siento por VB.NET):Interesante comportamiento de la propiedad
Private itsCustomTextFormatter As String
Public Property CustomTextFormatter As String
Get
If itsCustomTextFormatter Is Nothing Then CustomTextFormatter = Nothing 'thinking this should go into the setter - strangely it does not'
Return itsCustomTextFormatter
End Get
Set(ByVal value As String)
If value Is Nothing Then
value = "Something"
End If
itsCustomTextFormatter = value
End Set
End Property
Si lo hace:
Dim myObj as new MyClass
Console.WriteLine(myObj.CustomTextFormatter)
Usted se sorprenderá de los resultados. Imprimirá "Nada". Alguien tiene alguna idea de por qué no se imprime "Algo"
He aquí una prueba unitaria por sugerencia:
Imports NUnit.Framework
<TestFixture()> _
Public Class Test
Private itsCustomTextFormatter As String
Public Property CustomTextFormatter As String
Get
If itsCustomTextFormatter Is Nothing Then CustomTextFormatter = Nothing 'thinking this should go into the setter - strangely it does not'
Return itsCustomTextFormatter
End Get
Set(ByVal value As String)
If value Is Nothing Then
value = "Something"
End If
itsCustomTextFormatter = value
End Set
End Property
<Test()>
Public Sub Test2()
Assert.AreEqual("Something", CustomTextFormatter)
End Sub
End Class
Esto devuelve:
Test2 : Failed
Expected: "Something"
But was: null
at NUnit.Framework.Assert.That(Object actual, IResolveConstraint expression, String message, Object[] args)
at NUnit.Framework.Assert.AreEqual(Object expected, Object actual)
Me sorprende que 'value =" Something "' compile. Hmm parece que 'value' no es una palabra clave en VB.net, sino solo un parámetro normal, que explica que se compila. – CodesInChaos
Es una cadena, no tiene que "Nuevo" al configurarlo – Denis
@CodeInChaos en C# usted puede hacer 'value = null;' en un setter –