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

B

Bub.Paulson

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

Mark Rae [MVP]

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

IfThenElse

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

Bub.Paulson

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
 
I

IfThenElse

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

kaza

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
 
B

Bub.Paulson

Thanks for the help, folks. (Esp. IfThenElse, ;))

I will give it a whirl and report back.

Thanks,
B.P.
 
B

Bub.Paulson

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/Article.aspx?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.
 

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

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,050
Latest member
AngelS122

Latest Threads

Top