Using scope in metods (Using)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Olan Meier

    #1

    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


  • Armin Zingler

    #2
    Re: Using scope in metods (Using)

    "Olan Meier" <om@haps.comsch rieb
    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

    Comment

    • Herfried K. Wagner [MVP]

      #3
      Re: Using scope in metods (Using)

      "Olan Meier" <om@haps.comsch rieb:
      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/>

      Comment

      • Stephany Young

        #4
        Re: Using scope in metods (Using)

        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.comwro te in message
        news:ObDnZc8OHH A.1252@TK2MSFTN GP02.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
        >

        Comment

        • Jay B. Harlow [MVP - Outlook]

          #5
          Re: Using scope in metods (Using)

          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.comwro te in message
          news:ObDnZc8OHH A.1252@TK2MSFTN GP02.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
          >

          Comment

          • Branco Medeiros

            #6
            Re: Using scope in metods (Using)

            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.

            Comment

            Working...