basic ?: sharing variables between functions/subs

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)
 
K

Kyril Magnos

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)
|
|
|
 
D

darrel

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
 
K

Kyril Magnos

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
|
|
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top