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

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



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]" <mark@xxxxxxxxxxxxxxxxx> wrote in message
news:eLrqPGNEIHA.1184@xxxxxxxxxxxxxxxxxxxxxxx
<Bub.Paulson@xxxxxxxxx> wrote in message
news:1192633761.366425.219980@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

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


.



Relevant Pages

  • use single user.config for multiple .exe projects
    ... Two of the projects in my solution that both need to use a common ... working with 99% of the time, the other is a seperate "maintenance" ... The connection string is stored via My.Settings (encrypted of ... a single file. ...
    (microsoft.public.dotnet.languages.vb)
  • Re: SQL Connection Strings
    ... They are common to the whole ... so all users are sharing the same value... ... Check out all the static (shared or module level for VB.NET) variables. ... If the connection string is NOT unique, ...
    (microsoft.public.dotnet.framework.adonet)
  • Re: A compact problem
    ... It is common to get that error when your connection string is not valid. ... Use the connection string wizard from the TadoConnection to generate your ...
    (borland.public.delphi.database.ado)