Results 1 to 3 of 3

Thread: Data Structure Problems

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2004
    Posts
    11

    Data Structure Problems

    Code im using is

    Public Structure pupil
    Dim name As String
    Dim age As Int16
    Dim id As Int16
    Dim pupilClass As String
    End Structure

    Public Structure pupilList
    Dim list() As pupil
    Dim count
    End Structure

    Dim ListData As New pupilList()
    then to try and access the pupil name im using

    MsgBox(ListData.list(ListData.count).age)
    it give me a error saying that the last line is not set to a instance of the object?

    any help would be great on how i can update/desplay the name,age etc , cheers!

  2. #2
    Lively Member
    Join Date
    Sep 2002
    Location
    Belgium
    Posts
    99

    Re: Data Structure Problems

    Just an idea .... maybe you need to set the count to int16 as well ?

  3. #3
    Frenzied Member <ABX's Avatar
    Join Date
    Jul 2002
    Location
    Canada eh...
    Posts
    1,622
    First of all, since pupilList.List is an array it is zero based so

    VB Code:
    1. MsgBox(ListData.list(ListData.count).age)

    would refer to a item that is not in the array

    Second, The way you set it up you would have to do the following before adding anything

    VB Code:
    1. 'to get it to work the way you you have it setup now..
    2.  
    3.         ReDim ListData.list(0)
    4.         ListData.count = 1
    5.  
    6.         ListData.list(0) = New pupil
    7.  
    8.         MsgBox(ListData.list(ListData.count - 1).age)

    The way I would do it...

    VB Code:
    1. Option Explicit On
    2.  
    3. Public Class cPupilCollection
    4.     Inherits System.Collections.CollectionBase
    5.  
    6.     ' Restricts to cPupil types, items that can be added to the collection.
    7.     Public Sub Add(ByVal acPupil As cPupil)
    8.         ' Invokes Add method of the List object to add a cPupil.
    9.         List.Add(acPupil)
    10.     End Sub
    11.  
    12.     Public Sub Remove(ByVal index As Integer)
    13.         ' Check to see if there is a cPupil at the supplied index.
    14.         If index > Count - 1 Or index < 0 Then
    15.             ' If no cPupil exists, a messagebox is shown and the operation is
    16.             ' cancelled.
    17.             System.Windows.Forms.MessageBox.Show("Index not valid!")
    18.         Else
    19.             ' Invokes the RemoveAt method of the List object.
    20.             List.RemoveAt(index)
    21.         End If
    22.     End Sub
    23.  
    24.     ' This line declares the Item property as ReadOnly, and
    25.     ' declares that it will return a cPupil object.
    26.     Public ReadOnly Property Item(ByVal index As Integer) As cPupil
    27.         Get
    28.             ' The appropriate item is retrieved from the List object and
    29.             ' explicitly cast to the cPupil type, then returned to the
    30.             ' caller.
    31.             Return CType(List.Item(index), cPupil)
    32.         End Get
    33.     End Property
    34.  
    35. End Class

    VB Code:
    1. Option Explicit On
    2.  
    3. Public Class cPupil
    4.  
    5.     Private m_strName As String
    6.     Private m_sAge As Short ' Short = Int16
    7.     Private m_sID As Short
    8.     Private m_strPupilClass As String
    9.  
    10.     Public Property PupilName() As String
    11.         Get
    12.             Return m_strName
    13.         End Get
    14.         Set(ByVal Value As String)
    15.             m_strName = Value
    16.         End Set
    17.     End Property
    18.  
    19.     Public Property Age() As Short
    20.         Get
    21.             Return m_sAge
    22.         End Get
    23.         Set(ByVal Value As Short)
    24.             m_sAge = Value
    25.         End Set
    26.     End Property
    27.  
    28.     Public Property ID() As Short
    29.         Get
    30.             Return m_sID
    31.         End Get
    32.         Set(ByVal Value As Short)
    33.             m_sID = Value
    34.         End Set
    35.     End Property
    36.  
    37.     Public Property PupilClass() As String
    38.         Get
    39.             Return m_strPupilClass
    40.         End Get
    41.         Set(ByVal Value As String)
    42.             m_strPupilClass = Value
    43.         End Set
    44.     End Property
    45.  
    46. End Class

    VB Code:
    1. 'usage:
    2.  
    3.  
    4. Dim Test as New PupilCollection
    5.  
    6. Dim  NewPupil as New Pupil
    7.  
    8. NewPupil.PupilName = "Bob"
    9. NewPupil.Age = 10
    10.  
    11. Test.Add(NewPupil)
    Tips:
    • Google is your friend! Search before posting!
    • Name your thread appropriately... "I Need Help" doesn't cut it!
    • Always post your code!!!! We can't read your mind!!! (well, at least most of us!)
    • Allways Include the Name and Line of the Exception (if one is occuring!)
    • If it is relevant state the version of Visual Studio/.Net Framwork you are using (2002/2003/2005)


    If you think I was helpful, rate my post
    IRC Contact: Rizon/xous ChakraNET/xous Freenode/xous

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