Solved

accessing an enum from a separate class..

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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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…
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!
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
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…

627 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