如何在vb中使用类添加项目到列表?

时间:2016-11-28 11:19:58

标签: vb.net

我必须将我的c#转换为vb,但它没有100%转换,我坚持用类对象添加项目。

我在此处收到错误(类型'布尔值'无法转换为' _Default.Courses')在课程列表中添加:

Public Class Courses
    Public courseName As String
    Public qualName As String
    Public providerName As String
End Class

...

While r.Read
    Dim coursename As String = r("courseName").ToString
    Dim qualname As String = r("qualName").ToString
    Dim providername As String = r("providerName").ToString
    courseList.Add(New Courses() With {
        Key.courseName = coursename,
        Key.qualName = qualname,
        Key.providerName = providername
    })
End While

...

这是原始的c#代码:

while (r.Read())
{
    string coursename = r["courseName"].ToString();
    string qualname = r["qualName"].ToString();
    string providername = r["providerName"].ToString();

    courseList.Add(new Courses
    {
        courseName = coursename,
        qualName = qualname,
        providerName = providername
    });
}

1 个答案:

答案 0 :(得分:0)

将您的代码更改为:

While r.Read
    Dim coursename As String = r("courseName").ToString
    Dim qualname As String = r("qualName").ToString
    Dim providername As String = r("providerName").ToString
    courseList.Add(New Courses() With {
        .courseName = coursename,
        .qualName = qualname,
        .providerName = providername
    })
End While