Under what circumstances is an enum more appropriate than, for example, a Collection that guarantees unique elements (an implementer of java.util.Set, I guess...)?
(This is kind of a follow up from my previous question)
Thanks!
...
How do databases (mySQL if a particular example is important) determine the size of the column needed to store an ENUM?
Is it something simple like a single byte for less than 256 enum options, etc.?
...
I have the following code:
// Obtain the string names of all the elements within myEnum
String[] names = Enum.GetNames( typeof( myEnum ) );
// Obtain the values of all the elements within myEnum
Array values = Enum.GetValues( typeof( myEnum ) );
// Print the names and values to file
for ( int i = 0; i < names.Length; i++ )
{
pri...
Often, one needs several enumerated types together. Sometimes, one has a name clash. Two solutions to this come to mind: use a namespace, or use 'larger' enum element names. Still, the namespace solution has to possible implementations: a dummy class with nested enum, or a full blown namespace.
I'm looking for pro's and con's of all ...
When comparing enums that come from different sources such as those of the following code GCC emits warnings. Is there a way to avoid these warnings without c-style casts?
struct Enumerator
{
enum { VALUE = 5 };
};
template<int V>
struct TemplatedEnumerator
{
enum { VALUE = V };
};
if(Enumerator::VALUE == TemplatedEnumerator<5...
I have some questions on using std::map:
Is using an enum as the key in std::map a good practice? Consider the following code: enum Shape{
Circle,
Rectangle
};
int main(int argc, char* argv[])
{
std::map strMap;
// strMap.insert(Shape::Circle,"Circle"); // This will not compile
strMap[Shape::Circle] = "Circle"; ...
So I was looking at some code that was checked in and I got all puzzled over:
// Amount of days before cancellation can't be done
enum Cancellation { Limit = 2 };
Asking the guy who checked it in he argued that it's much better to use enums instead of static variables, bettern than this:
private static int CANCELLATION_LIMIT = 2;
S...
Most projects have some sort of data that are essentially static between releases and well-suited for use as an enum, like statuses, transaction types, error codes, etc. For example's sake, I'll just use a common status enum:
public enum Status {
ACTIVE(10, "Active");
EXPIRED(11, "Expired");
/* other statuses... */
/* c...
Can you pass a standard c# enum as a parameter?
For example:
enum e1
{
//...
}
enum e2
{
//...
}
public void test()
{
myFunc( e1 );
myFunc( e2 );
}
public void myFunc( Enum e )
{
// Iterate through all the values in e
}
By doing this I hope to retrieve all the names within any given enum. What would the Iterat...
Hi All,
I often find myself implementing a class maintaining some kind of own status property as an enum: I have a Status enum and ONE Status property of Status type. How should I solve this name conflict?
public class Car
{
public enum Status
{
Off,
Starting,
Moving
};
Status status = Status.Off;
public Status ...
I'd like to get a string representation of the underlying type of the enum.
Dim target As System.ConsoleColor = ConsoleColor.Cyan
Dim actual = 'What goes here?
Dim expected = "11"
...
Whichever way I do it, it seems something goes wrong when using the conditional operator on enum values in Linq to Sql. For example
var ret = from listings in db.Listings
select new Listing
{
ID = listings.ID,
//etc
OrderStatus = listings.OrderItems.Count > 0
? listings.OrderItems.First().Order.OrderStatus : OrderStatu...
If I have the following code:
enum foo : int
{
option1 = 1,
option2,
...
}
private foo convertIntToFoo(int value)
{
// Convert int to respective Foo value or throw exception
}
What would the conversion code look like?
...
Is this possible? I can't find it anywhere. Thanks!
...
I want to serialize my enum-value as an int, but i only get the name.
Here is my (sample) class and enum:
public class Request {
public RequestType request;
}
public enum RequestType
{
Booking = 1,
Confirmation = 2,
PreBooking = 4,
PreBookingConfirmation = 5,
BookingStatus = 6
}
And the code (just to be sure i'm not doing it w...
I got an Int16 value, from the database, and need to convert this to an enum type. This is unfortunately done in a layer of the code that knows very little about the objects except for what it can gather through reflection.
As such, it ends up calling Convert.ChangeType which fails with an invalid cast exception.
I found what I conside...
As a follow on from this question.
How can I call a function and pass in an Enum?
For example I have the following code:
enum e1
{
//...
}
public void test()
{
myFunc( e1 );
}
public void myFunc( Enum e )
{
var names = Enum.GetNames(e.GetType());
foreach (var name in names)
{
// do something!
}
}
...
Strings to enum in C#, how do you normally converting strings to enum in C++. Any helper function that you use, is it a good idea to do this.
...
I'm trying to make a function that can take an enum type, display all the possible selections to the user, let them select one and then pass it back. Generics don't allow you to limit to enum. I've got code working that will cast back and forth, but I'd like it to accept and return the same enum type.
This code works but not as well as ...
I need to create many classes that are somewhere between integer and enum.
I.e. have the arithmetics of integer but also are not implicitly converted to int.
...