Problem about "new"

Hi Experts,

Why we can not do
Dim WithEvents myForm As New Form2
in Form1, but can do
Dim myForm As New Form2
learnAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

VbmasterCommented:
You can't declare something with both the WithEvents and the New keyword. It's clearly stated in the help file.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
VBGuruCommented:
check out the following constraints on using withevents. The question you are asking is one of the limitaions of the withevents.

You should be aware of the following limitations on the use of WithEvents variables:
·      A WithEvents variable cannot be a generic object variable. That is, you cannot declare it As Object — you must specify the class name when you declare the variable.
·      You cannot declare a WithEvents variable As New. The event source object must be explicitly created and assigned to the WithEvents variable.
·      You cannot declare WithEvents variables in a standard module. You can declare them only in class modules, form modules, and other modules that define classes.
·      You cannot create arrays of WithEvents variables.
0
VbmasterCommented:
And if you do not see which of the limitations that is causing your problem here... a tip is to take a look at the comment above the 'answer' ;)
0
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

learnAuthor Commented:
To Vbmaster:

Any reason for that, or a bug, or will be able to do so?
0
learnAuthor Commented:
To Vbmaster:

Any reason for that, or a bug, or will be able to do so?
0
VbmasterCommented:
I don't know, 'cause I do not know how the keyword New is actually used 'behind the scenes'. But if I would make a guess I would guess on no change on upcoming versions. You can still use code like following though

WithEvents myForm As Form2

And use Set myForm = New Form2 in the Load event. This will make the app have a similar effect as if you were using the New keyword.
0
Éric MoreauSenior .Net ConsultantCommented:
>Any reason for that, or a bug, or will be able to do so?

This is because when you declare a variable with the NEW, the initialization is reported to the first time the object is used (the first time you set/get a property or call a method).

WithEvents can't live with this reported initialization!
0
learnAuthor Commented:
VBGuru's answer is difficult for me while the others' comments are esy to follow.

Many thanks to VBGuru, anyway.
0
learnAuthor Commented:
Hi emoreau,

Thank you very much.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.