Link to home
Start Free TrialLog in
Avatar of dentyne
dentyne

asked on

Question about classes and enumerated types

Hello,
I am writing a program that converts some data to Microsoft Project 2003 XML format.  I am writing a class that will allow the ASPX page to fill in properties based on what the user selected on the web page.  Some of these properties, like "FYStartDate", which represents Fiscal Year Start month, has acceptable values from January through December.  But on the conversion, I need this to be a numeric value.  I am unsure how to do this.   Here is my current code in the business layer, but it just accepts a numeric value:

public class MSProject2003
{
   private short projectFYStartDate;

   public short FYStartDate
   {
      get
      {
           return projectFYStartDate;
       }
       set
       {
           projectFYStartDate = value;
       }
    }
}


Then from the .cs code behind page, this looks like:

MSProject2003 msp = new MSProject2003();
msp.FYStartDate = 3;  //This would represent setting it to March.

However, I would want it to look something like this, but i don't know how to code it:

msp.FYStartDate = FYStartMonths.March;

This is an example just showing month.  But I have a lot of other types of things that need to be converted into some type of number.  I just don't know how to code it.  But this seems really clean.  Kind of like the ConnectionObject.  When you check the state of the Connection it's like:

if(conn.ConnectionState == ConnectionState.Closed)

How do I code the "ConnectionState.Closed" part?

I'm hoping I'm making sense.  this is kind of tough to articulate in words.  Thanks for the help.
ASKER CERTIFIED SOLUTION
Avatar of boethius78
boethius78

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of dentyne
dentyne

ASKER

Thank you for the response.  This is what I tried, but it doesn't seem to be working:




public class MSProject2003
{
   private short projectFYStartDate;

   public enum FYStartMonths
   {
      January = 1,
      February,
      March
   }
   public short FYStartDate
   {
      get
      {
           return projectFYStartDate;
       }
       set
       {
           projectFYStartDate = value;
       }
    }
}

I compiled the code.  Then on the .cs code behind of my .aspx page, I have:

MSProject2003 msp = new MSProject2003();
msp.FYStartDate =  FYStartMonths.March;

But unfortunately, it doesn't seem to recognize FYStartMonths and there's no intellisense to choose March.
When I look at the available choices by typing "msp.", it doesn't show the FYStartMonths
Given that you've defined the enumeration within MSProject2003, you'll have to refer to it as MSProject2003.FYStartMonths.March.
Avatar of dentyne

ASKER

I see.  That seems to work. I am still a bit confused.  It is unclear why, if I define "msp" as a MSProject2003 that the enum wouldn't show up.  But then when I type MSProject2003.  it does show up.  

Would there be a way to do it without having the MSProject2003 prefix?  

Going back to the connection example, I can do

using Oracle.DataAccess.Client;

OracleConnection conn = new OracleConnection();
if(conn.ConnectionState == ConnectionState.Open)...

In that case it wasn't needed to type: if(conn.ConnectionState == OracleConnection.ConnectionState)
or Oracle.DataAccess.Client.ConnectionState

Does that make sense?

Thank you so much for your help and patience.

Avatar of dentyne

ASKER

Ahh figured it out.  It just had to be outside of the class. I see what you're saying.  I changed it to be like such:

public class BusinessLayer
{
   //my enum definition here
   public class MSProject2003



Awesome.  Thanks for the help!
Thanks for the points!

To clear things up, I'm guessing you have a VB background.  In VB, if you write msp., you can access either static or instance members.  In C#, you can't access static members from an instance of the object.  So, msp.FYStartMonths will work in VB, but won't work in C#.