473,657 Members | 2,450 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

basic ?: sharing variables between functions/subs

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)

Nov 18 '05 #1
3 1734
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" <no*****@hotmai l.com> wrote in message
news:eO******** ******@TK2MSFTN GP12.phx.gbl...
| 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)
|
|
|
Nov 18 '05 #2
> 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
Nov 18 '05 #3
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" <no*****@hotmai l.com> wrote in message
news:eu******** ******@tk2msftn gp13.phx.gbl...
|> 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
|
|
Nov 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2732
by: Mladen Gogala | last post by:
How can I share variables between two processes? Here is my problem? File test1.php: ------------------------------------------------------------------- <?php session_start(); $var1="This should"; $var2="be displayed"; $_SESSION=$var1;
2
1662
by: Denis_dh | last post by:
Hi, I have a set of Forms and wish to have a varible used by more than one of them but can't seem to figure where to initialise it or how to acess it if I initialise it in a different form than the one that is accessing it! Any help appreciated! Denis
2
1824
by: Asha Gill | last post by:
hello all how can i share variables between asp.net pages without using the session or anything that has got to do with the global file. can i use properties for this? 1 page calling the other to accept the value Asha Gill
3
1334
by: Mothish K | last post by:
Hello, I am trying to connect 2 of my asp.net applications using context.items collections to share the variables. but it says Could not load type 'Proj2.SignIn'. I have set the authentication mode="Forms" but still it says the same..
1
1597
by: Terry Olsen | last post by:
What is the best practice for sharing variables across modules in the same project? I'm doing a console app with different modules for different functions (file i/o, sql commands, string manipulation, etc). But I have variables in the main module that other modules need to access such as AppPath, etc. What is the "best practice" concerning a situation like this?
6
1656
by: The Colonel | last post by:
In a .vb class function, can I assign user-specific information (e.g. First Name, Role, etc.) to local variables (as opposed to referring to the session variable multiple times)?
1
1627
by: Terry | last post by:
Hi Language: VBA (Excel) I am wanting to execute different functions/subs dependent on a value picked up in a cell on Excel. For example if Cell A1 has the value "name" then I want to be able to run the NewName() sub and send to it the value of Cell B1 (which will contain a name). However, if Cell A1 contains the value "Age" then I want to run the NewAge() sub and send it the value of cell B1 (which will contain the age). The reason...
8
23324
by: vishalsethia | last post by:
I was wondering if I declare a variable as global , can I share the same variable in a different file with the updated value. (ie) A global variable value to be used between different files without passing them within functions. Say for eg. Script 1 global foo = "sample" def func1
6
1727
by: awhan.iitk | last post by:
I have a set of variables that I want to share across mulitple c++ files. I was using the extern method so far. Is there any other way to do the same. The variables are not constants and I get the values during run time.
0
8421
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8325
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8742
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8518
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8621
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7354
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5643
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4173
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4330
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.