469,613 Members | 1,927 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,613 developers. It's quick & easy.

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 6763
<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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

11 posts views Thread by mrbog | last post: by
2 posts views Thread by Eddy Ilg | last post: by
8 posts views Thread by Paw Pedersen | last post: by
41 posts views Thread by none | last post: by
reply views Thread by devrayhaan | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.