basic ?: sharing variables between functions/subs

  • Thread starter Thread starter darrel
  • Start date Start date
D

darrel

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)
 
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.
| 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)
|
|
|
 
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
 
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.
|> 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
|
|
 
Back
Top