简体   繁体   English

在Vb.net中使用If-Statement作为For-Loop的测试条件

[英]Using If-Statement as test condition for For-Loop in Vb.net

I've been writing some software in Vb.net and I've come to a point in the program where it would be best if I could place an if-statement in the header of a for-loop. 我已经在Vb.net中编写了一些软件,并且在程序中到了一个关键点,那就是如果可以将if语句放在for循环的标题中,那将是最好的。

For example, in java, I could achieve what I need like so.. 例如,在Java中,我可以像这样实现我所需要的。

for (int I = 0; myArray[I].compareTo("") == 0; I ++)
{
    'code here
}

Unfortunately in Vb.net, all I know how to do is increment one number to a given number in a for-loop. 不幸的是,在Vb.net中,我所知道的唯一方法是在for循环中将一个数字递增到给定的数字。 I am aware however, that what I need to do can be accomplished using an if-test inside the for-loop 但是我知道,我需要做的事可以在for循环中使用if-test来完成

For I as Integer = 0 To myArray.length 'only possible test is comparison between two ints

    'code here
    If myArray(I).compareTo("") <> 0 Then
       Exit For
    End If

Next

Its not a big deal having to do this but if there is a way to streamline this more into the for-loop control then I would like to know for now and future reference. 这样做不是什么大不了的事情,但是如果有一种方法可以将其简化为for循环控制,那么我想知道现在和将来的参考。

So my question is, is it possible to check an if-condition (other than comparing two numbers) inside the header of a for-loop in Vb.net? 所以我的问题是,是否可以在Vb.net的for循环头中检查if条件(而不是比较两个数字)?

Update: In response to @Olivier Jacot-Descombes 's answer, I just wanted to clarify that I know while loops are used to test if-conditions in loops, but they lose the functionality of auto-incrementing possessed by for-loops. 更新:为了响应@Olivier Jacot-Descombes的回答,我只是想澄清一下,我知道while循环用于测试循环中的if条件,但是它们失去了for循环所具有的自动递增功能。 In Java, for-loops can do both of these. 在Java中,for循环可以同时实现这两个功能。 Which is why I'm wondering if Vb.net has the same functionality all within the header of a for-loop control somehow. 这就是为什么我想知道Vb.net是否以某种方式在for循环控件的标头中全部具有相同的功能。

Use a While-Loop instead 改用While-Loop

Dim i As Integer = 0
While i < myArray.Length AndAlso String.IsNullOrEmpty(myArray(i))
    'Code here
    i += 1
End While

In VB a string can be empty ( "" ) or Nothing ( null in C#). 在VB中,字符串可以为空( "" )或Nothing (在C#中为null )。 In order to cope with both situations use String.IsNullOrEmpty(s) . 为了应付两种情况,请使用String.IsNullOrEmpty(s)

AndAlso (unlike And ) ensures shortcut evaluation. AndAlso (与And不同)可确保快捷方式评估。 Ie if the first condition is not True then the second will not be evaluated. 即,如果第一个条件不为True ,则将不评估第二个条件。 We need this here, otherwise the array would throw an "Index out of bounds" exception. 我们在这里需要这个,否则数组将抛出“索引超出范围”异常。 Note also that the array index goes from 0 to array.Length - 1. 还要注意,数组索引从0到array.Length-1。

But you can also exit from a For-loop with Exit For 但是您也可以使用Exit For从For循环中Exit For

For I As Integer = 0 To myArray.Length-1

    'code here
    If Not String.IsNullOrEmpty(myArray(I)) Then
       Exit For
    End If

Next

But exiting a loop like this can make the code unreadable. 但是退出这样的循环会使代码不可读。 The problem is that the For-loop has now 2 exit points and loop and exit conditions defined at different places. 问题在于,For循环现在具有2个出口点,并且在不同位置定义了循环和出口条件。

There is also a Do...Loop statement allowing you to test the condition at the end of the loop. 还有一个Do ... Loop语句,允许您在循环结束时测试条件。

The short answer is no. 最简洁的答案是不。 Visual Basic languages don't have anything like the C/java style for() loop. Visual Basic语言没有C / java样式的for()循环。

The longer answer is that depending on what you want, you might not even need a loop. 更长的答案是,根据您的需要,您甚至可能不需要循环。

Dim a = {"a", Nothing, "", "b"}

' this will print from 0 to 1, but Array.IndexOf returns -1 if value is not found
For i = 0 To Array.IndexOf(a, "") - 1
    Debug.Print(i & "")
Next

For Each item In a : If item = "" Then Exit For ' this is actually 2 lines separated by : 
    Debug.Print("'{0}'", item)
Next

For Each item In a.TakeWhile(Function(s) s > "") ' TakeWhile is a System.Linq extension
    Debug.Print("'{0}'", item)
Next

a.TakeWhile(Function(s) s > "").ToList.ForEach(AddressOf Debug.Print) ' prints a

a.TakeWhile(Function(s) s > "").ToList.ForEach(Sub(s) Debug.Print(s)) ' prints a

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM