Return New

  • Thread starter Thread starter sougata12
  • Start date Start date
S

sougata12

Guest
Module Module1

Public Class shareTotal
Public Shared total As Integer 'Declaring Field
End Class

Sub main()
shareTotal.total = 10
Console.WriteLine("O/P of shareTotal.total = {0}", shareTotal.total)
Console.WriteLine()
Dim instanceVar As New shareTotal()
instanceVar.total += 100
Console.WriteLine("O/P of instanceVar.total = {0}", instanceVar.total)
Console.WriteLine()

returnClass().total += 1000

MsgBox("Value of total is " & CStr(shareTotal.total))
End Sub

Public Function returnClass() As shareTotal
MsgBox("Function returnClass() called")
Return New shareTotal
End Function

End Module


Can someone please explain what is the role of the statement: "Return New Sharetotal" in the function retrunClass. Two things confuse me:

1. to instantiate a class we need to provide an object name but here the NEW keyword is being followed directly by the class name ShareTotal

2. generally we return a particular instance of a class from a function. But here directly a class name is being returned. This also does not seem to be the anonymous type of object declaration because in anonymous type we do not give the class name but have to give the object name.

Can someone please explain how is this statement working?


Sougata Ghosh

Continue reading...
 
Back
Top