HOWTO: Define constants for the whole site?

D

Don Wash

Hi There!

I'm trying to define constants so that I can refer those constants from any
page of my ASP.NET website. I know I can use <appSettings> in web.config XML
file but I don't want to parse and read the whole XML document everytime a
page is requested.

In classic ASP, I used Include files and define constants using Const
keyword so that every page that has been included the file can refer the
constants.

Is there any proper and easier way in ASP.NET to accomplish this? (and of
course without using web.config file)

Thanks all in advance!!
Don
 
K

Karl

There are a couple methods available to you.

[a] You can create a class with public constances
Public Class Globals
Public Const NumberOfRecords As Integer = 15
...
...
End Class

And access them via Globals.NumberOfRecords


You can place them in the Application instance in your Global.asax's
Application_Start
Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
Application.Add("NumberOfRecords", 15)
...
End Sub

And access them via cint(Application("NumberOfRecords"))

There are more options, but those should do you fine.

Karl
 
S

Shiva

Hi,
An easy way is to have all such values as public members inside a class and
use it from anywhere inside the application.

Eg (C#):
class GlobalConsts
{
public const int MaxLength = 30;
Public const string EmailSubject = "Your Order";
}

You then simply refer the constant values as GlobalConsts.MaxLength,
GlobalConsts.EmailSubject, etc;

Hi There!

I'm trying to define constants so that I can refer those constants from any
page of my ASP.NET website. I know I can use <appSettings> in web.config XML
file but I don't want to parse and read the whole XML document everytime a
page is requested.

In classic ASP, I used Include files and define constants using Const
keyword so that every page that has been included the file can refer the
constants.

Is there any proper and easier way in ASP.NET to accomplish this? (and of
course without using web.config file)

Thanks all in advance!!
Don
 
C

clintonG

What about naming conventions that historically use upper
case for all constants, i.e. MAXLENGTH, EMAILSUBJECT?

Do you assume all code will always be used in Visual Studio.NET
where Intellisense will identify type when the mouse is hovered
over the variable?
 
S

Shiva

I agree with Karl. Using all caps is not the recommended way. Nor is the
hungarian notation.

What about naming conventions that historically use upper
case for all constants, i.e. MAXLENGTH, EMAILSUBJECT?

Do you assume all code will always be used in Visual Studio.NET
where Intellisense will identify type when the mouse is hovered
over the variable?
 
A

Ashish M Bhonkiya

Hi Don,

I wud create a seprate Project which will have all the constants,enum etc.
where i define the values for them.

Then just reference that newly created project where you need to access the
constants.

HTH
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top