C# 使用布尔值切换大小写
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16662631/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Switch case with Boolean
提问by user1445894
I am trying to create a prefix text value for a project. I am using switch case for that. When the user select the relevant radio button we should give the prefix value.
我正在尝试为项目创建前缀文本值。我正在为此使用开关盒。当用户选择相关的单选按钮时,我们应该给出前缀值。
what should I give after the "switch ()"
“开关()”后我应该给什么
The value coming from user selection is a boolean value. The output is string.
来自用户选择的值是一个布尔值。输出是字符串。
any help..
任何帮助..
public string officePostfix()
{
string postfix = null;
switch (...)
{
case SheetMgrForm.Goldcoast = true:
postfix = "QLD";
break;
case SheetMgrForm.Melbourne = true:
postfix = "MEL";
break;
case SheetMgrForm.Sydney = true:
postfix = "SYD";
break;
case SheetMgrForm.Brisbane = true:
postfix = "BIS";
break;
}
return postfix;
}
回答by TyCobb
switchdoes not support this. The cases must be constant. The cases must also be unique.
switch不支持这个。案例必须是恒定的。案例也必须是唯一的。
You need to either turn your boolean properties into an enumand switch off of that or change your switch into a standard ifstatement
您需要将布尔属性转换为 anenum并关闭它或将 switch 更改为标准if语句
http://msdn.microsoft.com/en-us/library/06tc147t(v=vs.110).aspx
http://msdn.microsoft.com/en-us/library/06tc147t(v=vs.110).aspx
回答by Blorgbeard is out
That's not how switchworks. You can't put an arbitrary boolean expression in each case(assuming you mean ==and not =, btw).
事情不是这样的switch。你不能在每一个中放置一个任意的布尔表达式case(假设你的意思是==而不是=,顺便说一句)。
You need to use a set of if-elseblocks, like:
您需要使用一组if-else块,例如:
if (SheetMgrForm.Goldcoast) {
postfix = "QLD";
} else if (SheetMgrForm.Melbourne) {
postfix = "MEL";
} else if ......
However, it looks like your design could use some rework. Having all those separate booleans is clumsy.
但是,看起来您的设计可能需要进行一些返工。拥有所有这些单独的布尔值是笨拙的。
回答by si618
Another approach is to use an enumeration to define your areas, then map these to a resource file. This makes it more maintainable, e.g. localization or further extension in the future, and could be useful if the list is long, i.e. automate the creation of the enum and resources via T4, or you have to query a web service, or whatever...
另一种方法是使用枚举来定义您的区域,然后将它们映射到资源文件。这使得它更易于维护,例如本地化或将来的进一步扩展,如果列表很长可能会很有用,即通过 T4 自动创建枚举和资源,或者您必须查询 Web 服务,或其他任何.. .
For example, say you have an AreaPostfixesresource file and an Areaenumeration.
例如,假设您有一个AreaPostfixes资源文件和一个Area枚举。
public enum Area
{
Goldcoast,
Melbourne,
// ...
}
public static class AreaExtensions
{
public static string Postfix(this Area area)
{
return AreaPostfixes.ResourceManager.GetString(area.ToString());
}
}
// AreaPostfixes.resx
Name Value
Goldcoast QLD
Melbourne MEL
// Usage
public string GetPostfix(Area area)
{
return area.Postfix();
}
That removes any need for switches, etc., the only thing you need to ensure is that there is a 1:1 mapping for each enum and resource. I do this via unit tests but it's easy enough to place an Assert or throw an exception if GetString returns null in the Postfix extension method.
这消除了对开关等的任何需要,您唯一需要确保的是每个枚举和资源都有一个 1:1 的映射。我通过单元测试来做到这一点,但是如果 GetString 在 Postfix 扩展方法中返回 null,那么放置 Assert 或抛出异常很容易。
回答by Jake Hathaway
This would probably return your desired result, but it's sort of an ugly brute force method. The case part is expecting some kind of constant value. In this case, you would have "case true:" or "case false:" (though I'm not sure what you would do with this, in this instance).
这可能会返回您想要的结果,但这是一种丑陋的蛮力方法。案例部分期望某种恒定值。在这种情况下,您将有“case true:”或“case false:”(尽管我不确定在这种情况下您会用它做什么)。
public string officePostfix()
{
string postfix = null;
switch (SheetMgrForm.Goldcoast == true)
{
case true:
postfix = "QLD";
break;
}
switch(SheetMgrForm.Melbourne == true)
{
case true:
postfix = "MEL";
break;
}
switch (SheetMgrForm.Sydney == true)
{
case true:
postfix = "SYD";
break;
}
switch(SheetMgrForm.Brisbane == true)
{
case true:
postfix = "BIS";
break;
}
return postfix;
}

