Go Premium for a chance to win a PS4. Enter to Win

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

Question about : For Each XXX In XXXX

Hello,
I have a form containing two command buttons :

Option Explicit

Private Sub Command1_Click()
Dim mynew As Form1
Set mynew = New Form1
mynew.Show
End Sub

Private Sub Command2_Click()
Dim myform As Form  
~~~~~~~~~~~~~~~~~~
For Each myform In Forms
    Debug.Print TypeName(myform)
    Unload myform
Next myform

End Sub

--
What surprises me is the line above.  There is no error if
I use "Dim myform As Form" or "Dim myform As Form1".
But typename(myform) shows 'form1'.

I want to know if :
Dim myvar As X
For Each myvar In Y
....

the, should the class of X = class of element in Y ?

Thank you.

rgds.
alan
alanpong@hkstar.com
0
alanpong
Asked:
alanpong
1 Solution
 
traygreenCommented:
Should not suprise you.
Form1 is a descendant of type form, it is your set command that will determine the attributes (properties, contained controls etc) of the new form.  So the name will always be the same unless you explictly change it.

Assuming that Y is a collection of <objects> X, then YES..
each MyVAR (instance of type X)will be an element of Y
0
 
alanpongAuthor Commented:
after thinking it again, i have the conclusions as below :

form is a parent class of form1.  By "Set" , form1
is instanced as a object that
inherit form's properties, method.... and so on.
In addition, two button are included in form1.

They are different classes in the view
of c++ :

class form has xxxx
class form1 has xxxx + two buttons

I don't think the <object> form is the same as
<object> form1 because object is a instance of class.  
However, I think the following is correct in VB :
 If A is B's parent, then type A = type B.
,where "type" is VB-defined characteristic. (e.g.
type can be "object","form", "control", "variant".....)

If so, "Dim myform as form" has the same effect as
"Dim myform as form1". The former means myform has the form's type.
The latter means myform has the form1's type.
(form's type = form1's type)


For the line "for each myform in forms", forms
contain a collection of object references.  In order
to assign these object references to the object varible
myform.  They should have the same object type.  Otherwise,
there is "Type match" error.

rgds.
alan
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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