Click here to Skip to main content
15,896,278 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Hi All,

Betraying my ignorance of some of the finer points OO programming. I come from the Hardware World (Assembler, C!) and have missed the need for certain things in the purely PC world. I have always used #define for definitions I have seen the const in some C++ and could quite see the difference can any one either explain or point me to some reference. eNums just seem to be a way of grouping constants together?....
Posted
Comments
glennPattonWork3 28-May-12 11:03am    
Dirty casting, have you seen some my code then! So if I have it correct you can have an eNum member say Shorty set to equal 32,766 and then it would get up set if you add 2 to it, where if you use a define and you do that with a #define it is down to the compiler to ensure it does cause something else to get upset?
[no name] 28-May-12 11:05am    
ok. deleted...
glennPattonWork3 28-May-12 11:09am    
Sorry, that wasn't meant as being picky or anything no offense intended! :)

1 solution

A constant will refer to a value that never changes, that you may need to reference often in a program. For example:

<lang pre="cs">
const long SpeedOfLight= 299792458;


Enums are pretty much constants, just neater. It also acts a list of sorts. Say you have a method that returns the cure to a disease, it can look like this:

public enum Disease { TB, AIDS, Cancer, Flu }

public string Cure(Disease d)
{
      switch(d)
      {
          case Disease.TB:
               return "Blah";
               break;
        ......
       }
}        


Thats pretty much the best I can do. I would recommend a Google search to get a more in depth answer.

-Dom
 
Share this answer
 
v2
Comments
glennPattonWork3 28-May-12 11:10am    
Ahh well off to google I go. Mind you I haven't in my travels seen that done with a switch(!)
glennPattonWork3 28-May-12 11:26am    
Ahh, well seems to a Holy War between const and #define lets see how it played out, as I have seen both used in some programs.....

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900