Solved

Question about classes and enumerated types

Posted on 2006-06-30
6
242 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
ID: 17017428
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
ID: 17017495
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
ID: 17017529
Given that you've defined the enumeration within MSProject2003, you'll have to refer to it as MSProject2003.FYStartMonths.March.
0
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
LVL 1

Author Comment

by:dentyne
ID: 17017568
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
ID: 17017588
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
ID: 17017634
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Export import database 4 51
Filename to be appended with DateTimeof Download 3 54
What .NET URL re-routing tool did I use? 2 54
Turn on intranet settings 1 55
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

816 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

8 Experts available now in Live!

Get 1:1 Help Now