class containing another class

Hi, I'm new to OOP and am having problems creating a class that contains a collection of items from another class. ie I have a LabelTemplate class that represents a label to printed from a Thermal label printer. My LabelTemplate class has a set of properties like label size, labelName,barcode string etc.I also have a class, "PrintFields", that contains properties for a dynamic number of different items to be printed on the label.  I want to have this class contain and access a collection of PrintField objects. Any ideas

Regards
         Kev



Answer this question

class containing another class

  • buladbanaw

    Hi,


    To have a collection of class in your class then try this pseudo-code:


    Public Class PrintField
       Public fieldValues as String
    End Class

    Public Class LabelTemplate
       Private fieldValues As New Collection

       Public Function getfieldValue(index As Integer) As PrintField
          Dim retVal As PrintField
          retVal = fieldValues(index)
          ' return the value
       End Function

       public Sub setFieldValue(f As PrintField)
          ' Add the new value in the collection
       End Sub
    End Class
    ' Also implement the Remove() function


    Or you could just simply expose your collection by making it public...




    cheers,


    Paul June A. Domag

  • Andrea_A

    Thanks Paul, I just aboutr have it, I think .
    The problem is Button3 click causes an error.Would you mind helping, I guess the answer must be obvious but I can't see it.

    Public Class PrintField
        Private xposValue As Integer
        Public Property xpos() As Integer
            Get
                Return xposValue
            End Get
            Set(ByVal value As Integer)
                xposValue = value
            End Set
        End Property

        Private yposValue As Integer
        Public Property ypos() As Integer
            Get
                Return yposValue
            End Get
            Set(ByVal value As Integer)
                yposValue = value
            End Set
        End Property
    End Class

    Public Class LabelTemplate
        Private PrintFields As New Collection   

        Public Function getfldValue(ByVal index As Integer) As PrintField
            Dim retVal As PrintField
            retVal = CType(PrintFields(index), PrintField)
            ' return the value
        End Function

        Public Sub setFldValue(ByVal f As PrintField)
            PrintFields.Add(f)
        End Sub

        Private FieldcountValue As Integer
        Public ReadOnly Property Fieldcount() As Integer
            Get
                FieldcountValue = PrintFields.Count
                Return FieldcountValue
            End Get
        End Property
    End Class

    Public Class Form3
        Private anitem As New PrintField
        Private nextitem As New PrintField
        Private mylabel As New LabelTemplate

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            anitem.xpos = 1111
            anitem.ypos = 2222
            mylabel.setFldValue(anitem)
            nextitem.xpos = 3333
            nextitem.ypos = 4444
            mylabel.setFldValue(nextitem)
        End Sub

        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            MsgBox(mylabel.getfldValue(0).xpos.ToString)
        End Sub

        Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
            'This causes an error
            MsgBox(mylabel.getfldValue(1).ypos.ToString)

        End Sub

        Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
            MsgBox(mylabel.Fieldcount.ToString)  'This works
        End Sub
    End Class



  • jasMSDN

    There's 2 ways you can do this.

    1, is to inherit the class.
    ------------------------
    Example.
    Public Class Drink
       Public sub Drink()
       End Sub

       Public sub Refill()
       End sub
    End Class

    Public class Coffee
       Inherits Drink

       Public Sugars as Integer
       Public Creams as Integer

       Public Sub GetCoffee()
          Refill()
       End sub

       Public Sub DrinkCoffee()
          Drink()
       End sub
    End Class
    --------------------------------

    The other option, is to make a local variable of the class.

    Exmaple.
    Public Class Drink
       Public sub Drink()
       End Sub

       Public sub Refill()
       End sub
    End Class

    Public class Coffee
       private MyDrink as Drink = New Drink

       Public Sugars as Integer
       Public Creams as Integer

       Public Sub GetCoffee()
          MyDrink.Refill()
       End sub

       Public Sub DrinkCoffee()
          MyDrink.Drink()
       End sub
    End Class

  • Mr PoPoP

    Hi,


    In your getFldValue() function, what I meant by return the value was declaring Return retVal coz at that time I was not sure on the return keyword in VB...







    cheers,


    Paul June A. Domag

  • class containing another class