473,395 Members | 1,422 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Sub New() called when object declared BEFORE it's 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
|\|.
Nov 21 '05 #1
3 3066
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Peter Åstrand | last post by:
There's a new PEP available: PEP 324: popen5 - New POSIX process module A copy is included below. Comments are appreciated. ---- PEP: 324 Title: popen5 - New POSIX process module
8
by: john sun | last post by:
hello, When we use new operator to user defined type, constructor will be called and a memory block is created in heap. My question: where those non-pointer data members belonging to this...
3
by: Nimmi Srivastav | last post by:
There's a rather nondescript book called "Using Borland C++" by Lee and Mark Atkinson (Que Corporation) which presents an excellent discussion of overloaded new and delete operators. I am...
822
by: Turamnvia Suouriviaskimatta | last post by:
I 'm following various posting in "comp.lang.ada, comp.lang.c++ , comp.realtime, comp.software-eng" groups regarding selection of a programming language of C, C++ or Ada for safety critical...
17
by: Tim Baur | last post by:
Hi All, I'm a VB6er who's recently started using VB2005; I have a general question about variable declaration. In VB, I've always tried to avoid using the "As New" construct when declaring a...
6
by: Pablo | last post by:
Hello, I am writing a windows application using C++ and BorlandBuilder 6 compiler. It is an event driven program and I need to create objects of some classes written by me. One of the classes...
37
by: jht5945 | last post by:
For example I wrote a function: function Func() { // do something } we can call it like: var obj = new Func(); // call it as a constructor or var result = Func(); // call it as...
15
by: LuB | last post by:
I am constantly creating and destroying a singular object used within a class I wrote. To save a bit of time, I am considering using 'placement new'. I guess we could also debate this decision -...
9
by: karthikbalaguru | last post by:
Hi, I find that articles stating that 'placement new' constructs an object on a pre-allocated buffer and so takes less time. Actually, we have to consider the allocation of the buffer and then...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.