Hidden variable

F

falderals

Error 1 Variable 'j' hides a variable in an enclosing block.
How do I fix this error? Thanks


Protected Sub ComputeA(ByVal i As Integer)
Dim sk As Single
Dim l As Integer = nn.N_Layers-1
' For the last layer
Dim j As Integer
For j = 0 To nn(l).N_Neurons - 1
nn(l)(j).A = nn(l)(j).OutputPrime * e(j)
Next j
' For other layer
l -= 1
Do While l>=0
Dim j As Integer******* error points to here....******
For j = 0 To nn(l).N_Neurons - 1
sk = 0f
Dim k As Integer
For k = 0 To nn(l+1).N_Neurons - 1
sk += nn(l+1)(k).A * nn(l+1)(k)(j)
Next k
nn(l)(j).A = nn(l)(j).OutputPrime * sk
Next j
l -= 1
Loop
End Sub
 
M

Michel Posseth [MCP]

rowe_newsgroups said:
Either name the variable something different or do the following in
your for...loops (.NET 2.0 and higher)

/////////////
For i As Integer = 0 to 200
....
Next
/////////////

Also, for the sake of your maintenance programmers, name your
variables something that makes sense! I don't mind using "i" for for
loops, but all other variable name imo should be named something that
makes immediate sense.

Thanks,

Seth Rowe [MVP]
http://sethrowe.blogspot.com/

Seth

i would like to add the following as this was unclear for me in your answer

"i" is as far as i know a "standard" in loop variabel naming
as it is used in this method as a parmeter value it is in my opinion
completely wrong in the context where it is used
also
Probably the TS doesn`t know that method level declaration isn`t necesary
for loop variabels if the loop variabel isn`t used outside that loop

To the TS

If you would like to learn more about naming conventions etc etc i would
recomend that you read this book
http://www.amazon.com/Practical-Gui...bs_sr_3?ie=UTF8&s=books&qid=1219697162&sr=8-3

Regards

Michel Posseth
 
R

rowe_newsgroups

"i"  is as far as i know a "standard" in loop variabel naming
as it is used in this method as a parmeter value it is  in my opinion
completely wrong in the context where it is used

I didn't even read that part of his code (I don't read ugly code). I
was simply defending my loop snippet where I use "i" for the loop
variable.

Thanks,

Seth Rowe [MVP]
http://sethrowe.blogspot.com/
 
P

Phill W.

Error 1 Variable 'j' hides a variable in an enclosing block.
How do I fix this error? Thanks

(1) Come up with better variable names? (Sorry; couldn't resist)

(2) /Scope/ your loop control variables so that they are defined and
contained within the [loop] block to which they apply (and not beyond
this) as in:

For j /as Integer/ = 0 To nn(l).N_Neurons - 1
. . .
Next

Do While l >= 0
For j /as Integer/ = 0 To nn(l).N_Neurons - 1
For k /as Integer/ = 0 To nn(l+1).N_Neurons - 1
. . .
Next
Next
l =- 1
Loop

In each case, the variables "j", "j" and "k" (and that's /three/ totally
different variables we're talking about) only exist within the loop that
they are controlling (and are /not/ accessible outside those loops).
In this way, you get rid of the confusion caused by having the same
variable /name/ declared multiple times within a method and meaning
different things, which is what the compiler's complaining about.

HTH,
Phill W.
 
C

Chris Dunaway

Error 1 Variable 'j' hides a variable in an enclosing block.
How do I fix this error? Thanks

Protected Sub ComputeA(ByVal i As Integer)
Dim sk As Single
Dim l As Integer = nn.N_Layers-1
' For the last layer
Dim j As Integer
For j = 0 To nn(l).N_Neurons - 1
nn(l)(j).A = nn(l)(j).OutputPrime * e(j)
Next j
' For other layer
l -= 1
Do While l>=0
Dim j As Integer******* error points to here....******
For j = 0 To nn(l).N_Neurons - 1
sk = 0f
Dim k As Integer
For k = 0 To nn(l+1).N_Neurons - 1
sk += nn(l+1)(k).A * nn(l+1)(k)(j)
Next k
nn(l)(j).A = nn(l)(j).OutputPrime * sk
Next j
l -= 1
Loop
End Sub

In addition to the other responses, just so you know what the problem
is. The line with the error is re-declaring the variable j in a
different scope. You already have j defined near the top of your
code. There is no need to define it again. In this instance, you can
just delete the second declaration. But I would definitely follow the
advice of the other posters regarding variable naming and scope.

Chris
 

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