2011-03-04 45 views
1

我正在寻找如何在vb.net的两行中编写if else语句。我不是指三元运营商,尽管这可能有用。如果... elseif,else语句在三行(或者如果在其他两行)

例如,我想这样做
if x = y then matching = true
elseif x < y then ygreater = true
else xgreater = true

我发誓,我见过这样的事情之前,可能使用了“:”操作符。

任何帮助将是伟大的,谢谢。

+0

它为什么它有多少行? – Jon

+0

说实话,它确实没什么,只是格式化而已。我只想简化一些基本的陈述,但不太喜欢三元运算符。 – Kosko

回答

3

我想你可以使用:标志​​分割线:

If x = y Then matching = True: ElseIf x < y Then ygreater = True: Else xgreater = True: End If 

但是,为什么?

+0

没有理由,实际上很重要,只是为了缩短方法。 – Kosko

1

您可以使用行续字符_将该单行语句拆分为三行。

+0

这两个结合的答案正是我要找的,谢谢! – Kosko

0
intInput = x 
Select Case intInput 
    Case y 
     matching = true 
    Case IS < y 
     ygreater = true 
    Case Else 
     xgreater = true 
End Select 

在C#.NET,你需要使用 “案例1:”(也许你意味着:)

1

不知道为什么,但在这里不用

  If x <= y Then If x < y Then ygreater = True Else matching = True Else xgreater = True 

证明

Dim y As Integer 
    Dim ygreater, xgreater, matching As Boolean 
    y = 1 
    Debug.WriteLine("") 
    For x As Integer = 0 To 2 
     Debug.WriteLine("X = {0} Y = {1} ", x, y) 
     ygreater = False 
     xgreater = False 
     matching = False 
     If x <= y Then If x < y Then ygreater = True Else matching = True Else xgreater = True 
     Debug.WriteLine("X > {0} Y > {1} Match {2}", xgreater, ygreater, matching) 
    Next