Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Error "Variable 'loStreamReader' hides a variable in an enclosing block" 1

Status
Not open for further replies.

IlyaRabyy

Programmer
Nov 9, 2010
566
0
16
US
Colleagues,

Subject line says it. The code:

Code:
Dim lcBuffer(13) As Char, lcHdrStr As String

Using loStreamReader As StreamReader = New StreamReader(tcFileIn)
   loStreamReader.Read(lcBuffer, 0, 14)
   lcHdrStr = String.Join("", lcBuffer)
End Using

And later, within the same Sub:

Code:
Dim loStreamReader As New StreamReader(tcFileIn)

Here's the screenshot:

20200304_Using_statement_Problem_eb4h9t.jpg


And now the question:
From what I read, Using statement is supposed to discard the object after the code block is executed (Hence me declaring loXMLStreamReader again - which generates this error.

What am I missing/misinterpreting?

Additionally: since TextReader is IDisposable, and StreamReader "implements" (?) TextReader ( shan't StreamReader be also IDisposable?

Please advise.
TIA!

Regards,

Ilya
 
>supposed to discard the object

Never gets this far - this is a design time error, not runtime, I'm afraid
 
strongm: "this is a design time error, not runtime, I'm afraid"

[ponder]

Could you, please elaborate?

TIA!

Regards,

Ilya
 
Design time errors (sometimes referred to as compile-time, particularly with something like Visual Studio where we have constant ongoing incremental compilation) result if we do not follow the proper syntax and semantics of a programming language, as such errors mean that the code cannot compile. In your case, you've not adhered to the semantics.

A run-time error is one that occurs, it may not surprise you to learn, when the code has successfully compiled and is running, and consist of such things as logic errors, IO errors (e.g trying to open a non-existent file), encoding errors, undefined object errors, division by zero errors, etc.

 
Actually (and I'm not boasting my ego ;-) ) I have ~35 years of programming experience under my belt (Basic from ver. 1.0 for DOS to VB6, FPD/VFP, some C/C++/C#, not to mention PDP-11 Assembly). That is - I am "somewhat familiar" with both concepts you have described. (No offense meant, neither sarcasm.)

My question, if read literally, means: does the SreamReader implement IDisposable and, if it does, why it isn't disposed after End Using?

I hope I made myself clear?

TIA!

Regards,

Ilya
 
The problem is occurring because you're *declaring* 2 variables with the same name. So, Using may destroy the *object* created by the declaration, but the declaration still exists. There's no way around this other than using different names in the declarations. Or, just use one declaration:

Dim loStreamReader As StreamReader

loStreamReader = New StreamReader(tcFileIn)

Using loStreamReader
loStreamReader.Read(lcBuffer, 0, 14)​
lcHdrStr = String.Join("", lcBuffer)​
End Using

'later

loStreamReader = New StreamReader(tcFileIn)



I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day!
 
This is odd - I posted a reply to this thread a while back, and it seems to have disappeared!

So, what did it say .. erm ...

>does the SreamReader implement IDisposable and, if it does, why it isn't disposed after End Using?

Yes, it does, and yes, your loStreamReader would be disposed off - if that code ran.

However, so what? It's irrelevant, as that is concerned with what happens to object creation and deletion at runtime. You are not getting that far, and your error is a semantic one (the details of which are pretty clearly explained in the error message you are seeing) that is picked up at design-time (and which is preventing successful compilation). Put simply, you've written incorrect VB. And Jebenson has outlined a solution to this
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top