枚举:: getValues()返回零元素
我对Method Enum :: getValues()有问题。它没有返回 - 没有错误,没有元素。 “ a”的长度为零。该代码来自Microsoft网站,因此这是官方示例。我无法使它起作用。项目适用于.NET框架4.7.2,这应该可以。我正在使用所有更新的VS2022。有人有任何建议吗?谢谢
using namespace System;
enum class Colors
{
Red, Green, Blue, Yellow
};
int main()
{
Console::WriteLine( "The values of the Colors Enum are:" );
Array^ a = Enum::GetValues( Colors::typeid );
for ( Int32 i = 0; i < a->Length; i++ )
{
Object^ o = a->GetValue( i );
Console::WriteLine( "{0}", Enum::Format( Colors::typeid, o, "D" ) );
}
}
示例应该产生:
// The values of the Colors Enum are:
// 0
// 1
// 2
// 3
但是结果仅是:
The values of the Colors Enum are:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
enum class
是一个c ++ 11范围的枚举,而不是.net枚举,请参阅 https://learn.microsoft.com/en-us/cpp/cpp/cpp/dotnet/how-to-to-define-和cppp-cli-inums-enums-enums要使它成为.net枚举,您需要添加
private
或public
。例如:enum class
is a c++11 scoped enum, not a .net enum, see the note at https://learn.microsoft.com/en-us/cpp/dotnet/how-to-define-and-consume-enums-in-cpp-cliTo make it a .net enum you need to add
private
orpublic
. E.g.: