C# 未能将值分配给枚举
你曾经有过什么都不管用的日子吗?现在我甚至无法成功设置枚举的值 我在底部的C# 未能将值分配给枚举,c#,enums,C#,Enums,你曾经有过什么都不管用的日子吗?现在我甚至无法成功设置枚举的值 我在底部的Enum.Parse语句中遇到了问题,所以我在上面写了'if'块。令我惊讶的是,这也失败了 我用调试器跟踪了这一点。字符串x的值为“OnDemand”。采用第一个“if”的“true”分支,但bitmapCacheOption保持bitmapCacheOption.Default 下面的Enum.Parse表达式也是如此。因此,我的问题是:在为枚举赋值时,我做错了什么? BitmapCacheOption bitmapCa
Enum.Parse
语句中遇到了问题,所以我在上面写了'if'块。令我惊讶的是,这也失败了
我用调试器跟踪了这一点。字符串x
的值为“OnDemand”。采用第一个“if”的“true”分支,但bitmapCacheOption
保持bitmapCacheOption.Default
下面的Enum.Parse
表达式也是如此。因此,我的问题是:在为枚举赋值时,我做错了什么?
BitmapCacheOption bitmapCacheOption;
if (x == "OnDemand") bitmapCacheOption = BitmapCacheOption.OnDemand;
else
{
if (x == "OnLoad") bitmapCacheOption = BitmapCacheOption.OnLoad;
else
{
if (x == "None") bitmapCacheOption = BitmapCacheOption.None;
else bitmapCacheOption = BitmapCacheOption.Default;
}
}
BitmapCacheOption bitmapCacheOption1 =
(BitmapCacheOption)Enum.Parse(typeof(BitmapCacheOption), x, false);
Debug.Assert(bitmapCacheOption == bitmapCacheOption1);
编辑:问题中的枚举是WPF枚举。在Enum.Parse语句末尾的false只是为了忽略大小写。我知道编写级联“if”语句(包括“else-if”和“switch”语句)的更好方法,但所有这些都与问题无关。在调试期间,我以这种方式编写了“if”,以便逐步完成调试器。重要的是,即使使用简单的if,当x等于“OnDemand”时,bitmapCacheOption仍保持bitmapCacheOption.Default
编辑:请注意调试器的“局部变量”窗口中“bitmapCacheOption”的值-即使黄色高亮显示“OnDemand”开关大小写未采用,它仍保持为“Default”状态
尝试删除假运算符-
BitmapCacheOption bitmapCacheOption1 =
(BitmapCacheOption)Enum.Parse(typeof(BitmapCacheOption), x);
在路上-上传枚举结构,让我们看看是否有问题
编辑:哇!
看起来他们有一个bug-两个索引都是0。
这就是为什么每次都会得到默认值。因为它是0。很好。。。但是,当您将值设置为x时,它会将0指定给他,而不是应该存在的YYY值….尝试删除假运算符-
BitmapCacheOption bitmapCacheOption1 =
(BitmapCacheOption)Enum.Parse(typeof(BitmapCacheOption), x);
在路上-上传枚举结构,让我们看看是否有问题
编辑:哇!
看起来他们有一个bug-两个索引都是0。
这就是为什么每次都会得到默认值。因为它是0。很好。。。但是当你把值设为x时,它会给他赋值0,而不是YYY值,而YYY值应该在那里….你在这里没有做错什么 查看枚举的定义:
// Summary:
// Specifies how a bitmap image takes advantage of memory caching.
public enum BitmapCacheOption
{
// Summary:
// Creates a memory store for requested data only. The first request loads the
// image directly; subsequent requests are filled from the cache.
OnDemand = 0,
//
// Summary:
// Caches the entire image into memory. This is the default value.
Default = 0,
//
// Summary:
// Caches the entire image into memory at load time. All requests for image
// data are filled from the memory store.
OnLoad = 1,
//
// Summary:
// Do not create a memory store. All requests for the image are filled directly
// by the image file.
None = 2,
}
OnDemand
和Default
都将0作为值;)
因此,您不能依赖0的字符串表示形式,将其转换为
BitmapCacheOption
的值,您在这里没有做错任何事情
查看枚举的定义:
// Summary:
// Specifies how a bitmap image takes advantage of memory caching.
public enum BitmapCacheOption
{
// Summary:
// Creates a memory store for requested data only. The first request loads the
// image directly; subsequent requests are filled from the cache.
OnDemand = 0,
//
// Summary:
// Caches the entire image into memory. This is the default value.
Default = 0,
//
// Summary:
// Caches the entire image into memory at load time. All requests for image
// data are filled from the memory store.
OnLoad = 1,
//
// Summary:
// Do not create a memory store. All requests for the image are filled directly
// by the image file.
None = 2,
}
OnDemand
和Default
都将0作为值;)
因此,您不能依赖0的字符串表示形式,将其转换为
BitmapCacheOption
的值。我知道这与您的问题无关,但您是否听说过switch()
?也许您需要尝试干净的构建?此外,还有一个else if
可用。Enum.Parse有什么问题?您所说的“bitmapCacheOption保持bitmapCacheOption.Default”到底是什么意思?你是如何观察这一点的?如果只是在调试时,它可能是由于线程问题。@ShadowWizard使用调试器的局部变量windwo我知道这与您的问题无关,但您是否听说过switch()
?也许您需要尝试一个干净的版本?此外,还有一个else if
可用。Enum.Parse有什么问题?您所说的“bitmapCacheOption保持bitmapCacheOption.Default”到底是什么意思?你是如何观察这一点的?如果只是在调试时,它可能是由于线程。@ShadowWizard使用调试器的局部变量windwo,我认为它确实可以解释它是如何发生的。我们都是人类!;)不可信。谢谢我认为这确实可以解释它是如何发生的。我们都是人类!;)不可信。谢谢