Centralizing global variables and functions - good & simple method?

D

D. Shane Fowlkes

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

Tim_Mac

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
 
T

Tim_Mac

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

D. Shane Fowlkes

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

Thanks again.
 
D

D. Shane Fowlkes

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
 
T

Tim_Mac

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
 
D

D. Shane Fowlkes

Thanks. I know I have much to learn....but the deadlines still come whether
I take the time to learn or not. =)
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top