Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

accessing an enum from a separate class..

Posted on 2003-11-13
2
Medium Priority
?
388 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
1 Comment
 
LVL 20

Accepted Solution

by:
TheAvenger earned 500 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Suggested Courses

824 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