Using integers within a For Each statement VB.NET

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.
 
J

Jeova Almeida

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}
 
J

Jeova Almeida

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}
 
J

Jeova Almeida

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.
 
J

Jeova Almeida

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.
 
T

tcropper

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
 
T

tcropper

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
 
A

Andrew Morton

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
 
A

Andrew Morton

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
 
T

tcropper

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'...
 
T

tcropper

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'...
 
A

Andrew Morton

'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
 
A

Andrew Morton

'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
 
N

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
 
N

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
 
N

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.
 
N

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top