[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 168
  • Last Modified:

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
0
learn
Asked:
learn
1 Solution
 
VbmasterCommented:
You can't declare something with both the WithEvents and the New keyword. It's clearly stated in the help file.
0
 
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 new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
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

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now