单行If语句问题 [英] Single-Line If Statement Problem

查看:92
本文介绍了单行If语句问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下单行if语句,即使它不应该评估为真,即使它是b $ b。我以前从未见过这个,我担心这会在我的代码的其他区域发生这种情况。


如果String1.Length> 0然后String2 = String1


其中String1 =""


当String1 =""时,此语句的计算结果为true:


如果String1<> ""然后String2 = String1


将If语句更改为多行If语句更正问题:


如果String1.Length> 0然后

String2 = String1

结束如果


有没有其他人看到这个问题或知道发生了什么?


谢谢

解决方案

使用单行If语句是一种不好的做法。如果编程正确

解决问题,那么问题是什么?


Bob Lehmann


" ; DLP22192" < DL ****** @ discussions.microsoft.com>在留言中写道

news:06 ********************************** @ microsof t.com ...

我有以下单行if语句,即使它不应该评估为true。我之前从未见过这个,我担心
这可能发生在我的代码的其他方面。

如果String1.Length> 0然后String2 = String1

其中String1 =""

当String1 ="":

时,此语句的计算结果为true如果String1<> ""然后String2 = String1

将If语句更改为多行If语句更正
问题:
如果String1.Length> 0然后
String2 = String1
结束如果

有没有其他人看到这个问题或知道发生了什么?

谢谢


你能给我们一个完整的(简短)程序来证明这个问题吗?

" DLP22192" < DL ****** @ discussions.microsoft.com>在留言中写道

news:06 ********************************** @ microsof t.com ...

我有以下单行if语句,即使它不应该评估为true。我以前从未见过这个,我担心
这可能发生在我的代码的其他方面。

如果String1.Length> 0然后String2 = String1

其中String1 =""

当String1 ="":

时,此语句的计算结果为true如果String1<> ""然后String2 = String1

将If语句更改为多行If语句更正了
问题:

如果String1.Length> 0然后
String2 = String1
结束如果

有没有其他人看到这个问题或知道发生了什么?

谢谢


我不买。我逐步完成了下面的代码,并没有任何条件

评估为真。你对这个问题的描述就是留下一些东西。

Dim s1 As String

Dim s2 As String

s1 =""

s2 =" x"

如果s1.Length> 0然后s2 = s1

s2 =" x"

如果s1<> ""然后s2 = s1

s2 =" x"

如果s1.Length> 0然后

s2 = s1

结束如果

" DLP22192"写道:

我有以下单行if语句评估为真,即使它不应该。我之前从未见过这个,我担心这会在我的代码的其他方面发生。

如果String1.Length> 0然后String2 = String1

其中String1 =""

当String1 ="":

时,此语句的计算结果为true如果String1<> ""然后String2 = String1

将If语句更改为多行If语句更正问题:

如果String1.Length> 0然后
String2 = String1
结束如果

有没有其他人看到这个问题或知道发生了什么?

谢谢


I have the following single-line if statement that is evaluating true even
though it shouldn''t. I have never seen this before and I am concerned that
this can happen in other areas of my code.

If String1.Length > 0 Then String2 = String1

where String1 = ""

This statement also evaluates as true when String1 = "":

If String1 <> "" Then String2 = String1

Changing the If statement to a multi-line If statement corrects the problem:

If String1.Length > 0 Then
String2 = String1
End If

Has anyone else seen this problem or know what''s going on?

Thanks

解决方案

Using Single-Line If statements is a bad practice. If programming correctly
solves the problem, then what''s the problem?

Bob Lehmann

"DLP22192" <DL******@discussions.microsoft.com> wrote in message
news:06**********************************@microsof t.com...

I have the following single-line if statement that is evaluating true even
though it shouldn''t. I have never seen this before and I am concerned that this can happen in other areas of my code.

If String1.Length > 0 Then String2 = String1

where String1 = ""

This statement also evaluates as true when String1 = "":

If String1 <> "" Then String2 = String1

Changing the If statement to a multi-line If statement corrects the problem:
If String1.Length > 0 Then
String2 = String1
End If

Has anyone else seen this problem or know what''s going on?

Thanks



Can you give us a complete (short) program that demonstrates the problem?
"DLP22192" <DL******@discussions.microsoft.com> wrote in message
news:06**********************************@microsof t.com...

I have the following single-line if statement that is evaluating true even
though it shouldn''t. I have never seen this before and I am concerned
that
this can happen in other areas of my code.

If String1.Length > 0 Then String2 = String1

where String1 = ""

This statement also evaluates as true when String1 = "":

If String1 <> "" Then String2 = String1

Changing the If statement to a multi-line If statement corrects the
problem:

If String1.Length > 0 Then
String2 = String1
End If

Has anyone else seen this problem or know what''s going on?

Thanks



I don''t buy it. I stepped through the code below, and none of the conditions
evaluated to true. Your description of the problem is leaving something out.
Dim s1 As String
Dim s2 As String
s1 = ""
s2 = "x"
If s1.Length > 0 Then s2 = s1
s2 = "x"
If s1 <> "" Then s2 = s1
s2 = "x"
If s1.Length > 0 Then
s2 = s1
End If
"DLP22192" wrote:

I have the following single-line if statement that is evaluating true even
though it shouldn''t. I have never seen this before and I am concerned that
this can happen in other areas of my code.

If String1.Length > 0 Then String2 = String1

where String1 = ""

This statement also evaluates as true when String1 = "":

If String1 <> "" Then String2 = String1

Changing the If statement to a multi-line If statement corrects the problem:

If String1.Length > 0 Then
String2 = String1
End If

Has anyone else seen this problem or know what''s going on?

Thanks



这篇关于单行If语句问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆