473,473 Members | 1,768 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Centralizing global variables and functions - good & simple method?

I posted this on another forum, and as I feared, the response(s) were too
complex and sophisticated. I certainly don't mind learning new methods, in
fact, that's why I asked, but I was hoping to emphasize that needed to take
small steps. The responses I received talked about Public Classes and DLLs
and such. =(

I'm tinkering in ASP.NET 2 (VB) and have been tinkering with VWD for about a
couple of weeks. I've written a few app pages that use a simple code-behind
method where I place all my subs and functions on a .vb page and just
"include" on the needed page like so (spaces added intentionally):

< % @ Page language="VB" CodeFile="CodeFiles / default_aspx . vb"
Inherits="LogInClass" % >

This seems to work well. I've managed to place my MSSQL connection string
in my web.config file to centralize this connect string. But what would you
recommend as far as centralizing all "global" values and possibly subs and
functions?

I have a few subs and functions that I can use on many different pages so it
would make sense to place them in one place. Please keep in mind that I'm
new to .NET (but not ASP programming) and VWD. Up until this point, I've
coded all my asp inline by hand and using Dreamweaver to manage / build
sites. I'm looking for a effectice but simple way to do this.

Would it be as easy as creating another .vb file and "including" it in the
required pages too? Or would I create a ascx file and use @Register it to
"include" it in these .vb files?

Thanks!


Feb 13 '06 #1
6 1531
hi,
i would suggest you have a quick read of namespaces, to make sure you
understand how to reference code in different places. this page seems
like a good article:
http://www.vbdotnetheaven.com/Code/Jun2003/2030.asp

i use Visual Studio, but i'm sure VWD is similar. you can create a
folder called 'App_Code' which provides a place for you to put the type
of 'shared' code that you are talking about. you must create a class
to put the code in, i always call my class "Util" because it stores
miscellaneous 'utility' subs or functions that i use in different pages
in the site. so you just create a class called Util, and add in your
subs to the class. make sure they are all public. to make it easier
to use the subs, mark them as 'Shared'
e.g. public shared sub DoWhatever()

then in your vb code behind file, you can just call Util.DoWhatever()
without having to worry about creating a Util object and using it etc.
if you don't go with 'shared' subs, you have to do something like

Dim u as new Util()
u.DoWhatever()

hope this helps. let me know if something is not clear.
tim

Feb 13 '06 #2
info on the the App_Code folder from MSDN:
http://msdn2.microsoft.com/en-us/library/t990ks23.aspx

also i forgot to mention, use the same technique to store 'global'
variables inside the Util class. look up MSDN or google on using const
variables.

as another tip, you may find over time that your 'const' variables are
not so constant. e.g. if they store regular expressions or strings or
settings etc. It is a good idea to put them in web.config and have a
shared function that reads it from the web.config. this technique will
usually give your app a longer lifetime.

Feb 13 '06 #3
Thank you! That's not soo bad. I'll experiment now and post bacck if I
have issues.....

Thanks again.
"Tim_Mac" <ma********@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
info on the the App_Code folder from MSDN:
http://msdn2.microsoft.com/en-us/library/t990ks23.aspx

also i forgot to mention, use the same technique to store 'global'
variables inside the Util class. look up MSDN or google on using const
variables.

as another tip, you may find over time that your 'const' variables are
not so constant. e.g. if they store regular expressions or strings or
settings etc. It is a good idea to put them in web.config and have a
shared function that reads it from the web.config. this technique will
usually give your app a longer lifetime.

Feb 13 '06 #4
OK. I'm having some success. What would be the method of declaring public
variables and values? I'm getting subs and functions to work just fine. For
example...

If my MyClass.vb file (in the App_Code folder) looks like this, how could I
define a variable like strFName to be avail anywhere from any page by using
MyClass.strFName? Or am I way off?
Imports Microsoft.VisualBasic

Public Class MyClass

Dim strFName As String
strFName = "John"
Public Shared MyFunction1 () As String
'yadda
End Funtion
Public Shared MySub1()
'yadda
End Sub

End Class


"D. Shane Fowlkes" <sh**********@h-o-t-m-a-i-l.com> wrote in message
news:u5**************@TK2MSFTNGP11.phx.gbl...
Thank you! That's not soo bad. I'll experiment now and post bacck if I
have issues.....

Thanks again.
"Tim_Mac" <ma********@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
info on the the App_Code folder from MSDN:
http://msdn2.microsoft.com/en-us/library/t990ks23.aspx

also i forgot to mention, use the same technique to store 'global'
variables inside the Util class. look up MSDN or google on using const
variables.

as another tip, you may find over time that your 'const' variables are
not so constant. e.g. if they store regular expressions or strings or
settings etc. It is a good idea to put them in web.config and have a
shared function that reads it from the web.config. this technique will
usually give your app a longer lifetime.


Feb 13 '06 #5
hi shane,
one of the subtleties of the way classes work is that when you don't
provide a modifier (public or private etc) for a method or variable,
then it defaults to private.
so if you use MyClass.strFName it will not work because strFname is a
private variable.
try the following syntax:
Public strFName as String

when you do that, would still need to create a 'MyClass' object before
you could use the variable. because you just want simple access to the
variable, without having to worry about creating objects, make it a
'const' variable.
Public Const strFName As String = "whatever"

hope this helps. i would recommend you look up a few online tutorials
about working with objects, and get stuck in with a few examples. this
area is typically where VB developers with an ASP background need to
brush up on, speaking from experience!
it will make your programming transition to .Net much easier, as you
will be better equipped to make good use of the existing classes in the
..Net framework, and you'll design your own classes much better as a
result.

tim

Feb 13 '06 #6
Thanks. I know I have much to learn....but the deadlines still come whether
I take the time to learn or not. =)
"Tim_Mac" <ma********@gmail.com> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com...
hi shane,
one of the subtleties of the way classes work is that when you don't
provide a modifier (public or private etc) for a method or variable,
then it defaults to private.
so if you use MyClass.strFName it will not work because strFname is a
private variable.
try the following syntax:
Public strFName as String

when you do that, would still need to create a 'MyClass' object before
you could use the variable. because you just want simple access to the
variable, without having to worry about creating objects, make it a
'const' variable.
Public Const strFName As String = "whatever"

hope this helps. i would recommend you look up a few online tutorials
about working with objects, and get stuck in with a few examples. this
area is typically where VB developers with an ASP background need to
brush up on, speaking from experience!
it will make your programming transition to .Net much easier, as you
will be better equipped to make good use of the existing classes in the
.Net framework, and you'll design your own classes much better as a
result.

tim

Feb 14 '06 #7

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

Similar topics

88
by: Tim Tyler | last post by:
PHP puts most of its functions into a big flat global namespace. That leads to short function names - but creates a namespace minefield for programmers. Lots of the functions are legacies from...
10
by: Matt | last post by:
Greetings, What are people's thoughts on global variables in C++? Why are we taught not to use them in programming? Is it true that if you are running two copies of the C program one copy can...
7
by: Michael | last post by:
Hi newsgroup, as the subject indicates I am looking for an advice using global variables. I am not if this problem is more about style then C. If its wrong in thi group, sorry. So I have a...
2
by: Nathan Sokalski | last post by:
I would like to access variables and functions that I declare in the Global.asax.vb file. However, I am having trouble doing that. What does the declaration have to look like in the Global.asax.vb...
7
by: zeecanvas | last post by:
Hi, First of all: Yes, I know global variables are bad, but I've a huge amount of legacy code, and I've to maintain it _as_is_. I'm maintaining a big program. I moved all (program-wide scope)...
37
by: eoindeb | last post by:
Sorry to ask another global variable question, but from reading other posts I'm still not sure whether to use them or not. I have a program with a set function that calls 4 other functions in...
4
by: Sheldon | last post by:
Hi, I have a series of classes that are all within the same file. Each is called at different times by the main script. Now I have discovered that I need several variables returned to the main...
5
by: Sandman | last post by:
I dont think I understand them. I've read the section on scope in the manual inside out. I'm running PHP 5.2.0 Here is the code I'm working on: //include_me.php <?php $MYVAR = array(); global...
112
by: istillshine | last post by:
When I control if I print messages, I usually use a global variable "int silent". When I set "-silent" flag in my command line parameters, I set silent = 1 in my main.c. I have many functions...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
1
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...
0
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...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.