Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
732 views
in Technique[技术] by (71.8m points)

vb.net - Storing Objects into a Class & Retrieving upon Request

I seriously need help with my project. I am trying to store specific jobs into a Class, which then displays in a List Box. When selecting the List Box, I want the rest of the information to be displayed into a Text Box.

I can add Jobs into the List Box, and the Report button sorts the Job by Earliest to Latest. I just CANNOT seem to code the Display Button to retrieve the rest of the information.

http://i.stack.imgur.com/0eV5j.png

What am I doing wrong?

My Code:

Public Class Form1

Dim jobList As List(Of UserInformation) = New List(Of UserInformation)
Dim j As UserInformation = New UserInformation()
Private Sub btnReport_Click(sender As Object, e As EventArgs) Handles btnReport.Click

    Dim p As UserInformation = New UserInformation()
    Dim qty As Integer = jobList.Count - 1

    Dim name(qty) As String

    Dim deadline(qty) As Date

    Dim i As Integer = 0

    'fill the array

    For i = 0 To qty

        p = jobList(i)

        name(i) = p.Name

        deadline(i) = p.Deadline

    Next

    'sort the array

    Dim done As Boolean = False

    While done = False

        done = True

        For i = 0 To qty - 1

            Dim tempName As String

            Dim tempDate As Date

            If deadline(i) > deadline(i + 1) Then

                tempName = name(i)

                tempDate = deadline(i)

                name(i) = name(i + 1)

                deadline(i) = deadline(i + 1)

                name(i + 1) = tempName

                deadline(i + 1) = tempDate

                done = False

            End If

        Next

    End While

    lsbReport.Items.Clear()

    lblListbox.Text = "List in date order"

    For i = 0 To name.Length - 1

        Dim str As String

        str = name(i) + ",  "

        str += deadline(i).ToString + "."

        lsbReport.Items.Add(str)

    Next

End Sub

Private Sub updateListBox()

    lsbReport.Items.Clear()

    lblListbox.Text = "All people in the List"

    For Each person As UserInformation In jobList

        Dim str As String

        str = person.Name + ",  "

        str += person.Deadline.ToString + "."

        lsbReport.Items.Add(str)

    Next

End Sub

Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click

    Dim p As UserInformation = New UserInformation()

    p.Name = firstNameText.Text

    p.Deadline = lastNameText.Value

    jobList.Add(p)

    updateListBox()

End Sub

Private Sub btnDisplay_Click(sender As Object, e As EventArgs) Handles btnDisplay.Click
    Dim job_info As UserInformation = CType(lsbReport.SelectedItem(), UserInformation)
    txtReport.Text = "Job Title: " & job_info.Name() & Environment.NewLine
    txtReport.Text &="Job DeadLine: " & job_info.Deadline & Environment.NewLine
    txtReport.Text &="Job Description" & job_info.Description

End Sub
End Class




Public Class UserInformation
Public job_deadline As Date
Public job_name As String
Public job_description As String
Public Property Name() As String
    Get
        Return job_name
    End Get
    Set(ByVal value As String)
        job_name = value
    End Set
End Property
Public Property Deadline() As String
    Get
        Return job_deadline
    End Get
    Set(ByVal value As String)
        job_deadline = value
    End Set
End Property

Public Property Description() As String
    Get
        Return job_description
    End Get
    Set(ByVal value As String)
        job_description = value
    End Set
End Property
End Class
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The bottom line is that you stored string values to the LBs:

    str = person.Name & ",  " & person.Deadline.ToString 
    lsbReport.Items.Add(str)

So, that is what you get back making it difficult to connect the string created with the object it represents.

Listboxes and comboboxes can store objects as well as strings. Simple Demo:

Public Class Employee
    Public Property ID As Integer      ' a db ID maybe
    Public Property Name As String
    Public Property Department As String

    Public Property HireDate As Date

    Public Overrides Function ToString As String
        Return Name                 ' text to show in controls

        ' in a more realistic class it might be:
        ' Return String.Format("{0}, {1} ({2})", LastName,
        '                 FirstName, BadgeNumber)
        ' e.g. "Whitman, Ziggy (123450)"
    End Function
End Class

Friend EmpList As New List(of Employee)      ' if needed

storing an object to a listbox is simple:

Dim newEmp As Employee
newEmp.Name = "Ziggy"      
' set props as needed

myLB.Items.Add(newEmp)         ' add to a ListBox directly.

Once you have a class for these things, you have many options. You can store them to a List(Of T) which can be used with a List or ComboBox:

Private EmpList As New List(Of Employee)
...
EmpList.Add(newEmp)             ' adding to a list is same as a ListBox 

' add from List to a control:

myLB.Items.AddRange(Emps.ToArray)

myLB.SelectedItem will now be an Employee object, which should make displaying details elsewhere simple.

To make it even more efficent, you can use the list as a DataSource so you dont have to add references to the listbox:

myLB.DataSource = EmpList
myLB.DisplayMember = "Name"      ' the property to display
myLB.ValueMember = "Id"          ' what to use for SelectedValue

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...