【问题标题】:Storing Objects into a Class & Retrieving upon Request将对象存储到类中并根据请求检索
【发布时间】:2016-01-14 23:23:36
【问题描述】:

我的项目非常需要帮助。 我正在尝试将特定作业存储到一个类中,然后显示在一个列表框中。 选择列表框时,我希望将其余信息显示到文本框中。

我可以将工作添加到列表框中,报告按钮按从最早到最新对工作进行排序。 我似乎无法对显示按钮进行编码来检索其余信息。

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

我做错了什么?

我的代码:

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

【问题讨论】:

  • 您可能指的是.Text &= "Job Deadline: ...,而不是.Text = +"Job Deadlinie...。对吗?
  • 感谢那个地方,修好了。但是还是不行;[
  • 这意味着...?你得到编译器错误吗?例外?出乎意料的结果?
  • 我得到:“'System.InvalidCastException' 类型的未处理异常... ...其他信息:无法将 'System.String' 类型的对象转换为类型“
  • 您的列表框中有字符串。然后您尝试将它们投射到UserInformation。这当然行不通。将UserInformation 放入列表框中,并为输出应用适当的格式。

标签: vb.net


【解决方案1】:

底线是您将字符串值存储到 LB:

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

所以,这就是你得到的结果,使得很难将创建的字符串与它所代表的对象联系起来。

列表框和组合框可以存储对象以及字符串。简单演示:

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

将对象存储到列表框很简单:

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

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

一旦你有了这些东西的课程,你就有很多选择。您可以将它们存储到 List(Of T) 中,该 List(Of T) 可以与 List 或 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 现在将是一个 Employee 对象,这应该可以简化在其他地方显示详细信息。

为了更高效,您可以将列表用作数据源,这样您就不必添加对列表框的引用:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-02-05
    • 1970-01-01
    • 2017-03-25
    • 1970-01-01
    • 1970-01-01
    • 2013-10-15
    • 1970-01-01
    相关资源
    最近更新 更多