"Global" variables in vb.net
Posted on 2014-11-03
As you might know from my recent questions, I am converting Access fron-end apps to VB.Net apps (Windows-based currently), and I am mightily confused with the "scope" of variables compared to Access.
Let me explain: in Access, I always have forms with code-behind, usually an app-specific module with all needed logic in subroutines and functions, and a raft of generic routines / functions which are used in many apps. These generic sub's/functions are located in theme-related modules, and they can be called from anywhere: forms and modules.
In VB.net, I took one of my most complex app (not optimal as choice for the "VB.Net newbie" that I am, but it's the one which should be replaced first), and started to convert the main form, the app-specific module and some of the generic modules. For the moment, I have kept all these in the same project, which is, I know, not how it should be, but gives me time to solve the myriad of other problems I encounter with VB.Net. I have defined the generic modules in Access as public modules in VB.net, and the app-specific module and the form behind-code as public subs within the form class.
But one thing is not clear to me at all: the scope of variables/objects. I don't know how to define (if it is possible) some variables/objects so that they can be accessed by any piece of code in the whole solution. I'm thinking of objects such as datasets, datatables, connections, even such mundane things such as the app's name. In order for variables/objects to be available to a module's subroutine, do they always have to be passed by the caller in the call ? As i have sometimes a hierarchy of calls (A calls B, which in turn calls C and D, the latter calls E), do I have to have a long list of params in every call so that the called routine can access the objects, or is there another way ?
Also I had some weird behavior with input and output variables (results of the called routine) passed in a call if I didn't mention a ByVal or ByRef. Are these absolutly mandatory in the called routines ? In some cases, I do not specify ByRef or ByVal in the called routines, and it works, in other cases it does not.
Can you give me some enlightening, clear pointers or doc about these themes ?
Thanks for help