By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,949 Members | 1,426 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,949 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
|\|.
Nov 21 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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
|\|.

Nov 21 '05 #2

P: n/a
may be you did following: Dim NewObject as NEW MyClass, instead of Dim
NewObject as MyClass ?
It would explain the behavoir. Hope it helps :)
"Nick Dreyer" <gu****@oz.net> schrieb im Newsbeitrag
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
|\|.

Nov 21 '05 #3

P: n/a
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?

|\|.

On Sat, 30 Jul 2005 16:33:51 GMT, gu****@oz.net (Nick Dreyer) wroth:
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)
Nov 21 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.