Enum / Cast Question
-
The docs for Enum says (paraphrasing) "Enumerations have an underlying type used for storage, which by default is int" So I have:
using System;
namespace Employee_Example1
{
public enum EmployeeType
{
FullTime = 0,
PartTime = 1,
Contract = 2,
Other = 3
}public class Employee\_Base { public int iEmpId = 0; public int iEmployeeType = EmployeeType.FullTime; public double dSalary = 0.00; public string sLastName = ""; public string sFirstName = ""; public Employee\_Base() { } }
}
So why am I getting the error "Cannot implicitly convert type 'Employee_Example1.EmployeeType' to 'int'" I have also tried "public enum EmployeeType : int", and it still won't compile. However, this works:
public int iEmployeeType = (int)EmployeeType.FullTime;
The question is, if the enum's underlying type is int, why do I need to cast?
Everything makes sense in someone's mind
-
The docs for Enum says (paraphrasing) "Enumerations have an underlying type used for storage, which by default is int" So I have:
using System;
namespace Employee_Example1
{
public enum EmployeeType
{
FullTime = 0,
PartTime = 1,
Contract = 2,
Other = 3
}public class Employee\_Base { public int iEmpId = 0; public int iEmployeeType = EmployeeType.FullTime; public double dSalary = 0.00; public string sLastName = ""; public string sFirstName = ""; public Employee\_Base() { } }
}
So why am I getting the error "Cannot implicitly convert type 'Employee_Example1.EmployeeType' to 'int'" I have also tried "public enum EmployeeType : int", and it still won't compile. However, this works:
public int iEmployeeType = (int)EmployeeType.FullTime;
The question is, if the enum's underlying type is int, why do I need to cast?
Everything makes sense in someone's mind
-
The docs for Enum says (paraphrasing) "Enumerations have an underlying type used for storage, which by default is int" So I have:
using System;
namespace Employee_Example1
{
public enum EmployeeType
{
FullTime = 0,
PartTime = 1,
Contract = 2,
Other = 3
}public class Employee\_Base { public int iEmpId = 0; public int iEmployeeType = EmployeeType.FullTime; public double dSalary = 0.00; public string sLastName = ""; public string sFirstName = ""; public Employee\_Base() { } }
}
So why am I getting the error "Cannot implicitly convert type 'Employee_Example1.EmployeeType' to 'int'" I have also tried "public enum EmployeeType : int", and it still won't compile. However, this works:
public int iEmployeeType = (int)EmployeeType.FullTime;
The question is, if the enum's underlying type is int, why do I need to cast?
Everything makes sense in someone's mind
-
The docs for Enum says (paraphrasing) "Enumerations have an underlying type used for storage, which by default is int" So I have:
using System;
namespace Employee_Example1
{
public enum EmployeeType
{
FullTime = 0,
PartTime = 1,
Contract = 2,
Other = 3
}public class Employee\_Base { public int iEmpId = 0; public int iEmployeeType = EmployeeType.FullTime; public double dSalary = 0.00; public string sLastName = ""; public string sFirstName = ""; public Employee\_Base() { } }
}
So why am I getting the error "Cannot implicitly convert type 'Employee_Example1.EmployeeType' to 'int'" I have also tried "public enum EmployeeType : int", and it still won't compile. However, this works:
public int iEmployeeType = (int)EmployeeType.FullTime;
The question is, if the enum's underlying type is int, why do I need to cast?
Everything makes sense in someone's mind
As Greeeg said above, there is no implicit conversion for a enum into an int. All conversions need to be done explicitly. Just a question/though about your base class: why are you storing the enum as an int? I think for program clarity and maintainability, storing EmployeeType as the enum type would be the best way to go (and it will be more clear if your going to have an object out of the base).
-
The docs for Enum says (paraphrasing) "Enumerations have an underlying type used for storage, which by default is int" So I have:
using System;
namespace Employee_Example1
{
public enum EmployeeType
{
FullTime = 0,
PartTime = 1,
Contract = 2,
Other = 3
}public class Employee\_Base { public int iEmpId = 0; public int iEmployeeType = EmployeeType.FullTime; public double dSalary = 0.00; public string sLastName = ""; public string sFirstName = ""; public Employee\_Base() { } }
}
So why am I getting the error "Cannot implicitly convert type 'Employee_Example1.EmployeeType' to 'int'" I have also tried "public enum EmployeeType : int", and it still won't compile. However, this works:
public int iEmployeeType = (int)EmployeeType.FullTime;
The question is, if the enum's underlying type is int, why do I need to cast?
Everything makes sense in someone's mind
KMAROIS wrote:
which by default is int
It can be other types including strings so explicit casting is the only logical way - implicit would create a whole bunch of problems!
Dave
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Expect everything to be hard and then enjoy the things that come easy. (code-frog) -
KMAROIS wrote:
which by default is int
It can be other types including strings so explicit casting is the only logical way - implicit would create a whole bunch of problems!
Dave
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Expect everything to be hard and then enjoy the things that come easy. (code-frog)Do you know how to create enums based on strings? That would be very useful to me.
“Time and space can be a bitch.” –Gushie, Quantum Leap {o,o}.oO( Looking for a great RSS reader? Try FeedBeast! ) |)””’) Built with home-grown CodeProject components! -”-”-
-
Do you know how to create enums based on strings? That would be very useful to me.
“Time and space can be a bitch.” –Gushie, Quantum Leap {o,o}.oO( Looking for a great RSS reader? Try FeedBeast! ) |)””’) Built with home-grown CodeProject components! -”-”-
Do you mean putting strings rather than int ? AFAIK, you can use attributes for the enum and specify the string value there, use reflection to read the attribute and it's value. I have seen some good articles here written by PiebelConsult and Scott dorman.
All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia How to use google | Ask smart questions
-
Do you know how to create enums based on strings? That would be very useful to me.
“Time and space can be a bitch.” –Gushie, Quantum Leap {o,o}.oO( Looking for a great RSS reader? Try FeedBeast! ) |)””’) Built with home-grown CodeProject components! -”-”-
Navaneeth is correct in his response - I was wrong, you can't use strings directly :-O . I knew I'd used them before but forgot that I'd used attributes to do it. There's many examples around of different methods of achieving a string enum. An interesting one is here[^].
Dave
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Expect everything to be hard and then enjoy the things that come easy. (code-frog) -
Navaneeth is correct in his response - I was wrong, you can't use strings directly :-O . I knew I'd used them before but forgot that I'd used attributes to do it. There's many examples around of different methods of achieving a string enum. An interesting one is here[^].
Dave
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Expect everything to be hard and then enjoy the things that come easy. (code-frog)Lol no worries. The attribute trick might be worth it, although with reflection you can get the name of an enum constant (of course these are subject to C# naming rules). That library looks interesting. Thanks to both of you for the responses.
“Time and space can be a bitch.” –Gushie, Quantum Leap {o,o}.oO( Looking for a great RSS reader? Try FeedBeast! ) |)””’) Built with home-grown CodeProject components! -”-”-