Using integers within a For Each statement VB.NET

  • Thread starter Thread starter tcropper
  • Start date Start date
T

tcropper

I have a set of integers which I want to search for using a For Each
statement... but I can't quite get it to work correctly.

dim l_intType as Integer

l_intType = MyInt

For Each l_intTranType As Integer In New Long() {1, 20, 100, 135, 200,
207, 205, 211}

WriteStuffToFile

Next

MyInt is a return method from a COM object and changes everytime a
record is read... but this above returns this error:

Variable l_intType hides a variable in an enclosing block

I know why I'm getting this message, but I just can't work out how to
fix this code to only write things to a file when l_intType is equal
to the one of the intgeres in the list above.

HELP!

Ta.
 
Change the line

For Each l_intTranType As Integer In New Long() {1, 20, 100, 135, 200,
207, 205, 211}

by

For Each l_intTranType In New Long() {1, 20, 100, 135, 200,
207, 205, 211}
 
Change the line

For Each l_intTranType As Integer In New Long() {1, 20, 100, 135, 200,
207, 205, 211}

by

For Each l_intTranType In New Long() {1, 20, 100, 135, 200,
207, 205, 211}
 
I have a set of integers which I want to search for using a For Each
statement... but I can't quite get it to work correctly.

dim l_intType as Integer

l_intType = MyInt

For Each l_intTranType As Integer In New Long() {1, 20, 100, 135, 200,
207, 205, 211}

WriteStuffToFile

Next

MyInt is a return method from a COM object and changes everytime a
record is read... but this above returns this error:

Variable l_intType hides a variable in an enclosing block

I know why I'm getting this message, but I just can't work out how to
fix this code to only write things to a file when l_intType is equal
to the one of the intgeres in the list above.

HELP!

Ta.

It looks like you have a logic problem, too. If MyInt is return method from
a COM, the For Each loop will ignore it completely.
 
I have a set of integers which I want to search for using a For Each
statement... but I can't quite get it to work correctly.

dim l_intType as Integer

l_intType = MyInt

For Each l_intTranType As Integer In New Long() {1, 20, 100, 135, 200,
207, 205, 211}

WriteStuffToFile

Next

MyInt is a return method from a COM object and changes everytime a
record is read... but this above returns this error:

Variable l_intType hides a variable in an enclosing block

I know why I'm getting this message, but I just can't work out how to
fix this code to only write things to a file when l_intType is equal
to the one of the intgeres in the list above.

HELP!

Ta.

It looks like you have a logic problem, too. If MyInt is return method from
a COM, the For Each loop will ignore it completely.
 
Then what's the best / easiest way of doing what I need to do?

There was a typo in my original message - it should be:

dim l_intType as Integer

l_intType = MyInt

For Each l_intType As Integer In New Long() {1, 20, 100, 135, 200,
207, 205, 211}

WriteStuffToFile

Next
 
Then what's the best / easiest way of doing what I need to do?

There was a typo in my original message - it should be:

dim l_intType as Integer

l_intType = MyInt

For Each l_intType As Integer In New Long() {1, 20, 100, 135, 200,
207, 205, 211}

WriteStuffToFile

Next
 
tcropper@ said:
I have a set of integers which I want to search for using a For Each
statement... but I can't quite get it to work correctly.

Dim tv As Integer() = {1, 20, 100, 135, 200, 205, 207, 211}
If tv.Contains(MyInt) then
' whatever
End If

Andrew
 
tcropper@ said:
I have a set of integers which I want to search for using a For Each
statement... but I can't quite get it to work correctly.

Dim tv As Integer() = {1, 20, 100, 135, 200, 205, 207, 211}
If tv.Contains(MyInt) then
' whatever
End If

Andrew
 
Dim tv As Integer() = {1, 20, 100, 135, 200, 205, 207, 211}
If tv.Contains(MyInt) then
    ' whatever
End If

Andrew

'contains' is not a member of 'sysyem.array'...
 
Dim tv As Integer() = {1, 20, 100, 135, 200, 205, 207, 211}
If tv.Contains(MyInt) then
    ' whatever
End If

Andrew

'contains' is not a member of 'sysyem.array'...
 
'contains' is not a member of 'sysyem.array'...

You didn't say which version of the framework you're using. I think it's an
extension that linq adds.

Dim tv As Integer() = {1, 20, 100, 135, 200, 205, 207, 211}
for each i as integer in tv
if i=myInt then
' whatever
exit for
end if
next

Andrew
 
'contains' is not a member of 'sysyem.array'...

You didn't say which version of the framework you're using. I think it's an
extension that linq adds.

Dim tv As Integer() = {1, 20, 100, 135, 200, 205, 207, 211}
for each i as integer in tv
if i=myInt then
' whatever
exit for
end if
next

Andrew
 
There was a typo in my original message - it should be:

dim l_intType as Integer

l_intType = MyInt

For Each l_intType As Integer In New Long() {1, 20, 100, 135, 200,
207, 205, 211}

WriteStuffToFile

Next

The original problem of "Variable l_intType hides a variable in an
enclosing block" is because you already have a declared variable of
the name I_intType. For Each statements are semantically equal to
this:

Dim I_intType as Integer
For i as integer = 0 to Array.length
I_intType = Array(i)
' Do Stuff
Next

Combine the above with your code and you get:

Dim l_intType as Integer

l_intType = MyInt

Dim l_intType as Integer
Dim arrLong as new Long(){1, 20, 100, 135, 200, 207, 205, 211}
For i as Integer = 0 To arrLong.length

WriteStuffToFile

Next

The problem here is that you are re-declaring "I_intType". Oddly, the
code with the typo was more correct!

As far was the logic, I am not quite sure what you are trying to do.
Are you searching the array of Longs for the value that was returned
from MyInt()?
If so, the code is much simpler:

Dim I_intType as Integer = MyInt()
Dim arrLong as new Long(){1, 20, 100, 135, 200, 207, 205, 211}

If Array.IndexOf(Of Long)(arrLong, I_IntType) < 0 Then
WriteStuffToFile
End If

Hope this helps!

Norm
 
There was a typo in my original message - it should be:

dim l_intType as Integer

l_intType = MyInt

For Each l_intType As Integer In New Long() {1, 20, 100, 135, 200,
207, 205, 211}

WriteStuffToFile

Next

The original problem of "Variable l_intType hides a variable in an
enclosing block" is because you already have a declared variable of
the name I_intType. For Each statements are semantically equal to
this:

Dim I_intType as Integer
For i as integer = 0 to Array.length
I_intType = Array(i)
' Do Stuff
Next

Combine the above with your code and you get:

Dim l_intType as Integer

l_intType = MyInt

Dim l_intType as Integer
Dim arrLong as new Long(){1, 20, 100, 135, 200, 207, 205, 211}
For i as Integer = 0 To arrLong.length

WriteStuffToFile

Next

The problem here is that you are re-declaring "I_intType". Oddly, the
code with the typo was more correct!

As far was the logic, I am not quite sure what you are trying to do.
Are you searching the array of Longs for the value that was returned
from MyInt()?
If so, the code is much simpler:

Dim I_intType as Integer = MyInt()
Dim arrLong as new Long(){1, 20, 100, 135, 200, 207, 205, 211}

If Array.IndexOf(Of Long)(arrLong, I_IntType) < 0 Then
WriteStuffToFile
End If

Hope this helps!

Norm
 
The original problem of "Variable l_intType hides a variable in an
enclosing block" is because you already have a declared variable of
the name I_intType. For Each statements are semantically equal to
this:

Dim I_intType as Integer
For i as integer = 0 to Array.length
    I_intType = Array(i)
     ' Do Stuff
Next

Combine the above with your code and you get:

Dim l_intType as Integer

l_intType = MyInt

Dim l_intType as Integer
Dim arrLong as new Long(){1, 20, 100, 135, 200, 207, 205, 211}
For i as Integer = 0 To arrLong.length

     WriteStuffToFile

Next

The problem here is that you are re-declaring "I_intType". Oddly, the
code with the typo was more correct!

As far was the logic, I am not quite sure what you are trying to do.
Are you searching the array of Longs for the value that was returned
from MyInt()?
If so, the code is much simpler:

Dim I_intType as Integer = MyInt()
Dim arrLong as new Long(){1, 20, 100, 135, 200, 207, 205, 211}

If Array.IndexOf(Of Long)(arrLong, I_IntType) < 0 Then
     WriteStuffToFile
End If

Hope this helps!

Norm

Oops, messed something up:
...only write things to a file when l_intType is equal
to the one of the intgeres in the list above.

If Array.IndexOf(Of Long)(arrLong, I_IntType) < 0 Then

should be:

If Array.IndexOf(Of Long)(arrLong, I_IntType) <> -1 Then

or something equivalent. "< 0" would only run code if I_intType was
NOT in the array.
 
The original problem of "Variable l_intType hides a variable in an
enclosing block" is because you already have a declared variable of
the name I_intType. For Each statements are semantically equal to
this:

Dim I_intType as Integer
For i as integer = 0 to Array.length
    I_intType = Array(i)
     ' Do Stuff
Next

Combine the above with your code and you get:

Dim l_intType as Integer

l_intType = MyInt

Dim l_intType as Integer
Dim arrLong as new Long(){1, 20, 100, 135, 200, 207, 205, 211}
For i as Integer = 0 To arrLong.length

     WriteStuffToFile

Next

The problem here is that you are re-declaring "I_intType". Oddly, the
code with the typo was more correct!

As far was the logic, I am not quite sure what you are trying to do.
Are you searching the array of Longs for the value that was returned
from MyInt()?
If so, the code is much simpler:

Dim I_intType as Integer = MyInt()
Dim arrLong as new Long(){1, 20, 100, 135, 200, 207, 205, 211}

If Array.IndexOf(Of Long)(arrLong, I_IntType) < 0 Then
     WriteStuffToFile
End If

Hope this helps!

Norm

Oops, messed something up:
...only write things to a file when l_intType is equal
to the one of the intgeres in the list above.

If Array.IndexOf(Of Long)(arrLong, I_IntType) < 0 Then

should be:

If Array.IndexOf(Of Long)(arrLong, I_IntType) <> -1 Then

or something equivalent. "< 0" would only run code if I_intType was
NOT in the array.
 
Back
Top