
Private Sub Button3_Click(sender As System.Object, e As System.EventArgs) Handles Button3.Click
Dim aux As MiClase
aux = funcion()
MessageBox.Show(aux.NewProperty)
MessageBox.Show(aux.OtraPropiedad)
End Sub
Public Function funcion() As MiClase
Dim oRet As MiClase = New MiClase()
Dim oRet2 As MiClase = oRet
Try
oRet.NewProperty = "otro valor"
Return oRet
Catch ex As Exception
Return Nothing
Finally
oRet.Dispose()
oRet = Nothing
End Try
End Function
Yo, que mi orígenes fueron el c y el c++ (no todo tiempo pasado fue mejor), siempre entiendo que el “dispose” libera todos objetos de la clase . Por supuesto sé que el Finally se ejecuta.Dim aux As MiClase
aux = funcion()
MessageBox.Show(aux.NewProperty)
MessageBox.Show(aux.OtraPropiedad)
End Sub
Public Function funcion() As MiClase
Dim oRet As MiClase = New MiClase()
Dim oRet2 As MiClase = oRet
Try
oRet.NewProperty = "otro valor"
Return oRet
Catch ex As Exception
Return Nothing
Finally
oRet.Dispose()
oRet = Nothing
End Try
End Function
La pregunta de hoy es: qué valor tiene aux.NewProperty y aux.OtraPropiedad?.

Para dar alguna pista (o confundir más al lector) dejo la clase :
Public Class MiClase
Private newPropertyValue As String
Private _otraPropiedad As String
Public Property OtraPropiedad() As String
Get
Return _otraPropiedad
End Get
Set(ByVal value As String)
_otraPropiedad = value
End Set
End Property
Public Property NewProperty() As String
Get
Return newPropertyValue
End Get
Set(ByVal value As String)
newPropertyValue = value
End Set
End Property
Public Sub New()
newPropertyValue = "mi valor"
_otraPropiedad = "mi 2 valor"
End Sub
Public Sub Dispose()
_otraPropiedad = String.Empty
End Sub
End Class
La respuesta es: aux tiene valor. El return de la función ha retornado el puntero y la memoria realmente no se ha liberado. Dejo la captura del debug. Private newPropertyValue As String
Private _otraPropiedad As String
Public Property OtraPropiedad() As String
Get
Return _otraPropiedad
End Get
Set(ByVal value As String)
_otraPropiedad = value
End Set
End Property
Public Property NewProperty() As String
Get
Return newPropertyValue
End Get
Set(ByVal value As String)
newPropertyValue = value
End Set
End Property
Public Sub New()
newPropertyValue = "mi valor"
_otraPropiedad = "mi 2 valor"
End Sub
Public Sub Dispose()
_otraPropiedad = String.Empty
End Sub
End Class

Ya puestos en la “funcion” he puesto un código que no “vale para nada”, solo para ver que realmente se usan punteros aunque no lo veamos
Dim oRet2 As MiClase = oRet
La pregunta es : ¿qué vale oRet2 después de ejecutar oRet = Nothing?. Le pasa exatamente lo mismo que al aux: todo son punteros
En C# funciona exactamente igual