G
Gagan
Hi All,
I am new to VB.NET, and am confused by following code. The code
basically obtains an instance of an object from a helper method. This
helper method instantiates a new object, and returns it. But in its
Finally block, it sets the returned object to Nothing. What perplexes
me is that caller receives a valid object instance.
The queston is: does this work because I am lucky, or this is really
an expected behavior? Also, is this a correct coding practice? Will
appreciate explanation.
Thanks!
Gagan
=============
Module Module1
Class Salary
Private m_Amount As Double = 0.0
Public Property Amount() As Double
Get
Return m_Amount
End Get
Set(ByVal value As Double)
m_Amount = value
End Set
End Property
End Class
Public Function GetSalary(ByVal dblAmount As Double) As Salary
Dim pSalary As Salary = Nothing
Try
pSalary = New Salary()
pSalary.Amount = dblAmount
Return pSalary
Catch ex As Exception
'
Finally
pSalary = Nothing
End Try
End Function
Sub Main()
Dim pSalary As Salary = GetSalary(100.0)
System.Console.WriteLine(pSalary.Amount)
End Sub
End Module
I am new to VB.NET, and am confused by following code. The code
basically obtains an instance of an object from a helper method. This
helper method instantiates a new object, and returns it. But in its
Finally block, it sets the returned object to Nothing. What perplexes
me is that caller receives a valid object instance.
The queston is: does this work because I am lucky, or this is really
an expected behavior? Also, is this a correct coding practice? Will
appreciate explanation.
Thanks!
Gagan
=============
Module Module1
Class Salary
Private m_Amount As Double = 0.0
Public Property Amount() As Double
Get
Return m_Amount
End Get
Set(ByVal value As Double)
m_Amount = value
End Set
End Property
End Class
Public Function GetSalary(ByVal dblAmount As Double) As Salary
Dim pSalary As Salary = Nothing
Try
pSalary = New Salary()
pSalary.Amount = dblAmount
Return pSalary
Catch ex As Exception
'
Finally
pSalary = Nothing
End Try
End Function
Sub Main()
Dim pSalary As Salary = GetSalary(100.0)
System.Console.WriteLine(pSalary.Amount)
End Sub
End Module