basic ?: sharing variables between functions/subs

Discussion in 'ASP .Net' started by darrel, Jul 15, 2004.

  1. darrel

    darrel Guest

    This is something I should know, but I don't.

    Say I have this:

    Function
    dim variable1
    dim variable2

    do stuff with the variables

    otherFunction()

    End Function

    function OtherFunction()
    do stuff with the variables
    end function

    How can I get the second function to see the same variables as the first
    one? I could obviously pass each one through the function call itself, but
    is there a simpler way? Perhaps declaring the first function variables
    public or have the second one inherit them? This is where my lack of OOP
    understanding shows ;o)
    darrel, Jul 15, 2004
    #1
    1. Advertising

  2. darrel

    Kyril Magnos Guest

    Hi Darrel,

    It's basically up to you and what you intend to do with these variables. If
    they are only needed for the life time of these functions, then you should
    pass them in as parameters to your second function. If they are going to be
    accessed and used by other methods or functions that, then you want to
    declare them outside of the function, at the class.

    Variables that are declared within functions are considered to have local
    scope and are disposed as soon as the function returns. In general, it is
    best to declare your variables with the narrowest scope possible.

    --
    HTH

    Kyril Magnos

    Disclaimer: Thinking may cause permanent thought processes to form and be
    used.
    "darrel" <> wrote in message
    news:...
    | This is something I should know, but I don't.
    |
    | Say I have this:
    |
    | Function
    | dim variable1
    | dim variable2
    |
    | do stuff with the variables
    |
    | otherFunction()
    |
    | End Function
    |
    | function OtherFunction()
    | do stuff with the variables
    | end function
    |
    | How can I get the second function to see the same variables as the first
    | one? I could obviously pass each one through the function call itself, but
    | is there a simpler way? Perhaps declaring the first function variables
    | public or have the second one inherit them? This is where my lack of OOP
    | understanding shows ;o)
    |
    |
    |
    Kyril Magnos, Jul 15, 2004
    #2
    1. Advertising

  3. darrel

    darrel Guest

    > Variables that are declared within functions are considered to have local
    > scope and are disposed as soon as the function returns. In general, it is
    > best to declare your variables with the narrowest scope possible.


    Well, here's the specifics.

    It's an image upload and resize tool.

    The main function gets the initial file and then sets a variety of
    variables, namely some different paths to different folders.

    Then I call the actual function to resize the image. I'm calling this as a
    separate function since I need to call it several times, saving different
    images each time.

    So, I did some digging and what I finally did was just declare these
    variables as public at the top of the codebehind page. This seems to work.
    What is the drawback (if any) to this method?

    -Darrel
    darrel, Jul 15, 2004
    #3
  4. darrel

    Kyril Magnos Guest

    Well, declaring anything as public in your class basically says to the world
    "Here I am, you can change or use me anytime you want with no
    restrictions.". ;)

    If you don't want outside consumers to be able to change your variables,
    then I would mark them as private, especially if they are only being used in
    this one class.

    --
    HTH

    Kyril Magnos

    Disclaimer: Thinking may cause permanent thought processes to form and be
    used.
    "darrel" <> wrote in message
    news:...
    |> Variables that are declared within functions are considered to have local
    | > scope and are disposed as soon as the function returns. In general, it
    is
    | > best to declare your variables with the narrowest scope possible.
    |
    | Well, here's the specifics.
    |
    | It's an image upload and resize tool.
    |
    | The main function gets the initial file and then sets a variety of
    | variables, namely some different paths to different folders.
    |
    | Then I call the actual function to resize the image. I'm calling this as a
    | separate function since I need to call it several times, saving different
    | images each time.
    |
    | So, I did some digging and what I finally did was just declare these
    | variables as public at the top of the codebehind page. This seems to work.
    | What is the drawback (if any) to this method?
    |
    | -Darrel
    |
    |
    Kyril Magnos, Jul 15, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Steffen Loringer

    Useless variables/Subs checking

    Steffen Loringer, Jan 30, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    344
    Rob Meade
    Jan 30, 2004
  2. =?Utf-8?B?QXNoYSBHaWxs?=

    Sharing variables between pages...

    =?Utf-8?B?QXNoYSBHaWxs?=, Jun 9, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    1,879
    Pete Wright
    Jun 9, 2004
  3. The Colonel
    Replies:
    6
    Views:
    4,386
    Hans Kesting
    Jun 7, 2006
  4. Colin Mc Mahon

    Functions vs. subs and role reversal

    Colin Mc Mahon, Sep 12, 2003, in forum: ASP General
    Replies:
    4
    Views:
    111
    Chris Barber
    Sep 13, 2003
  5. Gonzosez

    creating files for functions and subs

    Gonzosez, Jan 3, 2006, in forum: ASP General
    Replies:
    2
    Views:
    111
    Tim Slattery
    Jan 3, 2006
Loading...

Share This Page