Tony Abrams
Tony Abrams

Reputation: 4673

Function returns true, but then it becomes false upon assignment

I call a function (SaveChanges) to access my business / data layer and save any changes to fields that are marked as an Add or Update and are marked to be processed. This function used to work fine up until 60+ minutes ago.

What is happening is that suddenly the function is returning false. After debugging through the code, when it hits Return the local boolean is set as True ... but when it gets back to the calling method (and uses the return value) it is now False.

As I said I went through line by line and debugged it, then I added a watch to it. The funny part is that there is one spot that sets the boolean to false, and when I put a breakpoint on that spot it is never reached.

Here is the function:

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

And here is how I'm using the 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

I'm spending too much time on this and figured it was time to look for help.

Thanks

Upvotes: 0

Views: 21572

Answers (4)

dretzlaff17
dretzlaff17

Reputation: 1719

I agree that you are simply missing Return blnSaved statement. I would also suggest that you do not need the following else statement because you are already initializing the blnSaved value at the start of the method.

 Else

        blnSaved = True

 End If

Upvotes: 1

Meta-Knight
Meta-Knight

Reputation: 17875

Aren't you just missing a Return blnSaved at the end of your function?

Upvotes: 1

Hans Passant
Hans Passant

Reputation: 942358

You never assign the function return value. You'll get its default value, False. Instead of assigning to the local variable (blnSaved), assign to SaveChanges instead. Or add Return blnSaved at the end of the function.

Upvotes: 1

Joel Etherton
Joel Etherton

Reputation: 37543

You're never returning your boolean. Add:

Return blnSaved

at the bottom of the function.

Upvotes: 6

Related Questions