Results 1 to 3 of 3

Thread: [RESOLVED] Add objects to a class

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Nov 2005
    Posts
    29

    Resolved [RESOLVED] Add objects to a class

    I have defined a class:

    Public Class articleOrder
    Public myError As Boolean
    Public myErrorMessage As String
    Public articles() As articleAnswers
    End Class

    Public Class articleAnswers
    Public articleNumber As String
    Public quantity As Decimal
    End Class

    How could I add multiple articleAnswers to articleOrder.articles?

  2. #2
    Frenzied Member FishGuy's Avatar
    Join Date
    Mar 2005
    Location
    Bradford UK
    Posts
    1,708

    Re: Add objects to a class

    You need to use a collection class, lookup the innerlist and collection Base properties.
    Heres a couple of classes I did this with Carraiages and the ability to add multiple characters to the Collection class
    using
    VB Code:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.  
    3.         objCarriageCollection.Add(New Carriage(1, 2.4, "Pullman"))
    4.         objCarriageCollection.Add(New Carriage(2, 2.3, "Pullman"))
    5.         objCarriageCollection.Add(New Carriage(2, 2.3, "Topless"))
    6.         objCarriageCollection.Add(New Carriage(4, 3.0, "6Wheels"))
    VB Code:
    1. Public Class Carriage
    2.  
    3.     Private FCarriageNo As Integer
    4.     Private FCarriageWeight As Decimal
    5.     Private FCarriageModel As String
    6.     ' Add the Default Constructor
    7.     Public Sub New()
    8.         ' Default Constructor without parameter
    9.     End Sub
    10.     Public Sub New(ByVal CarriageID As Integer, ByVal CarriageWeight As Decimal, ByVal CarriageModel As String)
    11.         FCarriageNo = CarriageID
    12.         FCarriageWeight = CarriageWeight
    13.         FCarriageModel = CarriageModel
    14.     End Sub
    15.     Public Property CarriageID() As Integer
    16.         Get
    17.             Return FCarriageNo
    18.         End Get
    19.         Set(ByVal Value As Integer)
    20.             FCarriageNo = Value
    21.         End Set
    22.     End Property
    23.     Public Property pCarriageWeight() As Decimal
    24.         Get
    25.             Return FCarriageWeight
    26.         End Get
    27.         Set(ByVal Value As Decimal)
    28.             FCarriageWeight = Value
    29.         End Set
    30.     End Property
    31.     Public Property pCarriageModel() As String
    32.         Get
    33.             Return FCarriageModel
    34.         End Get
    35.         Set(ByVal Value As String)
    36.             FCarriageModel = Value
    37.         End Set
    38.     End Property
    39.  
    40.  
    41.  
    42. End Class
    VB Code:
    1. Public Class CarriageCollection
    2.     Inherits CollectionBase
    3.  
    4.     Default Public Overridable Property Item(ByVal index As Integer) As Carriage
    5.         Get
    6.             'Here you should really catch any exceptions, like ArgumentOutOfRangeExcetion,
    7.             'and then rethrow as an InnerException to your own custom exception.
    8.             Return DirectCast(Me.InnerList.Item(index), Carriage)
    9.         End Get
    10.         Set(ByVal Value As Carriage)
    11.             Me.InnerList.Item(index) = Value
    12.         End Set
    13.     End Property
    14.  
    15.     Public Overridable Function Add(ByVal value As Carriage) As Integer
    16.         Return Me.InnerList.Add(value)
    17.     End Function
    18.  
    19.     Public Overridable Sub AddRange(ByVal c As ICollection)
    20.         For Each i As Object In c
    21.             If Not TypeOf i Is Carriage Then
    22.                 'Here you should really throw your own custom exception.
    23.                 Throw New ArgumentException("All items added to a CarriageCollection must be of type Carriage.", "c")
    24.             End If
    25.  
    26.             Me.InnerList.AddRange(c)
    27.         Next
    28.     End Sub
    29.  
    30.     Public Overridable Function Contains(ByVal item As Carriage) As Boolean
    31.         Return Me.InnerList.Contains(item)
    32.     End Function
    33. End Class
    Last edited by FishGuy; Nov 14th, 2005 at 05:35 AM.

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Nov 2005
    Posts
    29

    Re: Add objects to a class

    Defining the array as NewArray() as myObject, and define every instance like:

    dim NewArray(1) as NEW myObject

    Would work... thanks

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