相当于 C++/CLI 中枚举类型的 ToString()

发布于 2024-08-03 19:59:47 字数 98 浏览 5 评论 0原文

在 C# 中,您可以声明一个枚举,设置其值后,可以调用该变量的 ToString 并获取该枚举值的字符串表示形式。

如何使用托管枚举在 C++/CLI 中执行此操作?

In C# you can declare an enum and once you have set its value call ToString on the variable and get a string representation of the value of the enum.

How do you do this in C++/CLI using a managed enum?

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

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

发布评论

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

评论(4

書生途 2024-08-10 19:59:47

虽然其他答案并没有错,但我发现自己也遇到了同样的问题。就我而言,我声明了一个标准 C++ 枚举,但忘记使用 CLI 语法(即使我已将其公开在公共属性中而没有编译器警告!)。

C++/CLI 枚举的正确语法是(注意“class”一词):

public enum class SomeEnum {
    Value1,
    Value2
}

注意:您还可以使用“struct”而不是“class”。

您还需要返回代码并更改任何分配(但是编译器会很高兴地向您显示错误),从: 更改为

SomeEnum value = Value1;

SomeEnum value = SomeEnum::Value1;

您现在会发现“ToString()”按照您的预期工作。

Whilst the other answers are not incorrect, I found myself with the same problem. In my case, I had declared a standard C++ enum and forgot to use the CLI syntax (even though I had it been exposed in public properties without compiler warnings!).

The proper syntax for a C++/CLI enum is (Note the word 'class'):

public enum class SomeEnum {
    Value1,
    Value2
}

NB: You can also use 'struct' rather than 'class'.

You will also need to go back through your code and change any assignments (the compiler will happily show you the errors however) from this:

SomeEnum value = Value1;

to this:

SomeEnum value = SomeEnum::Value1;

You will now find that 'ToString()' works as you'd expect it to.

泪痕残 2024-08-10 19:59:47

语法不是一样吗?

MyEnum enumValue = MyEnum::Enum1;
Console::WriteLine(enumValue.ToString());

请参阅 Google 图书

“枚举的 ToString() 方法将枚举名称打印为字符串”

Isn't it the same syntax ?

MyEnum enumValue = MyEnum::Enum1;
Console::WriteLine(enumValue.ToString());

Look at this page of the book "Pro Visual C++/CLI and the .NET 3.5 Platform" in Google Books

"The ToString() method for enum prints out the enum name as a character string"

风透绣罗衣 2024-08-10 19:59:47

ToString() 应该返回枚举值的名称。此外,如果您使用 [Flags] 属性装饰托管枚举,则例如 Colour::Red | Colour::Blue 将 ToString() 视为“红、蓝”。 (这是根据Marcus Heege的书“Expert Visual C++/CLI: .NET for Visual C++ Programmers”的记忆,我自己没有尝试过)

ToString() should return the name of the enum value. Furthermore, if you decorate the managed Enum with a [Flags] attribute, then eg Colour::Red | Colour::Blue will ToString() as "Red, Blue". (This is from memory from Marcus Heege's book "Expert Visual C++/CLI: .NET for Visual C++ Programmers", not tried it myself)

虐人心 2024-08-10 19:59:47

您的问题可能已得到解决。
我认为其他人可能会发现它有用:

NOT:成员数量最多可达 96 个。( ENUM96.inl )

#include "ENUM96.inl"

//
// DirectShowDeviceType
// ENUM96 sample definition
//
ENUM96(DirectShowDeviceType, unsigned int,
    None = 0,
    Input = 1,    // Capture
    Output = 2,   // Renderer
    Video = 4,    // Video support
    Audio = 8     // Audio support
    );

int main(int argc, char* argv[])
{
    DirectShowDeviceType eDevType = DirectShowDeviceType::Input
        | DirectShowDeviceType::Video
        | DirectShowDeviceType::Audio;

    printf("[ENUM96 Test]\n\n");

    _tprintf(_T("> %-24s --> \"%s\" (%u)\n"), _T("ToString()")
        , eDevType.ToString()
        , eDevType.ToUInt32());

    printf("> %-24s --> %s\n", "HasFlag(Output)",
        eDevType.HasFlag(DirectShowDeviceType::Output)
        ? "true"
        : "false");

    printf("> %-24s --> %s\n", "HasFlag(Input)",
        eDevType.HasFlag(DirectShowDeviceType::Input)
        ? "true"
        : "false");

    printf("> %-24s --> %s\n", "HasFlag(Video)",
        eDevType.HasFlag(DirectShowDeviceType::Video)
        ? "true"
        : "false");

    printf("> %-24s --> %s\n", "HasFlag(Audio)",
        eDevType.HasFlag(DirectShowDeviceType::Audio)
        ? "true"
        : "false");

    printf("> %-24s --> %s\n", "HasFlag(Input | Audio)",
        eDevType.HasFlag(DirectShowDeviceType::Input | DirectShowDeviceType::Audio)
        ? "true"
        : "false");

    printf("> %-24s --> %s\n", "HasFlag(Input | Video)",
        eDevType.HasFlag(DirectShowDeviceType::Input | DirectShowDeviceType::Video)
        ? "true"
        : "false");

    printf("> %-24s --> %s\n", "HasFlag(Output | Video)",
        eDevType.HasFlag(DirectShowDeviceType::Output | DirectShowDeviceType::Video)
        ? "true"
        : "false");

    printf("> %-24s --> %s\n", "HasFlag(Video | Audio)",
        eDevType.HasFlag(DirectShowDeviceType::Video | DirectShowDeviceType::Audio)
        ? "true"
        : "false");

    printf("\n");
    printf("> // Remove 'Video' flag\n");
    eDevType &= ~DirectShowDeviceType::Video;

    _tprintf(_T("> %-24s --> \"%s\" (%u)\n"), _T("ToString()")
        , eDevType.ToString()
        , eDevType.ToUInt32());

    printf("> %-24s --> %s\n", "HasFlag(Video)",
        eDevType.HasFlag(DirectShowDeviceType::Video)
        ? "true"
        : "false");

    printf("> %-24s --> %s\n", "HasFlag(Video | Audio)",
        eDevType.HasFlag(DirectShowDeviceType::Video | DirectShowDeviceType::Audio)
        ? "true"
        : "false");

    printf("> %-24s --> %s\n", "HasFlag(Audio)",
        eDevType.HasFlag(DirectShowDeviceType::Audio)
        ? "true"
        : "false");

    printf("> %-24s --> %s\n", "HasFlag(Input)",
        eDevType.HasFlag(DirectShowDeviceType::Input)
        ? "true"
        : "false");

    printf("> %-24s --> %s\n", "HasFlag(Input | Audio)",
        eDevType.HasFlag(DirectShowDeviceType::Input | DirectShowDeviceType::Audio)
        ? "true"
        : "false");

    printf("\n");
    printf("> // List ENUM members\n");
    printf("> %-24s --> %u\n", "MemberCount",
        eDevType.GetMemberCount());
    for (int i = 0; i < eDevType.GetMemberCount(); i++) {
        DirectShowDeviceType eMember = eDevType.GetMember(i);
        _tprintf(_T("  [%u] %s\n"), eMember.ToUInt32(), eMember.ToString());
    }

    getchar();
    return 0;
}

结果如下所示。

    [ENUM96 Test]
    
    > ToString()               --> "Input | Video | Audio" (13)
    > HasFlag(Output)          --> false
    > HasFlag(Input)           --> true
    > HasFlag(Video)           --> true
    > HasFlag(Audio)           --> true
    > HasFlag(Input | Audio)   --> true
    > HasFlag(Input | Video)   --> true
    > HasFlag(Output | Video)  --> false
    > HasFlag(Video | Audio)   --> true
    
    > // Remove 'Video' flag
    > ToString()               --> "Input | Audio" (9)
    > HasFlag(Video)           --> false
    > HasFlag(Video | Audio)   --> false
    > HasFlag(Audio)           --> true
    > HasFlag(Input)           --> true
    > HasFlag(Input | Audio)   --> true
    
    > // List ENUM members
    > MemberCount              --> 5
      [0] None
      [1] Input
      [2] Output
      [4] Video
      [8] Audio

Your issue was likely resolved.
I thought someone else might find it useful:

NOT: The number of members can be up to 96. ( ENUM96.inl )

#include "ENUM96.inl"

//
// DirectShowDeviceType
// ENUM96 sample definition
//
ENUM96(DirectShowDeviceType, unsigned int,
    None = 0,
    Input = 1,    // Capture
    Output = 2,   // Renderer
    Video = 4,    // Video support
    Audio = 8     // Audio support
    );

int main(int argc, char* argv[])
{
    DirectShowDeviceType eDevType = DirectShowDeviceType::Input
        | DirectShowDeviceType::Video
        | DirectShowDeviceType::Audio;

    printf("[ENUM96 Test]\n\n");

    _tprintf(_T("> %-24s --> \"%s\" (%u)\n"), _T("ToString()")
        , eDevType.ToString()
        , eDevType.ToUInt32());

    printf("> %-24s --> %s\n", "HasFlag(Output)",
        eDevType.HasFlag(DirectShowDeviceType::Output)
        ? "true"
        : "false");

    printf("> %-24s --> %s\n", "HasFlag(Input)",
        eDevType.HasFlag(DirectShowDeviceType::Input)
        ? "true"
        : "false");

    printf("> %-24s --> %s\n", "HasFlag(Video)",
        eDevType.HasFlag(DirectShowDeviceType::Video)
        ? "true"
        : "false");

    printf("> %-24s --> %s\n", "HasFlag(Audio)",
        eDevType.HasFlag(DirectShowDeviceType::Audio)
        ? "true"
        : "false");

    printf("> %-24s --> %s\n", "HasFlag(Input | Audio)",
        eDevType.HasFlag(DirectShowDeviceType::Input | DirectShowDeviceType::Audio)
        ? "true"
        : "false");

    printf("> %-24s --> %s\n", "HasFlag(Input | Video)",
        eDevType.HasFlag(DirectShowDeviceType::Input | DirectShowDeviceType::Video)
        ? "true"
        : "false");

    printf("> %-24s --> %s\n", "HasFlag(Output | Video)",
        eDevType.HasFlag(DirectShowDeviceType::Output | DirectShowDeviceType::Video)
        ? "true"
        : "false");

    printf("> %-24s --> %s\n", "HasFlag(Video | Audio)",
        eDevType.HasFlag(DirectShowDeviceType::Video | DirectShowDeviceType::Audio)
        ? "true"
        : "false");

    printf("\n");
    printf("> // Remove 'Video' flag\n");
    eDevType &= ~DirectShowDeviceType::Video;

    _tprintf(_T("> %-24s --> \"%s\" (%u)\n"), _T("ToString()")
        , eDevType.ToString()
        , eDevType.ToUInt32());

    printf("> %-24s --> %s\n", "HasFlag(Video)",
        eDevType.HasFlag(DirectShowDeviceType::Video)
        ? "true"
        : "false");

    printf("> %-24s --> %s\n", "HasFlag(Video | Audio)",
        eDevType.HasFlag(DirectShowDeviceType::Video | DirectShowDeviceType::Audio)
        ? "true"
        : "false");

    printf("> %-24s --> %s\n", "HasFlag(Audio)",
        eDevType.HasFlag(DirectShowDeviceType::Audio)
        ? "true"
        : "false");

    printf("> %-24s --> %s\n", "HasFlag(Input)",
        eDevType.HasFlag(DirectShowDeviceType::Input)
        ? "true"
        : "false");

    printf("> %-24s --> %s\n", "HasFlag(Input | Audio)",
        eDevType.HasFlag(DirectShowDeviceType::Input | DirectShowDeviceType::Audio)
        ? "true"
        : "false");

    printf("\n");
    printf("> // List ENUM members\n");
    printf("> %-24s --> %u\n", "MemberCount",
        eDevType.GetMemberCount());
    for (int i = 0; i < eDevType.GetMemberCount(); i++) {
        DirectShowDeviceType eMember = eDevType.GetMember(i);
        _tprintf(_T("  [%u] %s\n"), eMember.ToUInt32(), eMember.ToString());
    }

    getchar();
    return 0;
}

The result will be as shown below.

    [ENUM96 Test]
    
    > ToString()               --> "Input | Video | Audio" (13)
    > HasFlag(Output)          --> false
    > HasFlag(Input)           --> true
    > HasFlag(Video)           --> true
    > HasFlag(Audio)           --> true
    > HasFlag(Input | Audio)   --> true
    > HasFlag(Input | Video)   --> true
    > HasFlag(Output | Video)  --> false
    > HasFlag(Video | Audio)   --> true
    
    > // Remove 'Video' flag
    > ToString()               --> "Input | Audio" (9)
    > HasFlag(Video)           --> false
    > HasFlag(Video | Audio)   --> false
    > HasFlag(Audio)           --> true
    > HasFlag(Input)           --> true
    > HasFlag(Input | Audio)   --> true
    
    > // List ENUM members
    > MemberCount              --> 5
      [0] None
      [1] Input
      [2] Output
      [4] Video
      [8] Audio

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