473,836 Members | 1,362 Online
Bytes | Software Development & Data Engineering Community
+ 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="CodeF iles / default_aspx . vb"
Inherits="LogIn Class" % >

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 1554
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********@gma il.com> wrote in message
news:11******** **************@ o13g2000cwo.goo glegroups.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.strFNam e? Or am I way off?
Imports Microsoft.Visua lBasic

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******** ******@TK2MSFTN GP11.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********@gma il.com> wrote in message
news:11******** **************@ o13g2000cwo.goo glegroups.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.strFNam e 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********@gma il.com> wrote in message
news:11******** **************@ g43g2000cwa.goo glegroups.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.strFNam e 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
5172
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 the days before PHP got object-oriented features. For instance we currently have:
10
17895
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 overwrite another copies global variable? I know that you could overwrite a value in a global variable from a function, but you could also do that if you pass the variable in and then out again... so how is that any different?
7
3149
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 couple of function that all need the same information (all located in the same file). By now it looks like /* file beginns */
2
5218
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 file, and what would I do to access it? (I am using VB.NET for my code) Thanks. -- Nathan Sokalski njsokalski@hotmail.com http://www.nathansokalski.com/
7
2584
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) global variables outside of the files they were defined it, and created some files that just hold global variables definitions (just variables, without any function definition). So, depending on the purpose/category of variables, they're defined...
37
2756
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 order - let's say function A, B, C, D. It always calls function A first which is a function that returns a system path. Now all other functions require that variable as well (function A returns a char pointer)
4
1436
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 script. Simple, right? I thought so and simply returned the variables in a tuple: (a,b,c,d,e) = obj.method() Now I keep getting this error: "ValueError: unpack tuple of wrong size"
5
11837
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 $MYVAR, $a; ?>
112
5504
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 that may print some messages. foo(...) { if (!silent)
0
9672
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
10859
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10560
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
10601
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
10260
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
6982
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
5829
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4460
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 we have to send another system
3
3116
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.