?
Solved

link list of object class in VB6

Posted on 2005-03-15
4
Medium Priority
?
246 Views
Last Modified: 2013-11-23
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
Comment
Question by:TungVan
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 26

Accepted Solution

by:
EDDYKT earned 180 total points
ID: 13547398
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
 
LVL 86

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 120 total points
ID: 13547489
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
 

Author Comment

by:TungVan
ID: 13547852


Thanks for all your help...I decided to go with Collection since it is more easy....:)
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 13548332
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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question