VB.net Creating a class and populating it

939 Views Asked by At

Hey all I have this private class code here:

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

That I am trying to add to using this code here:

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

But I am currently getting the error from the above code:

Value of type 'LDAPemployee.Form1.Employee' cannot be converted to 'System.Collections.Generic.List(Of LDAPemployee.Form1.Employee)'.

Not sure if this is because I converted this from a C# to VB example of what? Any help would be great to solve this issue!

1

There are 1 best solutions below

0
On BEST ANSWER

if you just wanna create an instance of a class and set it's values like your title says:

    (Dim) x = New Employee

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

assuming empInfo is a list(of) you can then add your new class to that list:

empInfo.add(x)

please notice that the "with" part in this case is useless as the variables of the new class allready have the value ""

i stripped this a bit apart as it is easier to understand if you are new to vb. the short way would be empInfo.Add(New Employee With {... as Andrew Morton said in a comment above