嘿,我在这里有这个私有类代码:
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(来自LDAPemployee.Form1.Employee)'。
不确定这是不是因为我将它从C#转换为VB的例子是什么?任何帮助都可以解决这个问题!
答案 0 :(得分:2)
如果你只是想创建一个类的实例并设置它的值,就像你的标题所说:
(Dim) x = New Employee
With x
.displayName = ""
.ntID = ""
.lastName = ""
.givenName = ""
.department = ""
.eMail = ""
End With
假设empInfo是一个列表(of),您可以将新类添加到该列表中:
empInfo.add(x)
请注意,在这种情况下,“with”部分是无用的,因为新类的变量已经具有值""
我剥离了这一点,因为如果你是vb的新手更容易理解。正如Andrew Morton在上面的评论中所说的那样,短暂的方式是empInfo.Add(New Employee With {...