Link to home
Start Free TrialLog in
Avatar of TungVan
TungVan

asked on

link list of object class in VB6

I have 3 classes + 1 form: CListNode, CList, CCallCode, and a form to test

CCallCode
----------------------------------------------------------------------------------------------------------------
Private mCallCodeID As Integer
Private mCallCode As String

Public Property Get CallCodeID() As Integer
    CallCodeID = mCallCodeID
End Property

Public Property Let CallCodeID(ByVal ID As Integer)
    mCallCodeID = ID
End Property

Public Property Get CallCode() As String
    CallCode = mCallCode
End Property

Public Property Let CallCode(ByVal Code As String)
    mCallCode = Code
End Property

----------------------------------------------------------------------------------------------------------------

'CListNode'
----------------------------------------------------------------------------------------------------------------
Private mNodeData As Object
Private mNextNode As CListNode

Public Property Get Data() As Object
    Data = mNodeData
End Property

Public Property Let Data(ByVal vNewValue As Object)
   Set mNodeData = vNewValue
End Property

Public Property Get NextNode() As CListNode
    Set NextNode = mNextNode
End Property

Public Property Let NextNode(ByVal vNewValue As Variant)
    Set mNextNode = vNewValue
End Property
----------------------------------------------------------------------------------------------------------------

CList
----------------------------------------------------------------------------------------------------------------
Private mFirstNode As CListNode
Private mLastNode As CListNode

Public Function IsEmpty() As Boolean
    IsEmpty = IIf(mFirstNode Is Nothing, True, False)
End Function

Public Sub InsertAtFront(insertItem As Variant)
    Dim tempNode As CListNode
   
    If IsEmpty() Then
        Set mFirstNode = New CListNode
        Set mLastNode = mFirstNode
    Else
        Set tempNode = mFirstNode
        Set mFirstNode = New CListNode
        mFirstNode.NextNode = tempNode
    End If
    mFirstNode.Data = insertItem
End Sub

Public Sub InsertAtBack(insertItem As Variant)
    Dim tempNode As CListNode
    If IsEmpty() Then
        Set mLastNode = New CListNode
        Set mFirstNode = mLastNode
    Else
        Set tempNode = mLastNode
        Set mLastNode = New CListNode
        tempNode.NextNode = mLastNode
    End If
    mLastNode.Data = insertItem
End Sub
----------------------------------------------------------------------------------------------------------------

A form
----------------------------------------------------------------------------------------------------------------
Private mCList As CList



Private Sub cmdCallCode_Click()
   
    Dim mCallCode1 As New CCallCodes
    Dim mCallCode2 As New CCallCodes
   
    With mCallCode1
        .CallCodeID = 1000
        .CallCode = "Call Mom!"
    End With
   
    With mCallCode2
        .CallCodeID = 1001
        .CallCode = "Nurse Needed"
    End With
   
    Call mCList.InsertAtFront(mCallCode1)
    Call mCList.InsertAtBack(mCallCode2)
   
End Sub
----------------------------------------------------------------------------------------------------------------


There is no error compiling, but now i don't know how to access mCallCodeID and mCallCode in each of the CallCode object in the link list...
ASKER CERTIFIED SOLUTION
Avatar of EDDYKT
EDDYKT
Flag of Canada 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
SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America 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 TungVan
TungVan

ASKER



Thanks for all your help...I decided to go with Collection since it is more easy....:)
TungVan,

Here is how to encapsulate the Collection object in a Class so that you can use it in a For...Next loop.

<<< IMPORTANT >>> Follow the instructions at the bottom of the class in the newEnum() function.

' ----------------------------------------------------------------------------
' Form1
' ----------------------------------------------------------------------------
Option Explicit

Private List As New CList

Private Sub Form_Load()
    Dim mCallCode As CCallCode

    Set mCallCode = New CCallCode
    With mCallCode
        .CallCodeID = 1000
        .CallCode = "Call Mom!"
    End With
    List.InsertAtFront mCallCode
   
    Set mCallCode = New CCallCode
    With mCallCode
        .CallCodeID = 1001
        .CallCode = "Nurse Needed"
    End With
    List.InsertAtBack mCallCode
End Sub

Private Sub Command1_Click()
    Dim mCallCode As CCallCode
   
    For Each mCallCode In List                               ' <---- Enumerating the nodes in your CList with For...Next
        Debug.Print mCallCode.CallCode
        Debug.Print mCallCode.CallCodeID
    Next
End Sub



' ----------------------------------------------------------------------------
' Class CList
' ----------------------------------------------------------------------------
Option Explicit

Private myCollection As New Collection

Public Sub Clear()
    Set myCollection = New Collection
End Sub

Public Function Count() As Long
    Count = myCollection.Count
End Function

Public Function IsEmpty() As Boolean
    IsEmpty = (Me.Count = 0)
End Function

Public Sub InsertAtFront(ByRef insertItem As CCallCode)
    If Me.Count = 0 Then
        ' can't add item before the first item if the collection is empty
        myCollection.Add insertItem
    Else
        ' insert item before the first item
        myCollection.Add insertItem, , 1
    End If
End Sub

Public Sub InsertAtBack(ByRef insertItem As CCallCode)
    myCollection.Add insertItem
End Sub

Public Function newEnum() As IUnknown
    On Error Resume Next
    ' Click on Tools --> Procedure Attributes --> Advanced
    ' This function should be a hidden member and the Procedure Id should be -4
    Set newEnum = myCollection.[_NewEnum]
End Function