陈述陈述 [英] Straing If Statments
问题描述
好吧,我尝试不为老年人解决这个问题.
使用If语句时出现问题.
Ok i have tried to figure this out for agers not.
I have a problem when i use an If Statement.
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
因此,如果LogSettingsis =等于2或3,那么我需要查看消息框,问题是如果我将LogSettings As String = 1更改为
MessageBox仍然显示.
这是我的原始代码
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
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
If LogSettings = 2 Or 3 Then WriteLog("User: Button1_Click", Color.Blue)
我可以使用所有的If,Else,End If Bla Bla Bla,但这看起来很混乱,如果有人可以帮助我解决代码,将不胜感激.
感谢您的帮助.
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.
推荐答案
您需要检查运算符的优先级.
You need to check operator precedence.
If LogSettings = 2 Or 3 Then
被解析为
If (LogSettings = 2) Or 3 Then
,这始终是正确的,因为3非零.
您需要的是类似
的东西
which is always true, because 3 is nonzero.
What you need is something like
If (LogSettings = 2) Or (LogSettings = 3) Then
或者,您可以使用
Alternatively, you could use
If LogSettings >= 2 Then
或
Ff LogSettings > 1 Then
这篇关于陈述陈述的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!