I am simply trying to create an function to return a set of user. But, in order to be able to add the item later to a collection I used arrayList, but at last I am getting an error of Type ArrayList cannot be converted to 1-Dimensional array of Users
Here is my Code:
Function getDoctorsList() As Users()
Dim userCollection As New ArrayList
Dim sql = "SELECT * FROM '" + _tblName + "' WHERE usertype = 'doctor'"
Dim dr As SqlDataReader = dbHelper.ExecuteAndGetReader(sql)
While dr.Read
Dim user As New Users
user.Id = IIf(IsDBNull(dr("id")), 0, dr("id"))
user.UserName = IIf(IsDBNull(dr("username")), "", dr("username"))
user.UserNin = IIf(IsDBNull(dr("user_nin")), 0, dr("user_nin"))
user.UserType = IIf(IsDBNull(dr("usertype")), "", dr("usertype"))
user.Password = IIf(IsDBNull(dr("password")), "", dr("password"))
userCollection.Add(user)
End While
Return userCollection
End Function
How to solve such problems?
Either let your method return an
ArrayList:create an
Arrayout of yourArrayList:or, the best solution, let your method return an
IEnumerable(Of User)you should also use a
List(of User)instead of aArrayListinstead, since it is type safe (and clearer) and it also implementsIEnumerable(Of User).Edit
Example using
IEnumerable(of Users)andDataRowExtensions: