vb.net 收到错误“表达式不产生值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22124861/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 16:57:30  来源:igfitidea点击:

Getting the error "Expression does not produce a value

vb.net

提问by Xero

I am relatively new to using visual basic and I am having a problem populating a list box from a database. The error that comes up is Expression does not produce a value.

我对使用 Visual Basic 比较陌生,并且在从数据库填充列表框时遇到问题。出现的错误是 Expression 没有产生值。

Here is the code from My form:

这是我的表单中的代码:

Private Sub frmMain_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    'Populate Blu Ray and DVD listboxes

    Dim objMovies As New clsMovies

    objMovies.Select_BR_List()
    objMovies.Select_Dvd_List()

    For Each strBluRay As String In objMovies.Select_BR_List
        lstBluRay.Items.Add(strBluRay)
    Next

    For Each strDVD As String In objMovies.Select_Dvd_List
        lstDvd.Items.Add(strDVD)
    Next
End Sub

And here's the code from the class:

这是课程中的代码:

Public Sub Select_Dvd_List()
    Dim objConnection As New SqlCeConnection(mstrCN)

    'Create SQL statement
    mstrSQL = "Select * from Dvd"
    'Instantiate command
    Dim objCommand As New SqlCeCommand(mstrSQL, objConnection)

    'open Database
    objCommand.Connection.Open()

    'Instantiate Data Reader
    Dim objDataReader As SqlCeDataReader

    'Execute SQL
    objDataReader = objCommand.ExecuteReader()

    'read Sql results
    Do While (objDataReader.Read)
        mlstDvd.Add(objDataReader.Item("dvdTitle").ToString)
    Loop

    'Close 
    objCommand.Dispose()
    objDataReader.Close()
    objDataReader.Dispose()
    objConnection.Close()
    objConnection.Dispose()
End Sub

回答by jmcilhinney

The issue is that you're trying to enumerate a Sub. In VB.NET, methods can either a Sub, which doesn't return a value, or a Function, which does return a value. Your Select_Dvd_Listmethod is a Sub, so it doesn't return a value. You have this code though:

问题是您正在尝试枚举一个 Sub。在 VB.NET 中,方法可以是不返回值的 Sub,也可以是返回值的 Function。您的Select_Dvd_List方法是一个 Sub,因此它不返回值。你有这个代码:

For Each strDVD As String In objMovies.Select_Dvd_List

That is trying to loop through the result of Select_Dvd_Listbut, as we've already established, Select_Dvd_Listhas no result. In that method, you are adding items to mlstDvdso surely that loop should be:

那是试图遍历Select_Dvd_Listbut的结果,正如我们已经确定的那样,Select_Dvd_List没有结果。在该方法中,您将项目添加到mlstDvd如此肯定的循环应该是:

For Each strDVD As String In objMovies.mlstDvd