if condition true do next for loop iteration - vb.net

I have code like:
For Each dr As DataRow In items.Tables(0).Rows()
If dr("PRICE") = 0.0 Or dr("PRICE").ToString = "0.0" Then
'Skip end go in for-each again
Else
'Do something else.
End If
Next
I want to know if condition is true (Price is 0) then it should go to next item in for-each and do IF check again. Something like a NEXT?

As asawyer already pointed out, using the Continue Keyword is what you would want to do. It does exactly what you need - it just jumps to the next iteration of the loop specified. In your program it would look like this:
For Each dr As DataRow In items.Tables(0).Rows()
If dr("PRICE") = 0.0 Or dr("PRICE").ToString = "0.0" Then
Continue For
Else
...Do something else.
End If
Next

Related

Condition to Continue a For Loop

First go around with programming in VB.net and came across a little issue. I'm trying to find a way to have an If statement control whether a for loop executes or not before going through another iteration.
Code:
For Each theTable In tableDoc.Tables
testString = ""
For row = 1 To theTable.Rows.Count
isHeaderOrNot = theTable.Cell(row, 1).Range.Text 'Gets value in first column
If isHeaderOrNot is Not Like "Section" or "Field" then Continue For 'if the words "Section" or "Field" are NOT included in isHeaderOrNot then continue with For loop
keyText = theTable.Cell(row, 2).Range.Text
valueText = theTable.Cell(row, 3).Range.Text
Console.WriteLine("KEY: {0}", keyText)
Console.WriteLine("VALUE: {0}", valueText)
Console.WriteLine("")
Next
Next
I'm getting an error on Like saying there's an expression expected..
Basically if the first column in the row the for loop is in contains Section or Field I want it go to the next line. First time trying to explain a question dealing with this, so any questions just ask! I truly appreciate your time!
You can just include the whole block in the if, and if it's not the case and you want to end the loop, you can use Exit For.
For Each theTable In tableDoc.Tables
testString = ""
For row = 1 To theTable.Rows.Count
isHeaderOrNot = theTable.Cell(row, 1).Range.Text
'If cell doesn't contain Section or Field, then do the following
If Not isHeaderOrNot.contains("Section") AndAlso Not isHeaderOrNot.contains("Field")
keyText = theTable.Cell(row, 2).Range.Text
valueText = theTable.Cell(row, 3).Range.Text
Console.WriteLine("KEY: {0}", keyText)
Console.WriteLine("VALUE: {0}", valueText)
Console.WriteLine("")
Else
'If it DOES include Section or Field, then stop looping
Exit For
End If
Next
Next
The correct syntax should be
If Not isHeaderOrNot Like "Section" AndAlso
Not isHeaderOrNot Like "Field" Then
Exit For

datagridview value already exist vb

this is my code for checking if a value(from dgvlist) already exist in dgvorders, my problem is the error message only shows for the first selectedrow of the dgvlist and the rest still adds.
For Each row As DataGridViewRow In dgvOrders.Rows
For i = 0 To dgvOrders.Rows.Count - 1
If row.Cells(0).Value = dgvList.SelectedRows(i).Cells(0).Value Then
MsgBox("item already listed!", MsgBoxStyle.Critical)
Else : dgvOrders.Rows.Add(v, w, x, y, z)
End If
Exit Sub
Exit For
Next
Next
Because after If statement you are using End sub so once the first record is compared, It is coming out of entire sub.
I suggest you focus on each item in turn. You only want one For Each ... Next loop (unless you also wanted to traverse the columns).
Next get the value to test into a variable ValueFromdgvList. It should be declared as a number or string to match the value expected. I don't know if I have the right syntax or the right value in the code below. You should put a breakpoint in your code and step it so you can check that you have the expected value before starting your compare.
Finally to add a row you need to set the values to an array. Better would be to define a new DataGridViewRow and add the values to that. Finally add it to the DataGridView. But I'll leave that.
'change this as needed
Dim ValueFromdgvList As String = dgvList.SelectedRows(0).Cells(0).Value
Debug.Print(ValueFromdgvList)
Dim haveMatch As Boolean
For Each row As DataGridViewRow In dgvOrders.Rows
If row.Cells(0).Value = ValueFromdgvList Then
haveMatch = True
Exit For
End If
Next
If haveMatch Then
MsgBox("item already listed!", MsgBoxStyle.Critical)
Else
Dim ValuesArray() As Object = {v, w, x, y, z}
dgvOrders.Rows.Add(ValuesArray)
End If
We're using haveMatch because we don't want to add a new row until we've checked all the rows.

Don't go into this loop if the array is empty

Til now the method I use to avoid going into for loops which loop through an array which is currently empty is the following:
if len(join(array,"") > 0 then
for i = 1 to ubound(array)
code
next
end if
But this is no joke, I just recently used that line of code if len(join(array,"") > 0 then and that caused the deletion of array and it crashed my program 5 times in a row. I know that sounds hard to believe but here is a screen shot
for some reason the code len(join(array,"") > 0 would destroy the variables2 array. And here is a screen shot that shows that the variables array is clearly full before I go to the bad code: So now I'm trying to use a different code I tried:
if not isempty(array) then
But that's not working. Any ideas?
If Len(Join(greek_act, "")) > 0 Then
For i = 1 To UBound(greek_act)
For j = 1 To UBound(variables2)
If greek_act(i) = variables2(j) Then
variables2(j) = ""
Exit For
End If
Next
Next
variables2 = remove_blanks(variables2)
End If
'variables2 array is full
If Len(Join(greek_letters, "")) > 0 Then
'variables2 array gets destroyed and program crashes here.
For i = 1 To UBound(greek_letters)
rightres1 = Replace(rightres1, greek_letters(i), variables2(i))
Next
End If
Never mind, I decided to just go ahead with the following solution since it appears that the program is temporarily acting up
On Error Resume Next
For i = 1 To UBound(greek_letters)
rightres1 = Replace(rightres1, greek_letters(i), variables2(i))
Next
On Error GoTo 0``
For the Join() technique to work reliably, you must complete the Dimming/ReDimming process:
Sub dural()
Dim greek_ary(1 To 3) As String
s = Join(greek_ary, "")
MsgBox Len(s)
End Sub
Without filling the array, the Len will report 0
Since this is a common test I like to use a reusable function like this:
Function IsArrayEmpty(anArray As Variant)
Dim i As Integer
On Error Resume Next
i = UBound(anArray, 1)
If Err.Number = 0 Then
IsVarArrayEmpty = False
Else
IsVarArrayEmpty = True
End If
End Function
Now in your main Sub do this:
If Not IsArrayEmpty(greek_act) Then
For i = 1 To UBound(greek_act)
For j = 1 To UBound(variables2)
If greek_act(i) = variables2(j) Then
variables2(j) = ""
Exit For
End If
Next
Next
variables2 = remove_blanks(variables2)
End If

Is there a way to exclude blank excel cells on import using oledbadapter in a datagridview?

Hello I'm trying to find a way to trim blank cells on my datagridview if the excel cells are blank on import. I found this on MDSN but it doens't seem to be working. Maybe something along these lines will work. I greatly appreciate any help anyone can give!
Dim Empty As Boolean = True
For i As Integer = 0 To dataGridView1.Rows.Count - 1
Empty = True
For j As Integer = 0 To dataGridView1.Columns.Count - 1
If dataGridView1.Rows(i).Cells(j).Value IsNot Nothing AndAlso dataGridView1.Rows(i).Cells(j).Value.ToString() <> "" Then
Empty = False
Exit For
End If
Next
If Empty Then
dataGridView1.Rows.RemoveAt(i)
End If
Next
One problem is you need to work from the bottom up on the DGV:
For i As Integer = DataGridView1.Rows.Count - 1 To 0
If you work from the top down the index of any given row below the row removed changes.

Remove repetition from if-else statement

I have written an if...else statement which uses an array myArr and a string myStr as follows:
If myArr.Length > 0 AndAlso myArr(0) = "-1" Then
'Do stuff 1
ElseIf myStr= "xyz" Then
'Do stuff 2
ElseIf myArr.Length > 0 Then
'Do Stuff 3
Else
'Do Nothing
End If
It works exactly as I need. But It looks really confusing, mostly because the array length is checked twice. There must be a clearer way to write it, but I can't think of one.
Just to clarify.... the order that each statement is executed is crucial. i.e. Stuff 1 has priority over stuff 2, which has priority over stuff 3.
I don't think you'll be able to get exactly the same flow in a simpler way.
Either you're going to end up doing things different things, or doing duplicate things. eg:
If myArr.Length > 0 Then
If myArr(0) = "-1" Then
'Do stuff
Else
'Do stuff
End If
ElseIf myStr= "xyz" Then
'Do stuff
Else
'Do Nothing
End If
This will cause mystr="xyz" not to happen whenn myArr(0) <> -1 whereas it may have before.
You might be able to get rid of multiple checks against myArr.Length by using nested If statements, but this makes the code less clear.
A clear approach (i.e. one that can be understood easily) is one that allows you to read a code fragment without having to remember the context in which the code is executed. By nesting If statements, more information must be kept in the readers working memory (or short term memory) to deduce the meaning of he code.
I guess this would do the trick:
If myArr.Length > 0 Then
If myArr(0) = "-1" Then
'Do stuff
Else
'Do stuff
End If
ElseIf myStr= "xyz" Then
'Do stuff
Else
'Do Nothing
End If
What you need is nested if
If myArr.Length > 0 Then
'Do stuff for non zero length
If myArr(0) = "-1" Then
'Do stuff for -1 and you already have checked for array length
End If
End If
If myArr.Length > 0 Then
If myArr(0) = "-1" Then
' Stuff 1
Else
' Stuff 3
End If
ElseIf myStr = "xyz"
' Stuff 2
End If
... no further else needed
You can decompose the statements like this:
If myArr.Length > 0 Then
If myArr(0) = "-1" Then
'Do stuff
Else
'Do sstuff
End If
Else
If myyStr= "xyz" Then
'Do stuff
End If
End If