?
Solved

Question about classes and enumerated types

Posted on 2006-06-30
6
Medium Priority
?
255 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 3

Accepted Solution

by:
boethius78 earned 2000 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

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

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Suggested Courses

762 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