枚举赋值解析c#
好吧,无论出于什么原因,我似乎无法解决这个小问题。
我有以下枚举:
public enum EFeedType
{
TypeOne = 1,
TypeTwo = 2
}
现在,我将从数据库中获取数值。好吧,我需要将 int 值从数据库转换为枚举类型:
编辑: 数据库类型是整数,所以我不需要从字符串进行转换。 结束编辑
EFeedType feedType = (EFeedType) feedId;
但是,当我执行此操作并传入值 2 时,我收到以下错误:
实例验证错误:“2”不是 [Namespace Goes Here].EFeedType 的有效值。
对我可能做错或错过的事情有什么想法吗?
编辑 这是我正在使用的代码:
//GetFeed will return an int value which is pulled from the database
int feedId = new FeedEngine().GetFeed("FeedName");
//Convert the ID to the Enum
EFeedType feedType = (EFeedType) feedId;
//Set the User Control FeedType Enum to the enum
FeedControl.FeedType = feedType;
//Show the user control
FeedControl.Visible = true;
编辑 - 更多信息
好的,在看到 JSkeet 的响应后,我看到如果我的 feedId = 1 那么它将把枚举值设置为 TypeTwo 而不是 TypeOne 就像它应该的那样。也许我的枚举中需要一个 Default = 0 值才能正常工作?但必须有更好的方法,因为如果我的价值观不按顺序怎么办?
Okay, for whatever reason I can't seem to figure this little problem out.
I have the following enum:
public enum EFeedType
{
TypeOne = 1,
TypeTwo = 2
}
Now, I am going to be getting the numeric value from a database. Well, I need to cast the int value from the DB to the enum type:
EDIT:
The database type is integer, so I do not need to cast from string.
END EDIT
EFeedType feedType = (EFeedType) feedId;
However, when I do this, and pass in value of 2 I get the following error:
Instance validation error: '2' is not a valid value for [Namespace Goes Here].EFeedType.
Any thoughts on what I might be doing wrong or missing?
EDIT
Here is the code I am using:
//GetFeed will return an int value which is pulled from the database
int feedId = new FeedEngine().GetFeed("FeedName");
//Convert the ID to the Enum
EFeedType feedType = (EFeedType) feedId;
//Set the User Control FeedType Enum to the enum
FeedControl.FeedType = feedType;
//Show the user control
FeedControl.Visible = true;
EDIT - More Info
Okay, after seeing JSkeet's response, I see that If my feedId = 1 then it will set the enum value to TypeTwo instead of TypeOne like it should. Maybe I need a Default = 0 value in my enum for this to work? But there has to be a better way, because what if my values are not in sequence.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的错误不会来自您所显示的行。我强烈怀疑它来自这一行:
我的猜测是该属性正在进行一些验证 - 并且它不知道相关值。
编辑:请注意,如果您确实想知道某个值是否有效,请使用
Enum.IsDefined
。只要数值在正确的范围内,Enum.Parse
就不会因不正确的数值而引发异常。您真的发布了枚举的确切代码吗?因为如果它没有明确指定“= 1”和“= 2”,它将从0自动递增,这将是你的问题。
如果您可以用一个简短但完整的程序来演示所有这些,那将非常有帮助。无需访问数据库,也无需对用户控件执行任何操作。
Your error won't be coming from the line you showed it on. I strongly suspect it's coming from this line:
My guess is that that property is doing some validation - and that it doesn't know about the relevant value.
EDIT: Note that if you do want to find out if a value is valid, use
Enum.IsDefined
.Enum.Parse
will not throw an exception for an incorrect numeric value, so long as it's in the right range.Have you really posted the exact code of the enum? Because if it doesn't explicitly specify the "= 1" and "= 2" it will autoincrement from 0, and that will be your problem.
If you could demonstrate all of this with a short but complete program it would really help. There's no need to go to the database, and no need to do anything with a user control.
我刚刚尝试了以下方法,效果非常好。您可能想将代码分解为一个小测试项目,看看发生了什么。
I just tried the following and it worked perfectly. You might want to break your code out into a little test project to see what's going on.