Solved

Question about classes and enumerated types

Posted on 2006-06-30
6
237 Views
Last Modified: 2010-04-16
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.
0
Comment
Question by:dentyne
  • 3
  • 3
6 Comments
 
LVL 3

Accepted Solution

by:
boethius78 earned 500 total points
Comment Utility
Define an enumeration.  This should do the trick for months:

    public enum Months
    {
        January = 1,
        February,
        March,
        April,
        May,
        June
        // and so on
    }

If you need FYStartDate to be a short, then use FYStartDate = (short)Months.March.
0
 
LVL 1

Author Comment

by:dentyne
Comment Utility
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
0
 
LVL 3

Expert Comment

by:boethius78
Comment Utility
Given that you've defined the enumeration within MSProject2003, you'll have to refer to it as MSProject2003.FYStartMonths.March.
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 1

Author Comment

by:dentyne
Comment Utility
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.

0
 
LVL 1

Author Comment

by:dentyne
Comment Utility
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!
0
 
LVL 3

Expert Comment

by:boethius78
Comment Utility
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#.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now