Solved

accessing an enum from a separate class..

Posted on 2003-11-13
2
379 Views
Last Modified: 2011-09-20
OK..  This should be pretty straight forward, but I am having problems.

I have a class something like this

     public class BLLLayer
     {
      public enum Temperature: int
            {
                  cold=1,
                  warm=2,
                  hot=3
            }
//more class stuff...

I want to be able to access the Temperature enum from another class as follows:

public class mytestclass
{
      BLLLayer bll = new BLLLayer();
      int i = bll.Temperature.cold;
      Console.WriteLine ("value is: " + i);
}

For whatever reason, I can't access my enum from the BLLLayer class....  Do I need accessor methods ??  What would they look like ?

Thanks
0
Comment
Question by:mdaymond
2 Comments
 
LVL 20

Accepted Solution

by:
TheAvenger earned 125 total points
ID: 9740850
No, you just have to use the class name, not the class variable. So the right code is:

public class mytestclass
{
      BLLLayer bll = new BLLLayer();
      int i = (int)BLLLayer.Temperature.cold;
      Console.WriteLine ("value is: " + i);
}

Also here notice the (int) convertion in front of the value. You cannot directly assign enum values to ints. You can however define i as a variable of the enum:

public class mytestclass
{
      BLLLayer bll = new BLLLayer();
      BLLLayer.Temperature i = BLLLayer.Temperature.cold;
      Console.WriteLine ("I is: " + i);  // This will print "cold"
      Console.WriteLine ("I is: " + (int)i);  // This will print 1
}
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Bit flags and bit flag manipulation is perhaps one of the most underrated strategies in programming, likely because most programmers developing in high-level languages rely too much on the high-level features, and forget about the low-level ones. Th…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now