469,913 Members | 1,947 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,913 developers. It's quick & easy.

declare DIM within TRY statement

Hi,

I was wondering why when I declare the dim variable outside the try
statement, I could use the .dispose() function but when I declare it inside
the try statement, I get Name 'varname' is not declared.

thanks,
Will
Jul 21 '05 #1
4 2227
Will,
It has to do with variable scope.

If you use:

Dim x As IDisposable
Try
Finally
x.Dispose
End Try

The x variable is at the same scope as the Try itself.

However if you do:

Try
Dim y As IDisposable
Finally
y.Dispose
End Try

The y variable is in the Try's block scope.

Remember that most block statements in VB.NET introduces a new block scope,
allowing each scope to define the same variable.

For details on Scope in VB.NET see:

http://msdn.microsoft.com/library/de...vbconScope.asp

Hope this helps
Jay
"wk6pack" <wk***@sd61.bc.ca> wrote in message
news:uX**************@TK2MSFTNGP09.phx.gbl...
Hi,

I was wondering why when I declare the dim variable outside the try
statement, I could use the .dispose() function but when I declare it
inside
the try statement, I get Name 'varname' is not declared.

thanks,
Will

Jul 21 '05 #2
wk6pack <wk***@sd61.bc.ca> wrote:
I was wondering why when I declare the dim variable outside the try
statement, I could use the .dispose() function but when I declare it inside
the try statement, I get Name 'varname' is not declared.


Where are you trying to use it? In the finally block? If so, that's the
problem - the scope of the try block is just the try block, and with
good reason: the code execution might not have even reached the place
where your variable is declared before an exception is thrown.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #3
thanks for the explanation and reference.

Will
"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:Oi**************@TK2MSFTNGP11.phx.gbl...
Will,
It has to do with variable scope.

If you use:

Dim x As IDisposable
Try
Finally
x.Dispose
End Try

The x variable is at the same scope as the Try itself.

However if you do:

Try
Dim y As IDisposable
Finally
y.Dispose
End Try

The y variable is in the Try's block scope.

Remember that most block statements in VB.NET introduces a new block scope, allowing each scope to define the same variable.

For details on Scope in VB.NET see:

http://msdn.microsoft.com/library/de...vbconScope.asp
Hope this helps
Jay
"wk6pack" <wk***@sd61.bc.ca> wrote in message
news:uX**************@TK2MSFTNGP09.phx.gbl...
Hi,

I was wondering why when I declare the dim variable outside the try
statement, I could use the .dispose() function but when I declare it
inside
the try statement, I get Name 'varname' is not declared.

thanks,
Will


Jul 21 '05 #4
yes, I was trying to use it in the finally block.

Will
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
wk6pack <wk***@sd61.bc.ca> wrote:
I was wondering why when I declare the dim variable outside the try
statement, I could use the .dispose() function but when I declare it inside the try statement, I get Name 'varname' is not declared.


Where are you trying to use it? In the finally block? If so, that's the
problem - the scope of the try block is just the try block, and with
good reason: the code execution might not have even reached the place
where your variable is declared before an exception is thrown.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Jul 21 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by rick | last post: by
7 posts views Thread by Simon Osborn | last post: by
5 posts views Thread by Sakharam Phapale | last post: by
4 posts views Thread by wk6pack | last post: by
6 posts views Thread by John Bailo | last post: by
1 post views Thread by Waqarahmed | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.