If statement where both conditions get used

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 785 Views - Last Post: 11 October 2011 - 09:42 AM Rate Topic: -----

#1 Tildha  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 11-October 11

If statement where both conditions get used

Posted 11 October 2011 - 07:35 AM

Hello,
I have an if statement that is driving me crazy, and I suspect it's something really simple I have missed.

As you can see below, the testBoolean is set to the value True, but for some reason when debugging, it step through both conditions, so in the end the button1.visble becomes False, but I want it to be set to True.


Dim testBoolean As Boolean = True

If testBoolean Then
  button1.Visible = True
Else
  button1.Visible = False
End If




If anyone could explain why, I'd be very greatful.

Many thanks,

This post has been edited by modi123_1: 11 October 2011 - 07:36 AM
Reason for edit:: fixed botched code tags


Is This A Good Question/Topic? 0
  • +

Replies To: If statement where both conditions get used

#2 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 8915
  • View blog
  • Posts: 33,430
  • Joined: 12-June 08

Re: If statement where both conditions get used

Posted 11 October 2011 - 07:38 AM

Are you certain that's all your code? That button visibility isn't being set elsewhere?

Side note, you can just straight up assign the value to the button's property.
  button1.Visible = testBoolean 

Was This Post Helpful? 0
  • +
  • -

#3 Tildha  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 11-October 11

Re: If statement where both conditions get used

Posted 11 October 2011 - 07:47 AM

Thanks for quick reply.

Well, this is the exact code snippet, but I could not see that it would make a difference as the value would return True on FGetReadPNR.

<Code>
    Private Sub sSetUpBranchVisabilities()
        If objSQLServ_DAL.fGetReadPNR(cboHandlingBranches.Text.Trim) Then
            cmdReadPNR.Visible = True
        Else
            cmdReadPNR.Visible = False
        End If

    End Sub

</Code)

The cmdReadPNR does not have any properties set from anywhere else, apart from the default Visible property set to True in the Properties window.

This post has been edited by modi123_1: 11 October 2011 - 07:55 AM
Reason for edit:: fixed botched code tags

Was This Post Helpful? 0
  • +
  • -

#4 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 8915
  • View blog
  • Posts: 33,430
  • Joined: 12-June 08

Re: If statement where both conditions get used

Posted 11 October 2011 - 07:56 AM

... and you are certain "objSQLServ_DAL.fGetReadPNR" is returning right?

Was this some old VB6 app?
Was This Post Helpful? 0
  • +
  • -

#5 Tildha  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 11-October 11

Re: If statement where both conditions get used

Posted 11 October 2011 - 08:04 AM

Yes, I'm certain it returns True, as I can change the value it should return in the database, but also because I tested this with the first code snippet, when I didn't believe what I saw.

If the function returns False, it steps right to after 'Else' which is correct.

It's an vb.net (VS 2005) app that I'm doing some updates to, but it has got old vb6 naming converions.
Was This Post Helpful? 0
  • +
  • -

#6 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 8915
  • View blog
  • Posts: 33,430
  • Joined: 12-June 08

Re: If statement where both conditions get used

Posted 11 October 2011 - 08:10 AM

... and you are sure the return type for "fGetReadPNR" is boolean, right? Not integer, string, or float?
Was This Post Helpful? 0
  • +
  • -

#7 Tildha  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 11-October 11

Re: If statement where both conditions get used

Posted 11 October 2011 - 08:16 AM

Yep.

<CODE>
    Public Function fGetReadPNR(ByVal strHandlingBranch As String) As Boolean
        ' Created in v3.0.0
        Dim connectionString As String = "XXXXXXXXXXXXXXXXXXXXXXXXXXXX"
        Dim SQLcon As New SqlConnection(connectionString)
        Dim ReadPNR As Boolean
        Dim SQLCmd As New SqlCommand("GetReadPNR_v1", SQLcon)    ' Name of stored procedure

        SQLCmd.CommandType = CommandType.StoredProcedure
        SQLCmd.Parameters.AddWithValue("@CallHandlBranch", CObj(strHandlingBranch))
        SQLcon.Open()
        ReadPNR = CBool(SQLCmd.ExecuteScalar)
        SQLcon.Close()
        Return ReadPNR
    End Function

</CODE>

I seem to get it wrong when I paste the code. Sorry about that...

This post has been edited by modi123_1: 11 October 2011 - 08:18 AM
Reason for edit:: highlight code, click '[code]' button, profit.

Was This Post Helpful? 0
  • +
  • -

#8 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 8915
  • View blog
  • Posts: 33,430
  • Joined: 12-June 08

Re: If statement where both conditions get used

Posted 11 October 2011 - 08:26 AM

Hmm.. I don't know. Maybe the SQL call is crashing? Side note - you really should be wrapping your SQL calls in a try/catch. Side side note - I am not sure why you are casting a string as an object for the 'addwithvalue'.

Not knowing how that stored procedure works - you are aware a 'execute scalar' can return "nothing", right? That would be a no-no. Usually I would avoid direct casting the first column for the first row to a bool right out of the gate. Especially when dealing with PNRs.

Try tossing a try/catch and see what that fishes up.
Was This Post Helpful? 0
  • +
  • -

#9 Tildha  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 11-October 11

Re: If statement where both conditions get used

Posted 11 October 2011 - 08:41 AM

Well, I'm aware of that the function that calls the SQL might not be the best constructed, it's something I've "inherited" and never spent any time on working on really. (Which I probably should do..) But, I don't think that's the issue here, and also the reason why I posted the easy code snippet on top in the first place. Here it's clear that it is True as boolean that is in the variable that is being tested, and it still errors out.

In the meantime since my last post, I have worked the following out, while trying to debug it:

    Private Sub sSetUpBranchVisabilities()
        If objSQLServ_DAL.fGetReadPNR(cboHandlingBranches.Text.Trim) Then
            cmdReadPNR.Visible = True
            MessageBox.Show("True")
        Else
            cmdReadPNR.Visible = False
            MessageBox.Show("False")
        End If

        Dim newbool As Boolean
        newbool = cmdReadPNR.Visible
      End Sub



By adding the Message boxes and then the new boolean in the bottom, I though that would make things easier to debug - and for some reason now when the function returns True it skipps the Else statement just as it should do.

But If I go in and remove the two newbool lines, then it steps through both again.

I guess this would mean, I have solved my issue, but I haven't got a clue why...
Was This Post Helpful? 0
  • +
  • -

#10 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 8915
  • View blog
  • Posts: 33,430
  • Joined: 12-June 08

Re: If statement where both conditions get used

Posted 11 October 2011 - 08:52 AM

I would disagree. When one assumes a simple if statement should work and it's not there's a problem. The nature of an 'if' statement doesn't just decide to stop. My first instinct is that that function is not returning what you think it is. I also think that function may not be working - hence why dumping an try/catch around the SQL command call is recommended.

I always get a bit leery when someone strips away, or substitutes too basic of code, for a problem.

Try the try/catch and see what turns up.
Was This Post Helpful? 0
  • +
  • -

#11 Tildha  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 11-October 11

Re: If statement where both conditions get used

Posted 11 October 2011 - 09:15 AM

I see your point with the Try Catch statment.

    Private Sub sSetUpBranchVisabilities()
        Try
            If objSQLServ_DAL.fGetReadPNR(cboHandlingBranches.Text.Trim) Then
                cmdReadPNR.Visible = True
            Else
                cmdReadPNR.Visible = False
            End If

        Catch ex As Exception
            MessageBox.Show(ex.ToString)
        End Try

    End Sub



When the Yellow debugger is on cmdReadPNR.Visible = True I can see the following in Watch:
Name: objSQLServ_DAL.fGetReadPNR(cboHandlingBranches.Text.Trim)
Value: True
Type: Boolean

As far as I understand it, that means that the return value from the function is correct, and exactly what we want to test.

The strange thing that happens next when debugging is that the Yellow jumps down to the line Message.Show(ex.string).It does not even go to the Catch line first. At this point Watch displayes:
Name: ex
Value: Name 'ex' is not declared.

Also the Message box does never open up and at all even if the "Yellow" is on it.

Again, I don't understand how this can happen, because normally when an exception is caught there is always a message.
Was This Post Helpful? 0
  • +
  • -

#12 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 8915
  • View blog
  • Posts: 33,430
  • Joined: 12-June 08

Re: If statement where both conditions get used

Posted 11 October 2011 - 09:31 AM

So it is hitting an exception. Like I said - probably inside that SQL call, but where ever.

Try "ex.message" and not the "ex" object itself.
Was This Post Helpful? 0
  • +
  • -

#13 Tildha  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 11-October 11

Re: If statement where both conditions get used

Posted 11 October 2011 - 09:36 AM

No changes at all, unfortunatly.
When the Yellow debugger is on MessageBox.Show, ex is still nothing, and after it passed it (and leaves the sub) the Message box doesn't even appear.

    Private Sub sSetUpBranchVisabilities()
        Try
            If objSQLServ_DAL.fGetReadPNR(cboHandlingBranches.Text.Trim) Then
                cmdReadPNR.Visible = True
            Else
                cmdReadPNR.Visible = False
            End If

        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try

    End Sub


Was This Post Helpful? 0
  • +
  • -

#14 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 8915
  • View blog
  • Posts: 33,430
  • Joined: 12-June 08

Re: If statement where both conditions get used

Posted 11 October 2011 - 09:37 AM

Okay.. put the try/catch in "fGetReadPNR"... see what happens there.
Was This Post Helpful? 0
  • +
  • -

#15 Tildha  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 11-October 11

Re: If statement where both conditions get used

Posted 11 October 2011 - 09:41 AM

Done, and no exception is caught at all.

To clarify, no exception is caught in the function.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2