Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/react-native/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net 函数返回true,但赋值后变为false_Vb.net_Winforms - Fatal编程技术网

Vb.net 函数返回true,但赋值后变为false

Vb.net 函数返回true,但赋值后变为false,vb.net,winforms,Vb.net,Winforms,我调用一个函数(SaveChanges)来访问我的业务/数据层,并将任何更改保存到标记为添加或更新并标记为要处理的字段。直到60多分钟前,此功能一直工作正常 发生的情况是函数突然返回false。通过代码调试后,当它点击Return时,本地布尔值设置为True。。。但当它返回到调用方法(并使用返回值)时,它现在为False 正如我所说,我一行一行地检查并调试了它,然后我给它加了一块手表。有趣的是,有一个点将布尔值设置为false,当我在该点上设置断点时,它永远不会到达 以下是函数: Private

我调用一个函数(SaveChanges)来访问我的业务/数据层,并将任何更改保存到标记为添加或更新并标记为要处理的字段。直到60多分钟前,此功能一直工作正常

发生的情况是函数突然返回false。通过代码调试后,当它点击Return时,本地布尔值设置为True。。。但当它返回到调用方法(并使用返回值)时,它现在为False

正如我所说,我一行一行地检查并调试了它,然后我给它加了一块手表。有趣的是,有一个点将布尔值设置为false,当我在该点上设置断点时,它永远不会到达

以下是函数:

Private Function SaveChanges() As Boolean

    Dim blnSaved As Boolean = True

    Dim saveableRows As List(Of DataRow) = (From d As DataRow In _listData.Tables(0).Rows _
                                            Where Not d("Marking") = "Duplicate" _
                                            And d("Process") = True _
                                            Select d).ToList()


    If saveableRows.Count > 0 Then

        For Each drRow As DataRow In saveableRows

            Dim data As IDataEntity = DataEntities.GetData(_tableName)

            If drRow("Marking") = "Update" Then

                'UPDATE
                data.UpdateItem(drRow.ItemArray)

            Else

                'ADD
                data.AddItem(drRow.ItemArray)

            End If

            If data.HasErrors Then

                blnSaved = False 'Never reaches this part ... !?!?!

            End If

        Next

    Else

        blnSaved = True

    End If

End Function
If SaveChanges() Then

        Dim frmTableView As frmTableView = New frmTableView(_listData.Tables(0).TableName)

        If Not Me.MdiParent Is Nothing Then

            frmTableView.MdiParent = Me.MdiParent

        End If

        frmTableView.Show()

        Me.Close()

    Else

        MessageBox.Show("Unable to save the list. IT has been notified.", "Save Failed", MessageBoxButtons.OK, MessageBoxIcon.Error)

    End If
下面是我如何使用该函数:

Private Function SaveChanges() As Boolean

    Dim blnSaved As Boolean = True

    Dim saveableRows As List(Of DataRow) = (From d As DataRow In _listData.Tables(0).Rows _
                                            Where Not d("Marking") = "Duplicate" _
                                            And d("Process") = True _
                                            Select d).ToList()


    If saveableRows.Count > 0 Then

        For Each drRow As DataRow In saveableRows

            Dim data As IDataEntity = DataEntities.GetData(_tableName)

            If drRow("Marking") = "Update" Then

                'UPDATE
                data.UpdateItem(drRow.ItemArray)

            Else

                'ADD
                data.AddItem(drRow.ItemArray)

            End If

            If data.HasErrors Then

                blnSaved = False 'Never reaches this part ... !?!?!

            End If

        Next

    Else

        blnSaved = True

    End If

End Function
If SaveChanges() Then

        Dim frmTableView As frmTableView = New frmTableView(_listData.Tables(0).TableName)

        If Not Me.MdiParent Is Nothing Then

            frmTableView.MdiParent = Me.MdiParent

        End If

        frmTableView.Show()

        Me.Close()

    Else

        MessageBox.Show("Unable to save the list. IT has been notified.", "Save Failed", MessageBoxButtons.OK, MessageBoxIcon.Error)

    End If
我在这上面花了太多时间,我想是时候寻求帮助了


谢谢

您永远不会返回布尔值。加:

Return blnSaved

在函数的底部。

您从不指定函数返回值。您将获得其默认值False。与其指定给局部变量(blnSaved),不如指定给SaveChanges。或者在函数末尾添加Return-blnSaved。

您不是在函数末尾缺少了一个
Return-blnSaved

我同意您只是缺少Return-blnSaved语句。我还建议您不需要下面的else语句,因为您已经在方法的开头初始化了blnSaved值

 Else

        blnSaved = True

 End If

不应该保存更改(…)有返回语句吗?哈哈哈。。。一定是被删除了。。。或者我只是个白痴。。。我将让你们大家决定:P@Tony艾布拉姆斯——或者只是稍微远离它一点,然后在它离开你的大脑后再回到它。我犯了很多次这样的错误,一点都不好笑。编译器没有警告过你吗?编译器警告是否打开:“函数/运算符不带返回值”@MarkJ-使用我自己工作室中保留的设置,如果我看错了,它会警告我。但是完全有可能建立一个宽松的环境,不会因此而抛出警告或错误(另一个原因是转移到C#通常不是一个坏主意)。。。。遗憾的是,编译器警告“没有返回值的函数/运算符”似乎没有捕获没有返回值的布尔函数。我想是因为默认返回值为False。Pity@MarkJ-既然你提到了,我相信C#也是这样。我认为int返回值的行为类似于0的返回值,但我记不起来了。我添加了额外的else作为强制为真的一种方式,因为问题已经解决,它将被删除。