By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,285 Members | 2,124 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,285 IT Pros & Developers. It's quick & easy.

Reusable Code (Global Variables)

P: 102
I have a set of variables that are used across multiple projects. How can the variables be reused? Can a dll be used?

Oct 22 '18 #1
Share this Question
Share on Google+
1 Reply

P: 296
Ok - first to answer your question...

No - use a module. Put it in a folder common to all the projects in which you want to use them. When you add the module to the project(s) make sure you "Add as Link." Add as link is the same as adding it to your project, but it doesn't make a local copy. Instead it uses the one in your common folder. To add as link begin by adding it the way you normally do, but in the dialog where you click on "Add" you will notice a little drop-down arrow. Add as Link is one of the choices.

Be careful though - any changes you make to the common module will impact every program that uses it - and you WILL forget all of the programs that use it.

And next I want to challenge you to seek a better design...

Certainly there are times this approach makes sense (I'm not one of those "absolute" guys that will say "never do this.") There may be a better way to do what you need. Instead of globally referring to variables perhaps it will work better to make sure you are passing values into subs and functions, then handing them back as necessary. Make use of ByRef so any change you make to the variable will be reflected upon the function/sub return. Make use of ByVal if you want the sub/function to work with its own new copy of the variable. This can be a little tricky in VB though. Elementary types like integers will get copied in ByVal passing, but strings and instantiated objects will still pass in a reference and changes will be reflected up the chain unless you pass in a cloned copy. To avoid changing a string globally you can pass in string.clone. Some native dotnet objects support cloning - for your own objects you will need to implement iCloneable and decide if it needs to be shallow or deep. Implementing a deep-cloned class can be daunting...
Feb 10 '19 #2

Post your reply

Sign in to post your reply or Sign up for a free account.