Wrox Programmer Forums
|
ASP.NET 1.0 and 1.1 Professional For advanced ASP.NET 1.x coders. Beginning-level questions will be redirected to other forums. NOT for "classic" ASP 3 or the newer ASP.NET 2.0 and 3.5
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.0 and 1.1 Professional section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 7th, 2004, 09:51 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 596
Thanks: 1
Thanked 3 Times in 3 Posts
Default Site Wide Enum - Best Practice

I have an enum which I use to set the mode of dipslay for a user control I have created.
In fact I have several different user controls that could each use the same enum values.

I was wanting to know the correct method to define the enum. At present it is defined in the control, which I realize is inefficient.

Should I create a class to hold project wide enums or a seperate class for any enum I use site wide.

Having said that I may use this set of options in other sites.

Can anyone point to a good article or explain the best practices for where to define such things.

======================================
They say, best men are moulded out of faults,
And, for the most, become much more the better
For being a little bad.
======================================
__________________
======================================
"They say, best men are molded out of faults,
And, for the most, become much more the better
For being a little bad."
--Shakespeare
======================================
 
Old June 7th, 2004, 10:05 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

What I have done is create a class for constants. It will contain single constants and enums:

Class Constants
    Const SOME_CONST = 3
    Public Enum MyEnum
        One = 1
        Two = 2
        Three = 3
    End Enum
End Class

If you need to use this class in multipl projects then you'll need to put it into a separate project and reference it from the consuming project(s).
 
Old June 7th, 2004, 10:48 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 596
Thanks: 1
Thanked 3 Times in 3 Posts
Default

Thanks Peter.
That looks good, I think I'll leave them at the project level as it seems a little unattached to have a control rely on a enum that needs to be referenced seperately.

Thanks again

======================================
They say, best men are moulded out of faults,
And, for the most, become much more the better
For being a little bad.
======================================
 
Old June 8th, 2004, 01:36 AM
Authorized User
 
Join Date: Jun 2003
Posts: 42
Thanks: 0
Thanked 0 Times in 0 Posts
Default

In a project I'm working on now, I made a baseclass with an enum in it that is used by nearly every other page in the project. All pages in this project inherit from this baseclass.
I do not know if this is considered to be "best practice" (opinions please), but it works.

Gerhard Wentink
 
Old June 8th, 2004, 02:21 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

I usually do not even create a separate class for that. I just add a new .cs or .vb file to my project (either the Web project or a Class Library) and then add the enums directly to that file. If you make sure they are in the same namespace as the rest of your code, you can use them in your code directly, without prefixing them with Constants.

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
 
Old June 8th, 2004, 03:19 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 596
Thanks: 1
Thanked 3 Times in 3 Posts
Default

Thanks everyone for your input.
Very much appreciated.

======================================
They say, best men are moulded out of faults,
And, for the most, become much more the better
For being a little bad.
======================================
 
Old June 8th, 2004, 05:42 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Imar-

Can you post an example? I didn't think it was possible to put anything other than classes under a namespace. I'm sure I have tried it and didn't succeed.

Gerhard-
The only problem I see with your technique is that you might need to use those constants/enums in a different class that isn't inherited from the one in which you have the constants defined. Of course, if you expose the constants publicly there's no reason you can't access those constants. It just seems to make sense to me to keep them all together in a place that's generic. A kind of "code normalization" if you will.
 
Old June 8th, 2004, 06:04 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Sure, here you go.

In my Business Layer (a Class Library), I have a file called Enums.cs. It contains the following code:
Code:
namespace Spaanjaars.Imar.BLL
{
    /// <summary>
    /// Represents the status a member can have on the Web site.
    /// </summary>
    public enum MemberStatus: int
    {
        /// <summary>
        /// Member is active and has access to the Web site.
        /// Specific authorization depends on Role membership.
        /// </summary>
        Active = 1,
        /// <summary>
        /// Member account has been marked as deleted and 
        /// the member no longer has access to the Web site.
        /// </summary>
        Deleted = 2,
        /// <summary>
        /// Member recently subscribed and needs to confirm 
        /// the account to get access.
        /// </summary>
        Pending = 3
    }
}
In VB.NET you could do the same:
Code:
Namespace Spaanjaars.Imar.Bll
  Public Enum MemberStatus
    Active = 1
    Deleted = 2
    Pending = 3
  End Enum
End Namespace
As long as the namespace for the Enums matches those of the app (alternatively, import the namespace that holds the Enums), you can use them like this:

MemberStatus theStatus = MemberStatus.Active;

or

Dim theStatus As MemberStatus = MemberStatus.Active

HtH,

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
 
Old June 8th, 2004, 08:03 AM
Authorized User
 
Join Date: Jun 2003
Posts: 42
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Aah, it works as Imar said,
Thanks

Regards,

Gerhard Wentink





Similar Threads
Thread Thread Starter Forum Replies Last Post
enum 2 names OneQuestion General .NET 4 January 2nd, 2008 02:50 PM
MCMS 2002 site wide search problem jey_cms General .NET 0 March 13th, 2007 08:32 AM
MCMS 2002 site wide search problem jey_cms General .NET 0 March 13th, 2007 08:23 AM
Any Idea About Site Wide Search Engine yoord BOOK: Beginning ASP.NET 1.0 8 October 11th, 2004 08:31 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.