Hi there,

I see a sample code that an object is instantiated within the
class definition block. But when I test the folllowing, I got
error "Process is terminated due to StackOverflowException."?

Imports System
Module xx
	Sub Main()
		Dim a As New c()
	End Sub
End Module

Public Class c
	Dim b As New c()
End Class

Thanks

A Huang

Recommended Answers

All 3 Replies

Yes, because that is an infinity loop.
You create a new class "c" and each time you create a new c you create again a new c...

Thanks for the response. I kind of felt recursive calling is the reason too.
But I was surprised that the code at page 53 of this file http://www.education.gov.za/Curriculum/Curriculum%20doc%20question%20Papers2007/FET/VB.NET%20TRAINING%20MANUAL%20FOR%20WEBSITE/VB.Net%20Main%20Training%20Manua%20Sepelangl.pdf works fine, which also calls the class itself within the
class definition. Why it works fine but not my simple test?

A Huang

Dim testStudent As New Student() 'Class Student does NOT have a "New" initializer so. Thats used for initialise a class and call a function from that class
testStudent.PerformOperations() 'Call function from that class (which actually just fills its properties)
Class Student 
Public Sub PerformOperations() 
Dim st1 As New Student()    ' creating new instance of Student to get access of its properties
st1.name = "Einstein"       ' setting the values of fields 
...
End Sub
End Class

tbh i dont know what they created a new class of its own in that function. would be easier to fill its properties just like
Me.name = "Einstein"

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.