By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,341 Members | 1,726 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,341 IT Pros & Developers. It's quick & easy.

New to VB .NET - Error message not understood

P: n/a
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
Share this Question
Share on Google+
4 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.