Link to home
Start Free TrialLog in
Avatar of egsemsem
egsemsem

asked on

user defined types in class modules

hi experts,
 I want to create a UDT in a class module.

 for example, I put this code in the module:

     Public Type a
         X As Integer
         Y As Integer
     End Type

 and i create a class module and put this:

     Public a1 As a

 And in a form I put this:

        Dim MyFrm As Class1

     Private Sub Form_Load()
         Set MyFrm = New Class1
         MyFrm.a1.X = 10
         MsgBox MyFrm.a1.X
     End Sub

But it doesn't work.

thank you    
Avatar of caraf_g
caraf_g

Indeed. It doesn't. C'est la vie...
You could however create another little class for holding your "a" information and use that as the type for a1.
Visual Basic does not allow declaration of Public Constants, Strings, arrays , Declares or UDTs in object CLass modules. If your class module is in project which also has a form(part of standard exe) then it wont allow you declare UDT as public. It can be declared only as Private.
caraf_g's got the right idea by suggesting the use of a class.
This, well, works :-)

Put this in your class:
' Class1 ---------------------
Option Explicit
Private m_a1 As a

Friend Property Get a1() As a
    a1 = m_a1
End Property

And this in your form:
' Form1 ---------------------
Option Explicit
Dim MyFrm As Class1

Private Sub Form_Click()
    Set MyFrm = New Class1
    With MyFrm.a1
        .X = 10
        MsgBox .X
    End With
End Sub
Nice one.... as long as you remain in the same project. But perhaps that is indeed what egsemsem is doing..... in which case it's pretty good indeed.
ASKER CERTIFIED SOLUTION
Avatar of ameba
ameba
Flag of Croatia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of egsemsem

ASKER

Thank you all for your great help. Unforunatly, I couldn't distribute points among you, so I chose the best one!

osama
Blimey, Bruno, looks like they've really got it in for you today....

egsemsem, you've said it: "great help". So why the "B"? What pinnacle of perfection must we reach in order to be worthy of receiving an "A"?

:-(
I really hate when I get B!
Re-opening my black list application... Three names so far...
I think that the rank (A,B or C) represnts how difficult the question is, not how good the answer is , am I right ?!
no, the points represent the difficulty of the question. The grade represents the quality of the answer.
oh...sorry, i am new in experts exchange !
No problem, egsemsem, we live and learn.

:o)