473,473 Members | 2,109 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

HOWTO: Define constants for the whole site?

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
Nov 18 '05 #1
7 2547
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
[b] 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

"Don Wash" <do*@wash.com> wrote in message
news:uX**************@TK2MSFTNGP11.phx.gbl...
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

Nov 18 '05 #2
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;

"Don Wash" <do*@wash.com> wrote in message
news:uX**************@TK2MSFTNGP11.phx.gbl...
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

Nov 18 '05 #3
If you're using VB as code-behind, just put them in a public code module.

"Don Wash" <do*@wash.com> wrote in message
news:uX**************@TK2MSFTNGP11.phx.gbl...
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

Nov 18 '05 #4
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?

--
<%= Clinton Gallagher, "Twice the Results -- Half the Cost"
Architectural & e-Business Consulting -- Software Development
NET cs*********@REMOVETHISTEXTmetromilwaukee.com
URL http://www.metromilwaukee.com/clintongallagher/

"Shiva" <sh******@online.excite.com> wrote in message
news:%2***************@tk2msftngp13.phx.gbl...
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;

"Don Wash" <do*@wash.com> wrote in message
news:uX**************@TK2MSFTNGP11.phx.gbl...
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

Nov 18 '05 #5
I don't get your point.

Anyways, for naming convetions far better to follow something well
documented
(http://msdn.microsoft.com/library/de...-us/cpgenref/h
tml/cpconnetframeworkdesignguidelines.asp) and that has an policing tool
(http://www.gotdotnet.com/team/fxcop/)

which would be against user upper case for constants, and even more against
hungarian notation (which your 2nd paragraph suggests).

Karl

"clintonG" <cs*********@REMOVETHISTEXTmetromilwaukee.com> wrote in message
news:uZ*************@TK2MSFTNGP11.phx.gbl...
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?

--
<%= Clinton Gallagher, "Twice the Results -- Half the Cost"
Architectural & e-Business Consulting -- Software Development
NET cs*********@REMOVETHISTEXTmetromilwaukee.com
URL http://www.metromilwaukee.com/clintongallagher/

"Shiva" <sh******@online.excite.com> wrote in message
news:%2***************@tk2msftngp13.phx.gbl...
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;

"Don Wash" <do*@wash.com> wrote in message
news:uX**************@TK2MSFTNGP11.phx.gbl...
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


Nov 18 '05 #6
I agree with Karl. Using all caps is not the recommended way. Nor is the
hungarian notation.

"clintonG" <cs*********@REMOVETHISTEXTmetromilwaukee.com> wrote in message
news:uZ*************@TK2MSFTNGP11.phx.gbl...
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?

--
<%= Clinton Gallagher, "Twice the Results -- Half the Cost"
Architectural & e-Business Consulting -- Software Development
NET cs*********@REMOVETHISTEXTmetromilwaukee.com
URL http://www.metromilwaukee.com/clintongallagher/

"Shiva" <sh******@online.excite.com> wrote in message
news:%2***************@tk2msftngp13.phx.gbl...
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;

"Don Wash" <do*@wash.com> wrote in message
news:uX**************@TK2MSFTNGP11.phx.gbl...
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


Nov 18 '05 #7
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
--
Regards
Ashish M Bhonkiya

"Don Wash" <do*@wash.com> wrote in message
news:uX**************@TK2MSFTNGP11.phx.gbl...
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

Nov 18 '05 #8

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

Similar topics

15
by: Bob | last post by:
I've tried everything; and I can't seem to get past this VERY (seemingly) simply problem. I want to work with an array variable within a function(s). I can't get it to work; if I: 1) global...
4
by: Logan | last post by:
Several people asked me for the following HOWTO, so I decided to post it here (though it is still very 'alpha' and might contain many (?) mistakes; didn't test what I wrote, but wrote it - more or...
2
by: Alexander Eisenhuth | last post by:
Hello alltogether, I hope somebody can help me in that case. I bet I have overseen s.th.. I have a VC++ IDispatch Com-Server (ATL) and include for error handling issues a enumeration in the...
1
by: Roy | last post by:
Hi, I have a problem that I have been working with for a while. I need to be able from server side (asp.net) to detect that the file i'm streaming down to the client is saved...
11
by: Richard Meister | last post by:
Hi, I'd like to define several constants and make sure that all of them are smaller than a given other constant. I thought this could be done by a simple macro. Something like this: #define...
4
by: shonend | last post by:
I am trying to extract the pattern like this : "SUB: some text LOT: one-word" Described, "SUB" and "LOT" are key words; I want those words, everything in between and one word following the...
10
by: Paul | last post by:
Hi all, All of the classes in my DAL are static, with constants defining the stored procedures and parameters. I've been having some problems with my site which makes me wonder if there's a...
27
by: pereges | last post by:
I need to define the plancks constant 6.67 X 10 exp -34. Is it possible to do it using #define or would you suggest using a (static ?)const double.
11
by: Bill Davy | last post by:
I am trying to edit Contacts in Outlook. This is so I can transfer numbers from my address book which is an Excel spreadsheet to my mobile phone. I came across the following snippet of code which...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.