CtrlAltDestroy
01-09-2008, 04:33 PM
Any way to set the property of a User Defined Type that's a property of a class?
For example:
Type XandY 'Global type
X As Long
Y As Long
End Type
Class SomeClass
Private c_Pos As XandY
'[Property Let called "Pos" that edits c_Pos goes here]
'And then execute this code:
Dim Instance As New SomeClass
Instance.Pos.X = 3
Can this be done at all using a Property Let/Set Procedure?
Gruff
01-10-2008, 10:14 AM
In my experience objects in VB6 are slow and ugly.
If you are trying to make some sort of vector graphics tool I would not use objects at all
Use nested UDTs or move to VB.NET. (Much more robust in graphics.)
~T
OnErr0r
01-10-2008, 11:14 AM
Can this be done at all using a Property Let/Set Procedure?
Sure, you can do that one of two ways. Either expose a property Let for X and Y separately, or make a single XandY property. Note that Type XandY must be defined as public in a BAS module.
' Class code
Public Property Let X(ByVal lX As Long)
c_Pos.X = lX
End Property
Friend Property Let Pos(ByRef tPos As XandY)
c_Pos = tPos
End Property
' Usage
Private Sub Form_Load()
Dim tXY As XandY
Dim c As Class1
Set c = New Class1
tXY.X = 42
tXY.Y = 123
c.Pos = tXY
' Or
c.X = 42
End Sub
CtrlAltDestroy
01-11-2008, 11:26 AM
Thanks for the help. That wasn't the exact functionality I wanted, but I guess that's not possible.
Friend Property Let Pos(ByRef tPos As XandY)
c_Pos = tPos
End Property
This errored for me until I realized the name had to be different than a corresponding Get Property.
In my experience objects in VB6 are slow and ugly.
If you are trying to make some sort of vector graphics tool I would not use objects at all
Use nested UDTs or move to VB.NET. (Much more robust in graphics.)
I was actually trying to construct a class for a file format, so that cleanup and re-initialization of variables would be easier.
OnErr0r
01-11-2008, 06:55 PM
You asked for a UDT, so that's as close as you get. It is also possible with a class.
' begin cPos.cls
Option Explicit
Private m_lX As Long
Private m_lY As Long
Public Property Let X(ByVal lX As Long)
m_lX = lX
End Property
Public Property Get X() As Long
X = m_lX
End Property
Public Property Let Y(ByVal lY As Long)
m_lY = lY
End Property
Public Property Get Y() As Long
Y = m_lY
End Property
' begin Class1.cls
Option Explicit
Public Pos As cPos
Private Sub Class_Initialize()
Set Pos = New cPos
End Sub
' Usage:
Private Sub Form_Load()
Dim c As Class1
Set c = New Class1
c.Pos.X = 42
End Sub
OnErr0r
01-11-2008, 06:57 PM
This errored for me until I realized the name had to be different than a corresponding Get Property.
Name had to be different? Both the Let and Get would be named Pos. Do you mean the variable name?
CtrlAltDestroy
01-11-2008, 07:00 PM
Name had to be different? Both the Let and Get would be named Pos. Do you mean the variable name?
I had a Get property return a UDT and it worked fine, but the Let property errored when I tried to make it accept a UDT. Something about invalid last parameter. It fixed when I made it a Friend and named it something else, as you suggested.