JavaScript Editor JavaScript Editor     JavaScript Debugger

Previous Section Next Section

Main Page

An OOP Example

Examples always help, and in fact, we've already seen an example that puts together many of the aspects of OOP in Visual Basic, including custom classes and objects, fields, methods, constructors, and so on in Chapter 7 (see "Storing Objects in a List Box or Combo Box" in that chapter). This example, ComboBoxesData, stores objects in a combo box, and each object holds its name and index in the combo box. When the user selects an item in the combo box, the code recovers the data from the object corresponding to the selected item, and displays that data.

To make this work, I created a class named DataItem, which used a New constructor to store each item's name and index value as internal, private data:

Public Class DataItem
    Private Data As Single
    Private Name As String

    Public Sub New(ByVal NameArgument As String, ByVal Value As Single)
        Name = NameArgument
        Data = Value
    End Sub
        
End Class

I also added two methods to this class—ToString, which returns the name of the item (and which actually overrides the ToString method built into the Object class, which is the ultimate base class of every class), and GetData, which returns the index value of the item:

Public Class DataItem
    Private Data As Single
    Private Name As String

    Public Sub New(ByVal NameArgument As String, ByVal Value As Single)
        Name = NameArgument
        Data = Value
    End Sub

    Overrides Function ToString() As String
        Return CStr(Name)
    End Function

    Public Function GetData() As Single
        Return Data
    End Function
End Class

I created 20 objects of this class and placed them into a combo box with the combo box's AddRange method this way when the program's form first loaded:

Public Class Form1
    Inherits System.Windows.Forms.Form

    'Windows Form Designer generated code

    Private Sub Form1_Load(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles MyBase.Load
        Dim Objects(20) As DataItem
        ComboBox1.BeginUpdate()
        Dim intLoopIndex As Integer
        For intLoopIndex = 0 To 20
            Objects(intLoopIndex) = New DataItem("Item " & intLoopIndex, _
            CSng(intLoopIndex))
        Next
        ComboBox1.Items.AddRange(Objects)
        ComboBox1.EndUpdate()
    End Sub

Then, when the user changed the selection in the combo box, I recovered the selected item with the combo box's SelectedItem property, and used the GetData method we have given that item to recover the item's internal data:

Public Class Form1
    Inherits System.Windows.Forms.Form

    'Windows Form Designer generated code

    Private Sub Form1_Load(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles MyBase.Load
        Dim Objects(20) As DataItem
        ComboBox1.BeginUpdate()
        Dim intLoopIndex As Integer
        For intLoopIndex = 0 To 20
            Objects(intLoopIndex) = New DataItem("Item " & intLoopIndex, _
            CSng(intLoopIndex))
        Next
        ComboBox1.Items.AddRange(Objects)
        ComboBox1.EndUpdate()
    End Sub

    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As _
        System.Object, ByVal e As System.EventArgs) Handles _
        ComboBox1.SelectedIndexChanged
        MsgBox("The data for the item you selected is: " & _
            CType(ComboBox1.SelectedItem, DataItem).GetData())
    End Sub
End Class

As you see, now we can understand all that's going on in this code. You can see this example, ComboBoxData, at work in Figure 7.15 in Chapter 7.

Previous Section Next Section




JavaScript Editor Free JavaScript Editor     JavaScript Editor