|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Q: skipping a For EachI was wondering if there is a way to do the following? If I have a For Each loop, is there a way "jump out" of the current item so the next item is processed? For example, I can do it this way For Each row As DataRow In MyDataTable If Not row.Item("col1") = "dont do" Then ' Do some stuff EndIf Next however, I'm wondering if there is another way? Can anyone help? G
Show quote
Hide quote
"G .Net" <nodamnspam@email.com> wrote in message news:ZtKdndqqRtuusDDZRVnyvw@pipex.net... If you are using VB2005, look at the Continue statement.> Hi > > I was wondering if there is a way to do the following? > > If I have a For Each loop, is there a way "jump out" of the current item so > the next item is processed? > > For example, I can do it this way > > For Each row As DataRow In MyDataTable > > If Not row.Item("col1") = "dont do" Then > ' Do some stuff > EndIf > > Next > > however, I'm wondering if there is another way? > > Can anyone help? > > G -- Al Reid
Show quote
Hide quote
"G .Net" <nodamnspam@email.com> schrieb: If you are using VB 2005, check out the 'Continue' statement.> If I have a For Each loop, is there a way "jump out" of the current item > so the next item is processed? > > For example, I can do it this way > > For Each row As DataRow In MyDataTable > > If Not row.Item("col1") = "dont do" Then > ' Do some stuff > EndIf > > Next > > however, I'm wondering if there is another way? -- M S Herfried K. Wagner M V P <URL:http://dotnet.mvps.org/> V B <URL:http://classicvb.org/petition/> Ah, unfortunately, I'm using VS2003
G Show quoteHide quote "Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.at> wrote in message news:OuwKGJRoGHA.1204@TK2MSFTNGP04.phx.gbl... > "G .Net" <nodamnspam@email.com> schrieb: >> If I have a For Each loop, is there a way "jump out" of the current item >> so the next item is processed? >> >> For example, I can do it this way >> >> For Each row As DataRow In MyDataTable >> >> If Not row.Item("col1") = "dont do" Then >> ' Do some stuff >> EndIf >> >> Next >> >> however, I'm wondering if there is another way? > > If you are using VB 2005, check out the 'Continue' statement. > > -- > M S Herfried K. Wagner > M V P <URL:http://dotnet.mvps.org/> > V B <URL:http://classicvb.org/petition/> Hello G .Net,
Then wrap the code you dont want executed in a conditional. For ... DoSomething If blah = something then ' We dont want this code to execute when blah <> something End If Next -Boo G .Net wrote:
<snip> > If I have a For Each loop, is there a way "jump out" of the current item so <snip>> the next item is processed? > For Each row As DataRow In MyDataTable <snip>> > If Not row.Item("col1") = "dont do" Then > ' Do some stuff > EndIf > > Next > > however, I'm wondering if there is another way? If you were using VB 8.0 (VS 2005), then you could use the Continue keyword, which was finally added to the language. Since, unfortunatelly, you're not, then using If blocks as in your example is your best approach. You may keep a "Done" flag that would be tested at the beginning of each block, like this: For Each Row As DataRow In MyDataTable Dim Done As Boolean = False '<-- will be false at each cicle '... If Not Done AndAlso Not row.Item("col1") = "dont do" Then ' Do some stuff Done = True EndIf If Not Done AndAlso SomeOtherCondition then '... more stuff Done = True EndIf '... Next On the other side, if the logic inside the loop is so complex that enclosing the code with If blocks would actually hinder its legibility, consider breaking the method where the loop is located into simpler methods. Finally, if that doesn't help either, then you may, as a last resort, use Goto (no, I'm not trying to start some war, holly or otherwise). To minimize the ill effects of Goto, ensure that you don't use it to break out of the loop or jump backwards. For Each row As DataRow In MyDataTable '... If Not row.Item("col1") = "dont do" Then ' Do some stuff GoTo NextRow EndIf '... NextRow: Next Regards, Branco.
Valid file name
timer control Validating Data Using a Error Provider EM_CHARFROMPOS with RichTextBox CheckedListbox - change check event what is best practice to populate large combobox? Q: Deleting a table with constraints Data binding to an object Somebody please help - Request for the permission of type 'System.Web.AspNetHostingPermission' [...] DBNull problem |
|||||||||||||||||||||||