【问题标题】:VB.net Creating a class and populating itVB.net 创建一个类并填充它
【发布时间】:2015-01-23 23:29:14
【问题描述】:

大家好,我这里有这个私人课程代码:

Private Class Employee
    Public displayName As String
    Public ntID As String
    Public lastName As String
    Public givenName As String
    Public department As String
    Public eMail As String
End Class

我正在尝试添加到这里使用此代码:

empInfo = New Employee With { _
     .displayName = "", _
     .ntID = "", _
     .lastName = "", _
     .givenName = "", _
     .department = "", _
     .eMail = "" _
   }
}

但我目前从上面的代码中得到了错误:

“LDAPemployee.Form1.Employee”类型的值无法转换为“System.Collections.Generic.List(Of LDAPemployee.Form1.Employee)”。

不确定这是不是因为我将它从 C# 转换为 VB 示例?任何帮助都可以解决这个问题!

【问题讨论】:

  • 真的不需要所有空的 With 道具设置器,因为他们什么都不做
  • empInfo 必须是 List(Of Employee),而不是 Employee
  • empInfo.Add(New Employee With {... 是您要查找的内容,假设 empInfoList(Of Employee)

标签: vb.net list class arraylist populate


【解决方案1】:

如果你只是想创建一个类的实例并设置它的值,就像你的标题所说的那样:

    (Dim) x = New Employee

    With x
        .displayName = ""
        .ntID = ""
        .lastName = ""
        .givenName = ""
        .department = ""
        .eMail = ""
    End With

假设 empInfo 是一个列表,然后您可以将新类添加到该列表中:

empInfo.add(x)

请注意,这种情况下的“with”部分是无用的,因为新类的变量已经具有值""

我将其剥离了一点,因为如果您是 vb 新手,则更容易理解。正如安德鲁莫顿在上面的评论中所说的那样,捷径是empInfo.Add(New Employee With {...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-08-06
    • 1970-01-01
    • 2012-06-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多