Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Visual Basic > VB 6 Visual Basic 6 > VB.NET
|
VB.NET General VB.NET discussions for issues that don't fall into other VB.NET forums.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the VB.NET section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 17th, 2012, 04:26 PM
Registered User
 
Join Date: Jul 2012
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default Error: Variable "intNumber" hides a variable in an enclosing block

[code]
'Display the ratings for the Hotels
Dim intNumber



For intNumber As Integer = 1 To 6
lblStars.Text = intNumber
intNumber = Val(InputBox("Hotel Rating"))
Next



End Sub
End Class [/code}
 
Old July 17th, 2012, 04:41 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Yes, and? Is there a question in this post?

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
 
Old July 17th, 2012, 05:27 PM
Registered User
 
Join Date: Jul 2012
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

the question is that what does that error mean exactly
 
Old July 18th, 2012, 04:31 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

It means more or less exactly what it says: you're defining intNumber twice, the second one effectively "hiding" the first one. Inside the loop you're creating a *new* variable called intNumber using "intNumber as Integer". By using this name, you can no longer reach the intNumber you defined outside the loop. My guess is you only intend to have one, so the fix would either be:

Code:
Dim intNumber
For intNumber = 1 To 6
  lblStars.Text = intNumber
  intNumber = Val(InputBox("Hotel Rating"))
Next
or
Code:
For intNumber As Integer = 1 To 6
  lblStars.Text = intNumber
  intNumber = Val(InputBox("Hotel Rating"))
Next
In the former case, intNumber is also available outside of the For loop.

Hope this helps,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!





Similar Threads
Thread Thread Starter Forum Replies Last Post
to recover "undefined variable" error raihanveer BOOK: PHP and MySQL 24-Hour Trainer 0 April 4th, 2012 11:31 AM
Exception in thread "main" javax.crypto.BadPadd?ingException: pad block corrupted boy18nj BOOK: Beginning Cryptography with Java 9 June 6th, 2011 12:50 AM
MSXSL gives error message for "for" inside "select" ilyaz XSLT 1 December 9th, 2010 05:02 PM
Add a CheckBox DataColumn to my DataGridView, Null format: "" or "True" but Error: F ismailc C# 2005 0 September 25th, 2009 04:56 AM
"Add Variable" - Chap 14 ArtP2 BOOK: Ivor Horton's Beginning Visual C++ 2008 ISBN: 978-0-470-22590-5 4 May 30th, 2009 04:02 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.