Main Page | Report Page

 

  .NET DotNet Forum Index » ASP.NET Forum » Using integers within a For Each statement VB.NET...

Author Message
...
Posted: Wed May 20, 2009 6:07 am
 
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.
 
Jeova Almeida...
Posted: Wed May 20, 2009 11:19 am
 
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}



<tcropper at (no spam) gmail.com> escreveu na mensagem
news:918c098d-484d-402b-8fa8-76898782d020 at (no spam) n8g2000vbb.googlegroups.com...
Quote:
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.

 
Jeova Almeida...
Posted: Wed May 20, 2009 11:24 am
 
<tcropper at (no spam) gmail.com> escreveu na mensagem
news:918c098d-484d-402b-8fa8-76898782d020 at (no spam) n8g2000vbb.googlegroups.com...
Quote:
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.
 
Norm...
Posted: Fri May 22, 2009 7:52 am
 
On May 21, 3:19 am, tcrop... at (no spam) gmail.com wrote:
Quote:
On May 21, 10:59 am, tcrop... at (no spam) gmail.com wrote:



On 20 May, 18:24, "Jeova Almeida" <jeovaalme... at (no spam) yahoo.com> wrote:

tcrop... at (no spam) gmail.com> escreveu na mensagemnews:918c098d-484d-402b-8fa8-76898782d020 at (no spam) n8g2000vbb.googlegroups.com...

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

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
 
Norm...
Posted: Fri May 22, 2009 7:56 am
 
On May 22, 10:52 am, Norm <neon... at (no spam) gmail.com> wrote:
Quote:
On May 21, 3:19 am, tcrop... at (no spam) gmail.com wrote:



On May 21, 10:59 am, tcrop... at (no spam) gmail.com wrote:

On 20 May, 18:24, "Jeova Almeida" <jeovaalme... at (no spam) yahoo.com> wrote:

tcrop... at (no spam) gmail.com> escreveu na mensagemnews:918c098d-484d-402b-8fa8-76898782d020 at (no spam) n8g2000vbb.googlegroups.com...

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

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:

Quote:
...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.
 
 
Page 1 of 1    
All times are GMT - 5 Hours
The time now is Wed Aug 31, 2016 9:15 am