Click here to Skip to main content
15,896,557 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Ok i have tried to figure this out for agers not.
I have a problem when i use an If Statement.

VB
Public Class Form1
Public LogSettings As Integer = 2 ' 0= Disabled 1= low 2= med 3=high

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 'Goods Inwards
        TabControl1.SelectedIndex = 0
        'If LogSettings = 2 Or 3 Then WriteLog("User: Button1_Click", Color.Blue)

MessageBox.Show(LogSettings)
 If LogSettings = 2 Or 3 Then MessageBox.Show("If the number is a 2 or a 3 then you should see this")

    End Sub

End Class

So if LogSettingsis = to 2 or 3 then i need to see the messagebox, the problem is if i change the LogSettings As String = 1
the MessageBox still shows.
This is my original code
VB
Public Class Form1
    Public LogSettings As Integer = 1 ' 0= Disabled 1= low 2= med 3=high

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 'Goods Inwards
        TabControl1.SelectedIndex = 0
        'If LogSettings = 2 Or 3 Then WriteLog("User: Button1_Click", Color.Blue)
' below here is just some testing code when i was trying to work out this problem
        WriteLog("Setting: " & LogSettings, Color.Blue)
If LogSettings = 2 Or 3 Then WriteLog("User: <>", Color.Blue)

        'If LogSettings <> 2 Or 3 Then WriteLog("User: <>", Color.Blue)
        'If LogSettings < 2 Or 3 Then WriteLog("User: <", Color.Blue)
        'If LogSettings > 2 Or 3 Then WriteLog("User: >", Color.Blue)
    End Sub

as you can see i am trying to add a log file into my application, and i would like it to write a message to the log file all on 1 line of code
VB
If LogSettings = 2 Or 3 Then WriteLog("User: Button1_Click", Color.Blue)

i could use all the If, Else, End If Bla Bla Bla, But that just looks to messy, it would be much appreciated if some one could help me out with the code.

thanks for the help.
Posted

1 solution

You need to check operator precedence.
VB
If LogSettings = 2 Or 3 Then
is being parsed as
VB
If (LogSettings = 2) Or 3 Then
which is always true, because 3 is nonzero.
What you need is something like
VB
If (LogSettings = 2) Or (LogSettings = 3) Then


Alternatively, you could use
VB
If LogSettings >= 2 Then
or
VB
Ff LogSettings > 1 Then
 
Share this answer
 
v3
Comments
Member 8327439 6-Dec-11 20:38pm    
Thank you for that, i can't beleve that i did not think of that, you'r the best
Peter_in_2780 6-Dec-11 20:43pm    
Just because reading it out loud sounds right doesn't mean it works as code! :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900