• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 247
  • Last Modified:

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...
0
TungVan
Asked:
TungVan
  • 2
2 Solutions
 
EDDYKTCommented:
Is it easies to use collection?


ie

Private a As New Collection

Private Sub Command2_Click()


Dim mCallCode1 As New CCallCodes
   Dim mCallCode2 As New CCallCodes
   
   With mCallCode1
       .CallCodeID = 1000
       .CallCode = "Call Mom!"
   End With
   a.Add mCallCode1, mCallCode1.CallCode
   With mCallCode2
       .CallCodeID = 1001
       .CallCode = "Nurse Needed"
   End With
   
   a.Add mCallCode2, mCallCode2.CallCode
   
   Dim obj
   
   Set obj = a.Item("Nurse Needed")
End Sub
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
In your CList class add this:

    Public Property Get FirstNode() As CListNode
        Set FirstNode = mFirstNode
    End Property

In your CListNode class, your Data() property needs a "Set" in front of Data = mNodeData:

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

In your Form, you need to create an instance of CList so change your declaration to use the "New" keyword:

    Private mCList As New CList

Finally, to walk the nodes in your list you could use code like this:

    Private Sub Command1_Click()
        Dim n As CListNode
        Dim d As CCallCode
   
        Set n = mCList.FirstNode
        While Not (n Is Nothing)
            Set d = n.Data
            Debug.Print d.CallCodeID
            Debug.Print d.CallCode
            Set n = n.NextNode
        Wend
    End Sub
0
 
TungVanAuthor Commented:


Thanks for all your help...I decided to go with Collection since it is more easy....:)
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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

0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now