Sub New() called when object declared BEFORE it's creation?

  • Thread starter Thread starter Nick Dreyer
  • Start date Start date
N

Nick Dreyer

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


|\|.
 
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]
 
may be you did following: Dim NewObject as NEW MyClass, instead of Dim
NewObject as MyClass ?
It would explain the behavoir. Hope it helps :)
 
OK. The code I posted was just typed up without actually running it. As
m. posseth pointed out, MyClass won't compile, as well as one unclosed paren.

The cause for the second call to New() on startup came from a module-level
class declaration that had slipped in way down in between some method
declarations. That class in-turned did a "Dim NewObject as New MyClassx"
which caused the second call to New().

When a class is created, is there an easy way to tell the name of a class
causing the creation?

|\|.

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


|\|.


|\|.

================================================================================

Hey you know something people? They won't go for no more
I'm not black, but there's a whole lotsa times Great Midwestern hardware store
I wish I could say I'm not white. Philosophy that turns away
(1965, Frank Zappa in Trouble Every Day From those who aren't afraid to say
-> Freak Out) What's on their mind -- The left behinds
Of The Great Society.
(1965, Frank Zappa in Hungry Freaks, Daddy
-> Freak Out)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top