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
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
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
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
HOWTO: Remote User-Defined Types
http://support.microsoft.com/support/kb/articles/Q185/7/00.asp
http://support.microsoft.com/support/kb/articles/Q185/7/00.asp
ASKER
Thank you all for your great help. Unforunatly, I couldn't distribute points among you, so I chose the best one!
osama
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"?
:-(
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...
Re-opening my black list application... Three names so far...
ASKER
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.
ASKER
oh...sorry, i am new in experts exchange !
No problem, egsemsem, we live and learn.
:o)
:o)