Role of the NEW keyword

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

sougata12

Guest
Hi,

In the below code (Example 1) the following error comes in the line "instanceVar = returnClass()". However when I use the NEW keyword,i.e. if I write Dim instanceVar As New shareTotal() the error goes away. Can someone explain why is this happening?

The error is as follows:

1503259.png


'Example 1

Module Module1

Public Class shareTotal
'Nothing
End Class

Sub main()
Dim instanceVar As shareTotal() 'When NEW is used the error goes away
Dim check As Boolean
instanceVar = returnClass() 'error comes here
check = instanceVar Is returnClass()
Console.WriteLine("Result of (instanceVar Is returnClass) check: {0}", check)
Console.ReadLine()
End Sub

Public Function returnClass() As shareTotal
Return New shareTotal
End Function

End Module



The reason I am asking this question is because in the below example (Example 2), I can assign instance1 to instance2 although while creating instance2 we did not use the NEW keyword...this is exactly what I am trying to do in the example1 (i.e.assigning the instance returned by the function returnClass to instanceVar...but then where am I going wrong?


'Example 2

Module Module1

Public Class shareTotal
'Nothing
End Class

Sub main()
Dim instance1 As New shareTotal()
Dim instance2 As shareTotal
instance2 = instance1 'Works fine
End Sub

End Module


Sougata Ghosh

Continue reading...
 
Back
Top