473,399 Members | 2,159 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

New to VB .NET - Error message not understood

I am new to VB .NET moving from VB6. I wrote the following code and as a
result I received an error concerning it from the IDE. I don't understand
why I get the message or why that what I am doing is not legal. Here is the
code:

Public Function mP_IsPathExist(ByVal Path As String) As Boolean

On Error Resume Next

Dim myIO As New System.IO.FileInfo(Path)

If (Not myIO.Exists()) Then Dim myIO As New
System.IO.DirectoryInfo(Path)

Return myIO.Exists()

End Function

The error message flags the second Dim statement for myIO. It states:

Variable 'myIO' hides a variable in an enclosing block

I get the sense I cannot reuse the var myIO. Do I have to declare a
completely new var for initializing the DirectoryInfo structure? And what
"enclosing block" does it refer to? The function itself?

Thanks for any insight!

JW
Jul 21 '07 #1
4 3172
Yup, sorry

You cannot declare 2 variables with the same name in the same method.

In any case it would make things very hard to understand and we have obfuscators
for that if it's what you're after :)

A general comment on the function itself....

I'm not sure what you're trying to achieve though....

If you require to test does a file exist you can use...
-------------------------------------------------------------
System.IO.File.Exists(SomeFilename)
-------------------------------------------------------------
Or
-------------------------------------------------------------
(New System.IO.FileInfo(SomeFilename)).Exists
-------------------------------------------------------------

If you are truely after a path's existance then...

-------------------------------------------------------------
System.IO.Directory.Exists(SomeDirectoryName)
-------------------------------------------------------------
Or
-------------------------------------------------------------
(New System.IO.DirectoryInfo(SomeDirectoryName)).Exists
-------------------------------------------------------------
Note the versions which refer to info versions imply the precreation of the
Xinfo object which then can have multiple actions performed on it.
However those versions which do not have Info appended are akin to vb modules
in that they do not need to be instantiated prior to tests being called.
however the methods themselves require a reference to the file/directory
in question.

It appears (although I cannot be sure) that your function might be replaced
by either File.Exists or Directory.Exists.

Again I am guessing, I may not have grasped exactly what you are trying to
do in which case feel free to ignore this info :)

--
Rory
Jul 21 '07 #2
Jerry West wrote:
I am new to VB .NET moving from VB6. I wrote the following code and as a
result I received an error concerning it from the IDE. I don't understand
why I get the message or why that what I am doing is not legal. Here is the
code:

Public Function mP_IsPathExist(ByVal Path As String) As Boolean

On Error Resume Next
Ouch. That's ancient VB6-code. Use Try and Catch for error handling.

Also, as you don't have any error handling at all, you are cathing and
ignoring any possible exceptions. That is a very bad practice, as your
code will ignore any exceptions and just keep running with possibly
faulty data.
Dim myIO As New System.IO.FileInfo(Path)

If (Not myIO.Exists()) Then Dim myIO As New
System.IO.DirectoryInfo(Path)

Return myIO.Exists()

End Function

The error message flags the second Dim statement for myIO. It states:

Variable 'myIO' hides a variable in an enclosing block

I get the sense I cannot reuse the var myIO.
Yes, that is correct. It looks like what you are trying to do is to
change the type of the variable, which is not possible.

Instead what the code would do, if the compiler would allow it, is to
create a new variable that only existed inside the If statement. Once
outside the If statement, the myIO variable would still be the FileInfo
reference.
Do I have to declare a
completely new var for initializing the DirectoryInfo structure?
Yes.

But you don't have to create FileInfo and DirectoryInfo objects to check
if a file or directory exists. You can use the System.IO.File.Exist and
System.IO.Directory.Exists methods.
And what
"enclosing block" does it refer to? The function itself?
The enclosing block is the function. The enclosed block is the If statement.

You can declare variables that are local to a code block, but you can't
use the same names as variables in the surrounding code blocks.

--
Göran Andersson
_____
http://www.guffa.com
Jul 22 '07 #3
Jerry West wrote:
I am new to VB .NET moving from VB6. I wrote the following code and as a
result I received an error concerning it from the IDE. I don't understand
why I get the message or why that what I am doing is not legal. Here is the
code:

Public Function mP_IsPathExist(ByVal Path As String) As Boolean
On Error Resume Next
Dim myIO As New System.IO.FileInfo(Path)
If (Not myIO.Exists()) Then Dim myIO As New
System.IO.DirectoryInfo(Path)
Return myIO.Exists()
End Function

The error message flags the second Dim statement for myIO. It states:

Variable 'myIO' hides a variable in an enclosing block

I get the sense I cannot reuse the var myIO. Do I have to declare a
completely new var for initializing the DirectoryInfo structure? And what
"enclosing block" does it refer to? The function itself?
You /can/ and /should/ reuse the variable myIO but you're declaring a
whole new variable with the same name (and that is actually scoped to
exist only between the "If" and "End If".

Just assign a new value to the variable (i.e. drop the second Dim).

Public Function mP_IsPathExist(ByVal Path As String) As Boolean
On Error Resume Next
Dim myIO As New System.IO.FileInfo(Path)
If Not myIO.Exists() Then myIO = New
System.IO.DirectoryInfo(Path)
Return myIO.Exists()
End Function

Then ditch the awful one-line If..Then syntax; it's horribly confusing.

Public Function mP_IsPathExist(ByVal Path As String) As Boolean
On Error Resume Next
Dim myIO As New System.IO.FileInfo(Path)
If Not myIO.Exists() Then
myIO = New System.IO.DirectoryInfo(Path)
End If
Return myIO.Exists()
End Function

Then read up about the Imports Statement, to make your coding more concise:

Imports System.IO

Public Function mP_IsPathExist(ByVal Path As String) As Boolean
On Error Resume Next
Dim myIO As New FileInfo(Path)
If Not myIO.Exists() Then
myIO = New DirectoryInfo(Path)
End If
Return myIO.Exists()
End Function

Next, get shot of "On Error" - there are much, /much/ better ways of
doing Error Handling now:

Imports System.IO

Public Function mP_IsPathExist(ByVal Path As String) As Boolean
Try
Dim myIO As New FileInfo(Path)
If Not myIO.Exists() Then
myIO = New DirectoryInfo(Path)
End If
Return myIO.Exists()

Catch ex As Exception
' Do something about the Exception
' ... then ...
Return False
End Try
End Function

Then, read up on the File and Directory classes in the System.IO
Namespace which, IIRC, will be quicker for this particular test:

Imports System.IO

Public Function mP_IsPathExist(ByVal Path As String) As Boolean
Try
If File.Exists( Path ) Then
Return True
ElseIf Directory.Exists( Path )Then
Return True
End If
Return False

Catch ex As Exception
' Do something about the Exception
' ... then ...
Return False

End Try
End Function

HTH,
Phill W.
Jul 24 '07 #4
Phill W. wrote:
Just assign a new value to the variable (i.e. drop the second Dim).

Public Function mP_IsPathExist(ByVal Path As String) As Boolean
On Error Resume Next
Dim myIO As New System.IO.FileInfo(Path)
If Not myIO.Exists() Then myIO = New
System.IO.DirectoryInfo(Path)
Return myIO.Exists()
End Function
That doesn't work. You can't assign a DirectoyInfo object to a FileInfo
reference.

--
Göran Andersson
_____
http://www.guffa.com
Jul 24 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Geoff Cox | last post by:
Hello, One person to date has received a runtime error message saying "parent.frameleft.location is not an object" with the following code. The code is used to select 2 frames at the same...
14
by: sinister | last post by:
I have some CGI programs that spit out error pages when the user enters illegal form input. These custom error pages, while informing the user of errors, are otherwise just standard web pages. ...
5
by: K. Shier | last post by:
when attempting to edit code in a class file, i see the bug "Visual Basic ..NET compiler is unable to recover from the following error: System Error &Hc0000005&(Visual Basic internal compiler...
15
by: Ajay DSouza | last post by:
When I try to validate my page http://www.ajaydsouza.com/wordpress/wpthemes/ I get this: I got the following unexpected response when trying to retrieve...
102
by: Skybuck Flying | last post by:
Sometime ago on the comp.lang.c, I saw a teacher's post asking why C compilers produce so many error messages as soon as a closing bracket is missing. The response was simply because the compiler...
3
by: Antoine | last post by:
Hello, I'm writing a program to send requests to my wlan pocket pc device (UIO1: driver) in C#. Here how I import CreateFile functions from coredll.dll with DllImport: public static extern...
39
by: eruanion | last post by:
Hi, I've been working on this for a while now and I can't seem to find out what is wrong with my code. I have 2 files one c3common.js which only contains javascript functions for my main html page...
669
by: Xah Lee | last post by:
in March, i posted a essay “What is Expressiveness in a Computer Languageâ€, archived at: http://xahlee.org/perl-python/what_is_expresiveness.html I was informed then that there is a academic...
13
by: Paul Melis | last post by:
Can someone explain to me why the following code compiles without errors on gcc 4.0.2? void f() { } void t() { f(1,2,3);
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.