Hello Nick
Your problem sounded a bit strange to me ,,, curious as i am i tried to
reproduce it
i used this code
Public Class Form1
Inherits System.Windows.Forms.Form
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim NewObject As MyClassx
MsgBox("Before Creation")
NewObject = New MyClassx
MsgBox("After Creation")
End Sub
End Class
Public Class MyClassx
Sub New()
MsgBox("Running Sub New()")
End Sub
End Class
well in my situation it worked as expected ,, the msgbox with msg "running
sub new" was only called one time
also Myclass can`t be used as a class name so i guess you pasted pseudo code
an not the true failing code
however the code you posted works as expected ,,, so your problem is
probably something else that you overlooked ,,
Regards
Michel Posseth [MCP]
"Nick Dreyer" <gu****@oz.net> wrote in message
news:42*************@news.oz.net...
I was quite surprised to notice that Sub New() gets called twice,
once at declaration time and once at creation time. I can't figure out
why it would be called at declaration if there is no class instance
to work with. What is going on here:
Sub Main
Dim NewObject as MyClass
MsgBox("Before Creation")
NewObject= New MyClass
MsgBox("After Creation")
End Sub
Class MyClass
Sub New()
MsgBox("Running Sub New()"
End Sub
End Class
This puts out a MsgBox 4 times:
1) Running Sub New()
2) Before Creation
3) Running Sub New()
4) After Creation
|\|.