枚举:: getValues()返回零元素

发布于 2025-01-24 18:29:14 字数 989 浏览 3 评论 0 原文

我对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:

I've got problem with method Enum::GetValues(). It returns nothing - no error, no elements. Length of 'a' is zero. This code is from Microsoft website, so this is official example. I can't make it work. Project is for .NET framework 4.7.2, this should be ok. I'm using VS2022 with all updates. Does anybody has any suggestion? Thanks

Link: https://learn.microsoft.com/en-us/dotnet/api/system.enum.getvalues?view=net-6.0

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 example should produce:

//       The values of the Colors Enum are:
//       0
//       1
//       2
//       3

But the result is only:

The values of the Colors Enum are:

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

奶气 2025-01-31 18:29:14

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 。例如:

private enum class Colors
{
   Red, Green, Blue, Yellow
};

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-cli

To make it a .net enum you need to add private or public. E.g.:

private enum class Colors
{
   Red, Green, Blue, Yellow
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文