Results 1 to 4 of 4

Thread: dymanic list

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2000
    Posts
    6

    dynamic

    i want to create a dynamic list. For example, i want to read a file and fill this dyanmic list. but i should be able to delete for this list too. I know i can use dynamic array but how do i delete item from the array and resize it.

  2. #2
    Fanatic Member gwdash's Avatar
    Join Date
    Aug 2000
    Location
    Minnesota
    Posts
    666
    you can only delete elements from the back end. i.e., if you want to delete the third from the back, you'd have to delete the last three
    you just redim the array with the Preserve keyword. A Collection is much better for a single element list

    Look up collections in MSDN
    GWDASH
    [b]VB6, Perl, ASP, HTML, JavaScript, VBScript, SQL, C, C++, Linux , Java, PHP, MySQL, XML[b]

  3. #3
    _______ HeSaidJoe's Avatar
    Join Date
    Jun 1999
    Location
    Canada
    Posts
    3,946

    <?>

    Code:
    'this is rough but it should give you the idea
    'you need.
    
    Option Explicit
    
    Private Sub Quit_Click()
    'load your new array and save it to your file
    'on next open you have your adjusted list
    
        Dim myArr()
        Dim i As Integer
        Dim intNum As Integer
        intNum = FreeFile
        
        Dim myFile As String
        myFile = "C:\my documents\myfile.txt"
        
        Open myFile For Output As intNum
          For i = 0 To List1.ListCount - 1
          List1.ListIndex = i
          ReDim Preserve myArr(i)
            myArr(i) = List1.Text
            Next i
            
        For i = LBound(myArr) To UBound(myArr)
           Print #intNum, myArr(i)
        Next i
        Close intNum
        
        Unload Me
    End Sub
    
    Private Sub Form_Load()
    'open your file and load your array and listbox
        Dim myArr()
        Dim i As Integer
        Dim intNum As Integer
        intNum = FreeFile
        Dim myFile As String
        myFile = "C:\my documents\myfile.txt"
        
        Open myFile For Input As intNum
          Do While Not EOF(intNum)
          ReDim Preserve myArr(i)
            Line Input #intNum, myArr(i)
              i = i + 1
          Loop
           Close intNum
        For i = LBound(myArr) To UBound(myArr)
           List1.AddItem myArr(i)
        Next i
    
    End Sub
    
    Private Sub List1_DblClick()
    'delete the item from the list
        List1.RemoveItem (List1.ListIndex)
    End Sub
    "A myth is not the succession of individual images,
    but an integerated meaningful entity,
    reflecting a distinct aspect of the real world."

    ___ Adolf Jensen

  4. #4
    Hyperactive Member
    Join Date
    Jun 2000
    Location
    Auckland, NZ
    Posts
    411

    Linked LIst

    What you really want here is a linked list. A Linked list
    means you do not have the overhead of shifting array
    elements each time one in the middle is removed.

    You can implement a linked list any number of ways. Using
    classes is a good way because it allows you to totally
    encapsulate all the code for accessing the list. I've put
    a sample in here.

    The clsElement class provides the payload of the list.
    That is, the mData element is what we are trying to store.
    The mNext and mPrev just allow us to know our place in the
    list.

    The clsList class keeps track of the head and tail of the
    list, and makes sure that any adds or removes are handled
    properly.

    You would need to add an accessor to the class to make it
    useful of course. Generally, you would provide an accessor
    to get you to a specific element (an index) or if the list
    was a queue or stack, the accessor would provide you the
    next element (the head or tail depending on what it is).

    It is a wise move to go to a structure like this (even
    though my implementation written in 20 minutes late on
    Sunday night might not be the best for you) because it is a
    very very common structure in OO programming.

    Code:
    'clsElement
    Option Explicit
    Dim mNext As clsElement
    Dim mPrev As clsElement
    Dim mData As Variant
    Public Property Set NextElement(anElement As clsElement)
      Set mNext = anElement
    End Property
    Public Property Set PrevElement(anElement As clsElement)
      Set mPrev = anElement
    End Property
    Public Property Get NextElement() As clsElement
      Set NextElement = mNext
    End Property
    Public Property Get PrevElement() As clsElement
      Set PrevElement = mPrev
    End Property
    
    Public Property Let Data(vdata As Variant)
      ' this could be anything you want.  If it is an object,
      ' then change this to a set method and change the Get as well
      ' you can also have more members to depict more data
      mData = vdata
    End Property
    Public Property Get Data() As Variant
      Data = mData
    End Property

    Code:
    'clsList
    Option Explicit
    
    Dim mHead As clsElement
    Dim mTail As clsElement
    
    Public Function Add(vdata As Variant) As clsElement
      ' add method to create and insert a new element
      ' to implement a sorted list, simply find the correct place
      ' in the list to place the new item
      Dim anElement As clsElement
      Dim tmp As clsElement
      
      Set anElement = New clsElement
      anElement.Data = vdata
      If mHead Is Nothing Then
        ' must be the first element
        Set mHead = anElement
        Set mTail = anElement
      Else
        ' not the first, so we'll add it to the tail end
        Set mTail.NextElement = anElement
        Set anElement.PrevElement = mTail
        Set mTail = anElement
      End If
      
      Set Add = anElement
    End Function
    Public Function Remove(anElement As clsElement)
      ' simple remove method to remove an existing element
      Dim tmp As clsElement
      If anElement.PrevElement Is Nothing Then
        ' no previous element - must be the head
        Set mHead = anElement.NextElement
        Set mHead.PrevElement = Nothing
      ElseIf anElement.NextElement Is Nothing Then
        ' no next element = must be the tail
        Set mTail = anElement.PrevElement
        Set mTail.NextElement = Nothing
      Else
        ' somewhere in the middle
        With anElement
          Set tmp = .PrevElement
          Set .PrevElement.NextElement = .NextElement
          Set .NextElement.PrevElement = .PrevElement
          Set .NextElement = Nothing
          Set .PrevElement = Nothing
        End With
        
      End If
        
    End Function
    
    
    
    Private Sub Class_Terminate()
      Dim tmp As clsElement
      Do
        Set tmp = mTail.PrevElement
        Set mTail.PrevElement = Nothing
        Set mTail = tmp
      Loop Until mTail Is Nothing
      
    End Sub
    Public Sub PrintList()
      Dim tmp As clsElement
      Set tmp = mHead
      Do
        Debug.Print tmp.Data
        Set tmp = tmp.NextElement
      Loop Until tmp Is Nothing
    End Sub
    Code:
    ' form1 with text box and 3 command buttons.
    Option Explicit
    Dim myList As clsList
    Dim lastElement As clsElement
    Dim keepElement As clsElement
    
    Private Sub Command1_Click()
      Set lastElement = myList.Add(Text1)
      myList.PrintList
    End Sub
    
    Private Sub Command2_Click()
      myList.Remove keepElement
    End Sub
    
    Private Sub Command3_Click()
      Set keepElement = lastElement
    End Sub
    
    Private Sub Form_Load()
      Set myList = New clsList
      
    End Sub
    
    Private Sub Form_Unload(Cancel As Integer)
      Set lastElement = nothng
      Set keepElement = Nothing
      Set myList = Nothing
    End Sub
    The example form is pretty weak in that it allows you to
    easily add items, but to remove an item, you need to pass a
    clsElement object. To do this, I use button 3 to
    "remember" the item we want tot delete later. Then button
    2 tries to delete the item.

    If you need more info, post a message because I am off to
    sleep..

    Cheers
    Paul Lewis

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width