将字符串值与枚举的字符串值进行比较

发布于 2024-12-05 04:25:50 字数 1350 浏览 3 评论 0原文

下面的 C# 代码在以 case 开头的两行上给出了错误。错误是“需要一个常量值”

下面的 VB.NET 代码正在运行。我使用此代码作为用 C# 编写的真实应用程序的示例。

我没有看到问题所在,但这并不意味着问题不存在。我使用了几个在线代码转换器来仔细检查语法。两者返回相同的结果,这给出了错误。

ExportFormatType 是第三方库中的枚举。

有什么建议吗?谢谢!

public void ExportCrystalReport(string exportType, string filePath)
    {
        if (_CReportDoc.IsLoaded == true)
        {
            switch (exportType)
            {
                case  ExportFormatType.PortableDocFormat.ToString():  // Or "PDF"
                    ExportTOFile(filePath, ExportFormatType.PortableDocFormat);
                    break;
                case ExportFormatType.CharacterSeparatedValues.ToString(): // Or "CSV"
                    ExportTOFileCSV(filePath, ExportFormatType.CharacterSeparatedValues);

                    break;
            }
        }


 Public Sub ExportCrystalReport(ByVal exportType As String, ByVal filePath As String)

        If _CReportDoc.IsLoaded = True Then
            Select Case exportType
                Case ExportFormatType.PortableDocFormat.ToString 'Or "PDF"
                    ExportTOFile(filePath, ExportFormatType.PortableDocFormat)
                Case ExportFormatType.CharacterSeparatedValues.ToString ' Or "CSV"
                    ExportTOFileCSV(filePath, ExportFormatType.CharacterSeparatedValues)

The C# code below is giving me error on the two lines beginning with case . The error is "A constant value is expected"

The VB.NET code below is working. I'm using this code as a sample for my real app written in C#.

I don't see the problem but that doesn't mean one isn't present. I used a couple of online code converters to double-check the syntax. Both are returning the same result, which gives the error.

ExportFormatType is an enum in a third-party library.

Any suggestions? Thanks!

public void ExportCrystalReport(string exportType, string filePath)
    {
        if (_CReportDoc.IsLoaded == true)
        {
            switch (exportType)
            {
                case  ExportFormatType.PortableDocFormat.ToString():  // Or "PDF"
                    ExportTOFile(filePath, ExportFormatType.PortableDocFormat);
                    break;
                case ExportFormatType.CharacterSeparatedValues.ToString(): // Or "CSV"
                    ExportTOFileCSV(filePath, ExportFormatType.CharacterSeparatedValues);

                    break;
            }
        }


 Public Sub ExportCrystalReport(ByVal exportType As String, ByVal filePath As String)

        If _CReportDoc.IsLoaded = True Then
            Select Case exportType
                Case ExportFormatType.PortableDocFormat.ToString 'Or "PDF"
                    ExportTOFile(filePath, ExportFormatType.PortableDocFormat)
                Case ExportFormatType.CharacterSeparatedValues.ToString ' Or "CSV"
                    ExportTOFileCSV(filePath, ExportFormatType.CharacterSeparatedValues)

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

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

发布评论

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

评论(1

心如狂蝶 2024-12-12 04:25:50

在 C# 中,case 语句标签必须是编译时已知的值。我不认为同样的限制也适用于 VB.NET。

原则上, ToString() 可以运行任意代码,因此它的值在编译时是未知的(即使在您的情况下它是一个枚举)。

要解决此问题,您可以首先将 exportType 解析为枚举,然后在 C# 中打开枚举值:

ExportFormatType exportTypeValue = Enum.Parse(typeof(ExportFormatType), exportType);
switch( exportTypeValue )
{
    case ExportFormatType.PortableDocFormat: // etc...

或者您可以将开关转换为 if/else 语句:

if( exportType == ExportFormatType.PortableDocFormat.ToString() )
   // etc...

In C#, case statement labels must be values known at compile time. I don't believe this same restriction holds true for VB.NET.

In principle, ToString() can run arbitrary code, so it's value is not known at compile time (even though in your case it is an enum).

To address this you can either first parse the exportType into a enum, and switch on the enum value in C#:

ExportFormatType exportTypeValue = Enum.Parse(typeof(ExportFormatType), exportType);
switch( exportTypeValue )
{
    case ExportFormatType.PortableDocFormat: // etc...

or you could convert the switch to an if/else statement:

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