×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

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

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

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

(OP)
Colleagues,

Subject line says it. The code:

CODE --> VB

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 --> VB

Dim loStreamReader As New StreamReader(tcFileIn) 

Here's the screenshot:



And now the question:
From what I read, Using statement is supposed to discard the object after the code block is executed (https://docs.microsoft.com/en-us/dotnet/visual-bas...).
Hence me declaring loXMLStreamReader again - which generates this error.

What am I missing/misinterpreting?

Additionally: since TextReader is IDisposable, and StreamReader "implements" (?) TextReader (https://docs.microsoft.com/en-us/dotnet/api/system...), shan't StreamReader be also IDisposable?

Please advise.
TIA!

Regards,

Ilya

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

>supposed to discard the object

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

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

(OP)
strongm: "this is a design time error, not runtime, I'm afraid"

ponder

Could you, please elaborate?

TIA!

Regards,

Ilya

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

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.

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

(OP)
Actually (and I'm not boasting my ego winky smile ) 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

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

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!

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

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

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close