Solved

Question about classes and enumerated types

Posted on 2006-06-30
6
239 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Image(7) 1 53
Does this SQL code look like it will work using VS2010 with .NetFramework 4.0 ? 2 41
Runtime Error 2 27
VB.Net How to Exit Sub - Exit Form??? 5 48
Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

932 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

12 Experts available now in Live!

Get 1:1 Help Now