473,399 Members | 2,146 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.

Using scope in metods (Using)

Hi,

I like to define scope in a method to ensure that variables donøt get used
more than once, and that things are nicely disposed. I use the Using-keyword
most of the time - but what do one use if theres no logical way to do Using
(you can't say Using tmp as string=""). I do a "If True - End if" to make a
scope - but thats stupid. Whats the right way to do it?

Thanks
Jan 19 '07 #1
5 939
"Olan Meier" <om@haps.comschrieb
Hi,

I like to define scope in a method to ensure that variables donøt
get used more than once, and that things are nicely disposed. I use
the Using-keyword most of the time - but what do one use if theres
no logical way to do Using (you can't say Using tmp as string=""). I
do a "If True - End if" to make a scope - but thats stupid. Whats
the right way to do it?

I sometimes do the "if true then" thing also. I think there is no other way
(unless another procedure).
Armin

Jan 19 '07 #2
"Olan Meier" <om@haps.comschrieb:
I like to define scope in a method to ensure that variables donøt get used
more than once, and that things are nicely disposed. I use the
Using-keyword most of the time - but what do one use if theres no logical
way to do Using (you can't say Using tmp as string=""). I do a "If True -
End if" to make a scope - but thats stupid. Whats the right way to do it?
Reduce the size of the procedures to prevent such problems. VB does not
support a scoping construct like '{ ... }' in C#, for example.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Jan 19 '07 #3
You're damn right it's stupid.

Thr right way to do it is to apply self-discipline in the usage of variables
and proper disposal of objects.

If you really want to define scope down to small sections of code then
define that section of code in it's own method and call it when necessary.
But that too, is overkill.
"Olan Meier" <om@haps.comwrote in message
news:Ob**************@TK2MSFTNGP02.phx.gbl...
Hi,

I like to define scope in a method to ensure that variables donøt get used
more than once, and that things are nicely disposed. I use the
Using-keyword most of the time - but what do one use if theres no logical
way to do Using (you can't say Using tmp as string=""). I do a "If True -
End if" to make a scope - but thats stupid. Whats the right way to do it?

Thanks

Jan 19 '07 #4
Olan,
As Herfried suggests: I normally write smaller methods to limit the scope of
variables in said methods.

In addition to the Using statement you can use a For or For Each statements
to introduce a new scope.

For index As Integer = 1 to 10
Next

For Each item As Whatever In whatevers
Next

NOTE: The Using statement should *not* be used to introduce a new scope,
rather it *should* be used to control unmanaged resources. Specifically it
is used to encapsulate a Try/Finally block around an object that implements
IDisposable! For example use a Using statement to ensure a file you are
reading is closed, or a Pen you are using is properly cleaned up.

--
Hope this helps
Jay B. Harlow [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
"Olan Meier" <om@haps.comwrote in message
news:Ob**************@TK2MSFTNGP02.phx.gbl...
Hi,

I like to define scope in a method to ensure that variables donøt get used
more than once, and that things are nicely disposed. I use the
Using-keyword most of the time - but what do one use if theres no logical
way to do Using (you can't say Using tmp as string=""). I do a "If True -
End if" to make a scope - but thats stupid. Whats the right way to do it?

Thanks
Jan 20 '07 #5
Olan Meier wrote:
Hi,

I like to define scope in a method to ensure that variables donøt get used
more than once, and that things are nicely disposed. I use the Using-keyword
most of the time - but what do one use if theres no logical way to do Using
(you can't say Using tmp as string=""). I do a "If True - End if" to make a
scope - but thats stupid. Whats the right way to do it?
<snip>

Unfortunately there's no way to do it in VB without looking stupid or
worse -- cryptic.

I remember using, in another life, a Do...Loop:

Do 'put a suitable descriptive name, here
...
Loop Until True

Or

Do 'put a suitable descriptive name, here
...

Exit Do
Loop

In my opinion, the advantage of this (really stupid) method over If
True Then... End If is that you can easily exit the block.

Regarghs,

Branco.

Jan 20 '07 #6

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

Similar topics

9
by: Roger Withnell | last post by:
I'm inserting a new record into an MS SQL database table and I want to obtain the new records autonumber immediately afterwards, as follows: MadminRS.CursorLocation = adUseServer...
2
by: Brian Worth | last post by:
I have just upgraded from VB 4.0 to VB .NET 2002. One program under VB 4.0 was able to shut down or restart the (windows XP) machine using a series of API calls. (Getlasterror, GetCurrentProcess,...
13
by: Andy Baxter | last post by:
Can anyone recommend a good online guide to using objects in javascript? The book I bought (DHTML Utopia) suggests using objects to keep the code clean and stop namespace clashes between different...
4
by: Niels Dekker (no reply address) | last post by:
When calling swap as follows (as recommanded in Effective C++, 3rd Edition, by Scott Meyers), what swap is chosen to be called? using std::swap; swap(a, b); Suppose there is a global ::swap...
7
by: garyusenet | last post by:
This is the first time i've worked with openfile dialog. I'm getting a couple of errors with my very basic code. Can someone point out the errors in what i've done please....
4
by: Karol Kowcik | last post by:
Here's an example of 3 ways of using a method of an object with a different object context. Is there a signficant difference between method 1., where a reference to beta's method is copied and 2.,...
3
by: VRSki | last post by:
Hello, Is there any way to use "using" for aliasing in the global scope? The example below works fine in the context of a given file, but in the different file MyByte alias is unknown. Is...
2
by: GaryDean | last post by:
When I use transactions with sql server I usually do this... using (TransactionScope scope = new TransactionScope) { using (SqlConnection1 = new SqlConnection . . . . . and this all works...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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...
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.