Jag lyckas inte lägga till en variabel av typ "egendefinierad typ" (Type) till en Collection. Någon som har en aning? Varför inte göra så här? Då kan du bygga upp en hel klass med länkar där de känner till föräldrar, nästa i listan osv på ett enkelt sätt. Mitt förslag är:Problem med Collection
Kopiera koden till ett vanligt .EXE projekt, lägg till en knapp. Försök köra. Lycka till!
Så här ser min kod ut
-BEGIN->
Private Type link
x As Integer
y As Integer
End Type
Dim linkCollection As Collection
Private Sub Command1_Click()
Dim lnkitem As link
Set linkCollection = New Collection
Call linkCollection.Add(lnkitem)
End Sub
<-END-Sv: Problem med Collection
<code>
'i formulär
Option Explicit
Dim lnklista As LankadLista
Private Sub Command1_Click()
Dim lnk As link
Set lnk = New link
Call lnklista.lista.Add(lnk)
End Sub
Private Sub Form_Load()
Set lnklista = New LankadLista
End Sub
'i klass LankadLista
Option Explicit
'local variable(s) to hold property value(s)
Private mvarlista As Collection 'local copy
Public Property Set lista(ByVal vData As Collection)
'used when assigning an Object to the property, on the left side of a Set statement.
'Syntax: Set x.lista = Form1
Set mvarlista = vData
End Property
Public Property Get lista() As Collection
'used when retrieving value of a property, on the right side of an assignment.
'Syntax: Debug.Print X.lista
If mvarlista Is Nothing Then
Set mvarlista = New Collection
End If
Set lista = mvarlista
End Property
'i klass Link
Option Explicit
'local variable(s) to hold property value(s)
Private mvarx As Integer 'local copy
Private mvary As Integer 'local copy
Public Property Let y(ByVal vData As Integer)
'used when assigning a value to the property, on the left side of an assignment.
'Syntax: X.y = 5
mvary = vData
End Property
Public Property Get y() As Integer
'used when retrieving value of a property, on the right side of an assignment.
'Syntax: Debug.Print X.y
y = mvary
End Property
Public Property Let x(ByVal vData As Integer)
'used when assigning a value to the property, on the left side of an assignment.
'Syntax: X.x = 5
mvarx = vData
End Property
Public Property Get x() As Integer
'used when retrieving value of a property, on the right side of an assignment.
'Syntax: Debug.Print X.x
x = mvarx
End Property
</code>Sv: Problem med Collection
<code>
'Form: Form1
Option Explicit
Private linkCollection As Collection
Private Sub Command1_Click()
Dim lnkitem As Link
Set linkCollection = New Collection
Set lnkitem = New Link
linkCollection.Add lnkitem
End Sub
'Class: Link
Option Explicit
Public X As Long
Public Y As Long
</code>
Kan ju oxå göra en liten funktion som skapar en instans av klassen och tilldelar den värden:
<code>
'Form: Form1
Private Sub Command1_Click()
Set linkCollection = New Collection
linkCollection.Add NewLink
End Sub
'Module: Module1
Public Function NewLink(Optional X As Long, Optional Y As Long) As Link
Set NewLink = New Link
NewLink.X = X
NewLink.Y = Y
End Function
'Class: Link
Option Explicit
Public X As Long
Public Y As Long
</code>