Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / August 2006

Tip: Looking for answers? Try searching our database.

file of constants? (vs2005/c#)

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
MarkusR - 22 Aug 2006 19:12 GMT
Good day, bare with me while I probably ask a very novice question...

I have multiple projects that use the same constants. I want to create
a common file they can share.

Is this the best way to do it:

using System;

namespace myconsts
{
   class orderconsts
   {
       public const int ORDER_STATUS_HOLD = 1;
       public const int ORDER_STATUS_ERROR = 2;
       public const int ORDER_STATUS_READYTOSHIP = 3;
       public const int ORDER_STATUS_PACKAGED = 4;
       public const int ORDER_STATUS_SHIPPED = 5;
       public const int ORDER_STATUS_VOIDED = 6;
   }
}

and access it using orderconsts.ORDER_STATUS_HOLD

-Markus_R
Andy - 22 Aug 2006 19:25 GMT
First, I'd think this would be an enum.  Second, you'd probably want to
embed this in your Order class, which knows about its own status.
Other projects which need to know order status would likely ask this
order class.

HTH
Andy

> Good day, bare with me while I probably ask a very novice question...
>
[quoted text clipped - 21 lines]
>
> -Markus_R
MarkusR - 22 Aug 2006 21:33 GMT
Hey Andy,

Actually you are probably right. I  forget to use enums. (consts always
start out with 1 or 2 and grow).

This project that I am doing is extremely small and I opted to not use
OO for everything. About 80 lines of code. But, again, another good
point. I will have to think if I could organize all into a reusable
class.

-Markus_R

> First, I'd think this would be an enum.  Second, you'd probably want to
> embed this in your Order class, which knows about its own status.
[quoted text clipped - 29 lines]
> >
> > -Markus_R
sloan - 22 Aug 2006 22:58 GMT
I do mine like this:

     public enum ExampleEnum
     {
      [Description("MyUnknownDescription")]    Unknown    = 0
      [Description("Items of this enum are the coolest")]    SuperCool    =
1
     }

Ok.. then I have a EnumHelper.

using System;
using System.ComponentModel;
using System.Reflection;
using System.Xml;

namespace MyCompany.Enums
{
/// <summary>
/// Summary description for EnumHelper.
/// </summary>
public class EnumHelper
{
 private EnumHelper()
 {
  //only static methods
 }

 public static string GetDescription(System.Enum  value)
 {

  //if a description is defined for an enum value, this procedure will get
it

  //Example of defining a description with an enum value
  /*
   *

    public enum ExampleEnum
    {
     [Description("MyUnknownDescription")]    Unknown    = 0
    }

   *
   * */

  FieldInfo fi= value.GetType().GetField(value.ToString());
  DescriptionAttribute[] attributes =
(DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute),
false);
  return (attributes.Length>0)?attributes[0].Description:value.ToString();

 }

}

I kinda get the best of both worlds.
I use the enums as enums 99% of the time.

But I have get a long description if I ever need it.

> Good day, bare with me while I probably ask a very novice question...
>
[quoted text clipped - 21 lines]
>
> -Markus_R
MarkusR - 31 Aug 2006 21:13 GMT
Thanks much sloan. I will have to study this. ;)

-Markus

> I do mine like this:
>
[quoted text clipped - 82 lines]
> >
> > -Markus_R

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.