Using scope in metods (Using)

  • Thread starter Thread starter Olan Meier
  • Start date Start date
O

Olan Meier

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
 
Olan Meier said:
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
 
Olan Meier said:
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.
 
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,
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.
 
Olan said:
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.
 
Back
Top