How to use a switch statement with Guid?(如何在 Guid 中使用 switch 语句?)
问题描述
在以下 C# 代码中,cboRole 返回一个 Guid.
In the following C# code, cboRole returns a Guid.
然后我尝试在 switch 语句中使用它来执行一些操作.
I'm then trying to use it in a switch statement to do some actions.
cboRole 只能返回 4 个不同的 Guid,所以我认为切换对我来说是一个不错的选择.
cboRole can return only 4 different Guid so I think a switch is a good option for me.
问题是所有情况都被忽略了,我总是得到默认操作.当我调试时,我清楚地看到 cboRole 返回一个值,如下面的打印屏幕所示.
The thing is that all the case are ignored and I always get the default action. When I debug I clearly see that cboRole return a value like in the following printscreen.
在 C# switch 语句中比较"Guid 的正确方法是什么
What is the correct way to "compare" Guids in a C# switch statement
代码:
if (!cboRole.IsNull)
{
switch (cboRole.EditValue.ToStringEx())
{
case "532E8EED-9E72-42E0-871E-36470C1AE327":
param1 = "4E08BA7C-E81F-40AE-92F0-DF33A98DD0BB";
MessageBox.Show("It's working");
break;
case "FA7637E9-A9E4-4D57-A59B-80615424D27F":
param1 = "E540C382-F22C-4FE2-9068-1E10AA8DD076";
break;
case "2734CCD9-93E6-4A86-8B83-5EA9E62FA921":
param1 = "8A54F8D5-5B74-4B3F-A29A-D423AA8DD02E";
break;
default:
MessageBox.Show("Not Working");
break;
}
推荐答案
你的 switch 语句是大写的,而你的 ToStringEx()
返回的是小写的 Guid
案例.
Your switch statements are in upper-case while your ToStringEx()
returns the Guid
in lower-case.
你可以使用
switch (cboRole.EditValue.ToStringEx().ToUpper())
或修改您的 case
语句.
这篇关于如何在 Guid 中使用 switch 语句?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在 Guid 中使用 switch 语句?


基础教程推荐
- 如何动态获取文本框中datagridview列的总和 2022-01-01
- 全局 ASAX - 获取服务器名称 2022-01-01
- 首先创建代码,多对多,关联表中的附加字段 2022-01-01
- 错误“此流不支持搜索操作"在 C# 中 2022-01-01
- 从 VS 2017 .NET Core 项目的发布目录中排除文件 2022-01-01
- 经典 Asp 中的 ResolveUrl/Url.Content 等效项 2022-01-01
- JSON.NET 中基于属性的类型解析 2022-01-01
- 将事件 TextChanged 分配给表单中的所有文本框 2022-01-01
- 是否可以在 asp classic 和 asp.net 之间共享会话状态 2022-01-01
- 在 VS2010 中的 Post Build 事件中将 bin 文件复制到物 2022-01-01