473,785 Members | 2,185 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2467
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******** ******@TK2MSFTN GP09.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.co m>
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******** ******@TK2MSFTN GP11.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******** ******@TK2MSFTN GP09.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.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
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.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Jul 21 '05 #5

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

Similar topics

6
19062
by: rick | last post by:
Noob problem. I prefer to keep all my scripts in an external '.js' file. I am currently loading the external '.js' file from the header. Problem is I would like to declare a global variable in the external file, but I keep getting an error about the object does not exist. Can someone tell me where or how to declare a global variable in an external file that is available after the page is loaded.
7
1464
by: Simon Osborn | last post by:
Hi, Instead of adding my own custom dll's as references into my project, I want to call it via declare statement. However, I'm having a few difficulties! My dll contains a basic function : Public Class MyDLL Public shared function MyTestFunction (byval mystr as string) try
5
4610
by: Sakharam Phapale | last post by:
Hi All, How to declare the following statement in following structure. szPname As String * MAXPNAMELEN Public Structure MIXERCAPS public wMid As Integer public wPid As Integer public vDriverVersion As Long
4
235
by: wk6pack | last post by:
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
5
2374
by: iwdu15 | last post by:
hi, i need to have a declare statement for an API to call a method in it, the only problem is that i dont neccessarily know where on the comp it will be. I kno it will be in my application folder, but how do i say that in the declares statement? for instance, i have this Private Declare Sub AccCreateSession Lib "acccore.dll" Alias "#111" _ (<MarshalAs(UnmanagedType.LPStruct)ByVal Name As Guid, <MarshalAs(UnmanagedType.IDispatch)ByRef...
6
7773
by: John Bailo | last post by:
I created a sproc with 3 input parameters and one output parameter. I want to test it in the "Run SQL Scripts" app of Navigator. I wrote this code: DECLARE RTNDATE CHAR(10) DEFAULT ''; CALL MANLIB.MANDATCL('ADD','20060101','1',RTNDATE) SELECT MYVAR;
2
13460
by: juan.gautier | last post by:
Hi, I try to construct a SQL code for a view to select a specific data from a table, this query take the value of the filter from a text box in a visual basic 6.0 form. my problem is when i DECLARE the variable @AMP receive a error message that said "The Declare cursor SQL construct or statement is not supported." What i do wrong? Please help me. i attach the code. DECLARE @AMP int;
1
3782
by: okonita | last post by:
Hi all, My environment is DB2 UDBv8.2 on Windows and Linux. I am getting a lot of errors on the script you see below. Some of the error I am geting are: -----------------------------------Start of Script----------------------------------- ---------------------original creation by Norm Wong------------------------ CREATE PROCEDURE DB2ADMIN.REORGTS( ) DYNAMIC RESULT SETS 1
3
2454
by: Lennart | last post by:
Any thoughts on the following scenario anyone? During a performance test I discovered that the application asked one specific query extremely often. It turned out that this particular query where asked 25/50/100 or 200 times from a "htmlpage", dependent of user preferences. I figured that using a global temp table, looping and inserting, then join would do the trick. However, it turned out that this killed performance totally. Why,...
0
9646
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10350
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10157
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9957
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8983
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5386
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5518
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4055
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2887
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.