473,322 Members | 1,347 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Best place for "Global Variables" and Methods in C# though ASP.Net

A month ago I finally took the plunge and began learning C# and
ASP.Net, coming from a Classic ASP and VBScript background.

In my classic ASP, I had my own little library of code that I stuck in
an include file called "Common_VBscript.asp" which had all of the
common stuff I used. (Constants, Connection Strings, little utlities
like "email validation" functions, or functions to ready strings for
db entry, etc.)

What is the best way for me to have a "common code" module tacked on
to all (or many) of my ASP.Net pages for a particular project? And
how can I do it, precisely?

Thanks,
B.P.

Oct 17 '07 #1
10 6907
<Bu*********@gmail.comwrote in message
news:11**********************@e34g2000pro.googlegr oups.com...
>A month ago I finally took the plunge and began learning C# and
ASP.Net, coming from a Classic ASP and VBScript background.

In my classic ASP, I had my own little library of code that I stuck in
an include file called "Common_VBscript.asp" which had all of the
common stuff I used. (Constants, Connection Strings, little utlities
like "email validation" functions, or functions to ready strings for
db entry, etc.)

What is the best way for me to have a "common code" module tacked on
to all (or many) of my ASP.Net pages for a particular project? And
how can I do it, precisely?
ASP.NET, like the rest of the .NET Framework, is object-orientated.
Generally speaking, if you're looking for "global" anything, you probably
need a bit of a refresher on OOP...

An obvious solution is to create a new class with common functions. Then,
whenever you need one or more of them, just instantiate the class and use
the function(s) you need...

In addition, C# supports classes with static methods which you can use
anywhere in your code without instantiating the class. Purists may flinch at
doing this, but it can be useful:
http://www.eggheadcafe.com/articles/20020206.asp

And, purely in the interests of balance, VB.NET can do the same thing with
its Shared methods...

However, be very wary of static variables, as they will be common across all
sessions, which can lead to some very unexpected and unwanted results...
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Oct 17 '07 #2
in addition use the web.config for encrypted connection string and other
configurations, may additionally use XML config files etc..

may want to have some aspx pages inherit from some base with common
functionality.

"Mark Rae [MVP]" <ma**@markNOSPAMrae.netwrote in message
news:eL**************@TK2MSFTNGP04.phx.gbl...
<Bu*********@gmail.comwrote in message
news:11**********************@e34g2000pro.googlegr oups.com...
>>A month ago I finally took the plunge and began learning C# and
ASP.Net, coming from a Classic ASP and VBScript background.

In my classic ASP, I had my own little library of code that I stuck in
an include file called "Common_VBscript.asp" which had all of the
common stuff I used. (Constants, Connection Strings, little utlities
like "email validation" functions, or functions to ready strings for
db entry, etc.)

What is the best way for me to have a "common code" module tacked on
to all (or many) of my ASP.Net pages for a particular project? And
how can I do it, precisely?

ASP.NET, like the rest of the .NET Framework, is object-orientated.
Generally speaking, if you're looking for "global" anything, you probably
need a bit of a refresher on OOP...

An obvious solution is to create a new class with common functions. Then,
whenever you need one or more of them, just instantiate the class and use
the function(s) you need...

In addition, C# supports classes with static methods which you can use
anywhere in your code without instantiating the class. Purists may flinch
at doing this, but it can be useful:
http://www.eggheadcafe.com/articles/20020206.asp

And, purely in the interests of balance, VB.NET can do the same thing with
its Shared methods...

However, be very wary of static variables, as they will be common across
all sessions, which can lead to some very unexpected and unwanted
results...
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Oct 17 '07 #3
Thank you for the reply! A few questions based on your comments
follow:
An obvious solution is to create a new class with common functions. Then,
whenever you need one or more of them, just instantiate the class and use
the function(s) you need...
Yes, that sounds terrific, and is what I had in mind. Trouble is,
while I understand how to make "Web Forms" (ASPX files) and "Web User
Controls" (ASCX files), making a separate class and being able to
include it only in needed pages escapes me. In Visual Studio, I can
see "make class" when adding a new item, but it wants to stick it into
"App_Code", which I do not think is where I want it. (This will make
whatever static variables I use common across the entire application,
right? There are many times where I am using variables I only want
"common" to particular sessions.)

Can someone give me explicit directions on how to include a Class that
I have made (a file called "Common.cs) for instance in specific pages
that use methods and variables that it includes, without using
"App_Code", or _SHOULD_ I be using "App_Code" for this?

Thanks,
B.Paulson

Oct 17 '07 #4
do not use static then if it is not common to all. or have a mix of static
that is common to all and Instance variables, members, or Methods.

create your new class, and it does not matter where VS.NET put it leave it
where it wants it to be.

then Instantiate your class where you want to use it. ( if in the same
namespace no need to add a reference )

Dim myClass as Common = new Common()

You can have properties or Methods in your common class.

you can hydrate your new object myClass with session-data via properties or
constructor.

you can call methods on the object myClass as you wish.

for static members you don't need to instantiate.

<Bu*********@gmail.comwrote in message
news:11**********************@e34g2000pro.googlegr oups.com...
Thank you for the reply! A few questions based on your comments
follow:
>An obvious solution is to create a new class with common functions. Then,
whenever you need one or more of them, just instantiate the class and use
the function(s) you need...

Yes, that sounds terrific, and is what I had in mind. Trouble is,
while I understand how to make "Web Forms" (ASPX files) and "Web User
Controls" (ASCX files), making a separate class and being able to
include it only in needed pages escapes me. In Visual Studio, I can
see "make class" when adding a new item, but it wants to stick it into
"App_Code", which I do not think is where I want it. (This will make
whatever static variables I use common across the entire application,
right? There are many times where I am using variables I only want
"common" to particular sessions.)

Can someone give me explicit directions on how to include a Class that
I have made (a file called "Common.cs) for instance in specific pages
that use methods and variables that it includes, without using
"App_Code", or _SHOULD_ I be using "App_Code" for this?

Thanks,
B.Paulson

Oct 17 '07 #5
"IfThenElse" <sq**********@hotmail.comwrote in message
news:OG**************@TK2MSFTNGP05.phx.gbl...
>I have made (a file called "Common.cs) for instance in specific pages

Dim myClass as Common = new Common()
He's using C#...
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Oct 17 '07 #6
Sorry,
Common myClass = new myClass(...);
"Mark Rae [MVP]" <ma**@markNOSPAMrae.netwrote in message
news:u2**************@TK2MSFTNGP03.phx.gbl...
"IfThenElse" <sq**********@hotmail.comwrote in message
news:OG**************@TK2MSFTNGP05.phx.gbl...
>>I have made (a file called "Common.cs) for instance in specific pages

Dim myClass as Common = new Common()

He's using C#...
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Oct 17 '07 #7
Sorry,

Common myClass = new Common(...);
"Mark Rae [MVP]" <ma**@markNOSPAMrae.netwrote in message
news:u2**************@TK2MSFTNGP03.phx.gbl...
"IfThenElse" <sq**********@hotmail.comwrote in message
news:OG**************@TK2MSFTNGP05.phx.gbl...
>>I have made (a file called "Common.cs) for instance in specific pages

Dim myClass as Common = new Common()

He's using C#...
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Oct 18 '07 #8
On Oct 17, 5:09 pm, Bub.Paul...@gmail.com wrote:
A month ago I finally took the plunge and began learning C# and
ASP.Net, coming from a Classic ASP and VBScript background.

In my classic ASP, I had my own little library of code that I stuck in
an include file called "Common_VBscript.asp" which had all of the
common stuff I used. (Constants, Connection Strings, little utlities
like "email validation" functions, or functions to ready strings for
db entry, etc.)

What is the best way for me to have a "common code" module tacked on
to all (or many) of my ASP.Net pages for a particular project? And
how can I do it, precisely?

Thanks,
B.P.
for this thing I alway use Static Classes, and they never have never
member variables as problems will occure as mentioned in other thread
when sharing data this way

Oct 18 '07 #9
Thanks for the help, folks. (Esp. IfThenElse, ;))

I will give it a whirl and report back.

Thanks,
B.P.

Oct 19 '07 #10
On Oct 19, 1:58 pm, Bub.Paul...@gmail.com wrote:
Thanks for the help, folks. (Esp. IfThenElse, ;))

I will give it a whirl and report back.

Thanks,
B.P.
A couple weeks later and I have learned A LOT!

The responses here were great, but this page from a person with a
similar background really drove things home:

http://www.mikesdotnetting.com/Artic...x?ArticleID=17

So... I thought I would give this thread some "closure" by including a
useful link in case someone ever reads this thread seeking answers...

B.P.

Nov 8 '07 #11

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

Similar topics

11
by: mrbog | last post by:
I have an array/hash that stores path information for my app. As in, what directory this is in, what directory that's in, what the name of the site is, what the products are called, etc. It's...
2
by: Eddy Ilg | last post by:
Hi, I am having a problem with an application I am writing: I have 2 scripts called 'conf' and 'build'. Both define a variable named 'root' and import a module named 'helper'. In the...
7
by: Lyn | last post by:
Hi and Season's Greetings to all. I have a question regarding the use of a qualifier word "Global". I cannot find any reference to this in Access help, nor in books or on the Internet. "Global"...
5
by: j | last post by:
Anyone here feel that "global variables" is misleading for variables whose scope is file scope? "global" seems to imply global visibility, while this isn't true for variables whose scope is file...
8
by: Paw Pedersen | last post by:
Is there a way to save a variabel that can be access from a static method? I hope there would be some way to save it in memory so I don't have to save it in a file. It's only for a few minutes the...
4
by: BB | last post by:
Hello all, I might be missing something here, but am trying to understand the difference between using application-level variables--i.e. Application("MyVar")--and global variables--i.e. public...
5
by: dave | last post by:
If I have a class that hold, for instance, user settings that should be accessible to the entire program logic, what is a good paradigm to use? In C++, I would have made it a global object,...
11
by: eBob.com | last post by:
I have this nasty problem with Shared methods and what I think of as "global storage" - i.e. storage declared outside of any subroutines or functions. In the simple example below this "global"...
41
by: none | last post by:
Hello, IIRC, I once saw an explanation how Python doesn't have "variables" in the sense that, say, C does, and instead has bindings from names to objects. Does anyone have a link? Thanks, ...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.