将字符串值与枚举的字符串值进行比较
下面的 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 C# 中,case 语句标签必须是编译时已知的值。我不认为同样的限制也适用于 VB.NET。
原则上,
ToString()
可以运行任意代码,因此它的值在编译时是未知的(即使在您的情况下它是一个枚举)。要解决此问题,您可以首先将
exportType
解析为枚举,然后在 C# 中打开枚举值:或者您可以将开关转换为 if/else 语句:
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#:or you could convert the switch to an if/else statement: