Solved

accessing an enum from a separate class..

Posted on 2003-11-13
2
382 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

Independent Software Vendors: 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!

Question has a verified solution.

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

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
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 antispam), the admini…

713 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