标签: switch-statement

Switch(Case)总是错的吗?

是否存在switch(case)是战略或类似模式的良好设计选择(简单除外)的情况......

language-agnostic design-patterns switch-statement

11
推荐指数
4
解决办法
3686
查看次数

.NET:用于字符串键的切换与字典

我有一种情况,我有一个业务对象,有大约15种不同类型的属性.业务对象还必须实现具有以下方法的接口:

object GetFieldValue(string FieldName);
Run Code Online (Sandbox Code Playgroud)

我可以看到实现此方法的两种方法:

使用switch语句:

switch ( FieldName )
{
    case "Field1": return this.Field1;
    case "Field2": return this.Field2;
    // etc.
}
Run Code Online (Sandbox Code Playgroud)

使用字典(SortedDictionary或HashTable?):

return this.AllFields[FieldName];
Run Code Online (Sandbox Code Playgroud)

哪个会更有效率?

补充:忘了说.此方法用于在网格中显示项目.网格将为每个属性都有一列.通常会有超过1000个项目的网格.这就是为什么我关注性能.

新增2:

这是一个想法:混合方法.创建一个静态字典,其中键是属性名称,值是数组中的索引.在应用程序启动时,字典仅填充一次.每个对象实例都有一个数组.所以,查找将是:

return this.ValueArray[StaticDictionary[FieldName]];
Run Code Online (Sandbox Code Playgroud)

字典填充算法可以使用反射.然后将相应地实现属性本身:

public bool Field1
{
    get
    {
        object o = this.ValueArray[StaticDictionary["Field1"]]; 
        return o == null ? false : (bool)o;
    }
    set
    {
        this.ValueArray[StaticDictionary["Field1"]] = value;
    }
}
Run Code Online (Sandbox Code Playgroud)

任何人都可以看到这个问题吗?

它也可以更进一步,ValueArray/StaticDictionary可以放在一个单独的泛型类型中ValueCollection<T>,其中T将指定反射的类型.ValueCollection还将处理尚未设置任何值的情况.然后可以将属性简单地写为:

public bool Field1
{
    get
    {
        return (bool)this.Values["Field1"];
    }
    set
    {
        this.Values["Field1"] = value;
    } …
Run Code Online (Sandbox Code Playgroud)

.net dictionary switch-statement

11
推荐指数
2
解决办法
5002
查看次数

可以在java中使用switch(java.lang.Object)吗?

我的应用程序需要具有类型的switch case语句String.

我需要这样的东西:

    Object list1 = "list1";
    Object list2 = "list2";
    Object list3 = "list3";

    Object option = "list1";
    switch (option) {
        case list1: // Do something
        case list2: // Do something
        case list3: // Do something
        default:    // Do something
    }
Run Code Online (Sandbox Code Playgroud)

有可能吗?

编辑:

是否更适合使用开关盒n而不是if 和else?一起使用?请评论一下?

java if-statement object switch-statement

11
推荐指数
1
解决办法
2万
查看次数

切换语句>可以在一个案例中包含多个案例匹配吗?

我想在一个交换机案例中传递多个值.我意识到我不可能这样做.还有另一种方法,没有将每个案例放在网上吗?

switch(get_option('my_template'))
{
case 'test1', 'test2':
return 850;
break;

default:
return 950;
}
Run Code Online (Sandbox Code Playgroud)

php switch-statement

11
推荐指数
1
解决办法
1万
查看次数

如何在Javascript中使用typeof和switch case

我在查找下面的代码有什么问题时遇到了问题.我已经咨询过如何使用typeof和switch case,但此时我已经迷路了.提前感谢您的建议.

// Write a function that uses switch statements on the
// type of value. If it is a string, return 'str'. If it
// is a number, return 'num'. If it is an object, return
// 'obj'. If it is anything else, return 'other'.
function detectType(value) {
  switch (typeof value) {
    case string:
      return "str";
    case number:
      return "num";
    default:
      return "other";
  }
}
Run Code Online (Sandbox Code Playgroud)

-------------更新-----------------------------------

事实证明,问题来自于我没有正确遵循指示的错误(或者说是疏忽).再次感谢您的帮助和评论!

javascript typeof switch-statement

11
推荐指数
1
解决办法
1万
查看次数

给定/何时实验的哪些部分?

整个"切换"功能是否已成为实验性的?在没有未来版本的Perl破坏我的代码的情况下,我可以依赖它的部分吗?总的来说,将稳定特征改为实验的政策是什么?

背景

use feature "switch"自5.10以来一直在Perl.从5.10到5.14,perlsyn似乎表明这是一个稳定的,支持的功能.

然而,perlsyn 从 5.16 开始,它开始称之为 " 实验性开关功能",并且对于被认为是实验的内容更加困惑.

部分文档似乎表明该功能的所有内容都是实验性的:

在"开关"功能,Perl中获得了实验用的关键字given,when,default,continue,和break.

甚至还有关于给定和何时的实验细节的整个部分.

然而,perlsyn 也说"这foreach是设置一个局部化器的非实验性方法",并给出了一个例子,似乎暗示foreach/when不是实验性的.

据我所知,"实验"语言来自提交c2f1e22,它引用了RT#90926,即使与RT#90906配对,它仍然没有提供太多的上下文.

syntax perl switch-statement

11
推荐指数
1
解决办法
2516
查看次数

我如何为jQuery做一个switch case?

我有这个if-else语句给了我奇怪的回应......每当我首先选择"输出"时,之后没有其他选择可以出现...仅供参考,我使用多选我所以我可以选择并显示我想要的数量.

 $('#outputText').hide();
    $('#armCB').hide();
    $('#outputCB').hide();
    $('#zoneText').hide();
    $('#counterText').hide();
    $('#flagText').hide();
    $('#sensorText').hide();

('#select-choice-1').change(function(){
        if ($('#output').is(':selected')){
            $('#outputText').show();
        }
        else if ($('#arm').is(':selected')){
            $('#armCB').show();
        }
        else if ($('#zone').is(':selected')){
            $('#zoneText').show();
        }
        else if ($('#counter').is(':selected')){
            $('#counterText').show();
        }
        else if ($('#flag').is(':selected')){
            $('#flagText').show();
        }
        else if ($('#sensor').is(':selected')){
            $('#sensorText').show();
        }
        else{
            $('#outputText').hide();
            $('#armCB').hide();
            $('#zoneText').hide();
            $('#counterText').hide();
            $('#flagText').hide();
            $('#sensorText').hide();

        }
Run Code Online (Sandbox Code Playgroud)

我的if-else声明中有错误吗?或者我必须在这里使用Switch case语句吗?如果是这样,我该怎么办?

HTML:

<div id="display" style=" clear:both">
        <div class="left" style="float:left; width:48%">
        <div data-role="fieldcontain">
            <label for="select-choice-1" class="select">Select Category</label>
            <select name="select-choice-1" id="select-choice-1" data-native-menu="false" data-mini="true" multiple="multiple" size="2">

                <option value="arm" id="arm">Arm</option>
                <option value="zone" id="zone">Zone Input</option>
                <option value="output" id="output">Output</option> …
Run Code Online (Sandbox Code Playgroud)

jquery if-statement switch-statement

11
推荐指数
2
解决办法
6万
查看次数

使用switch命令中的可能值列表

是否可以为我的switch函数提供一种可能的值列表.如果有人提供了错误的参数,我很乐意让所有内容自动更新.

我使用{}for each条件,因为我将在我的函数中执行更多然后只是这个变量声明,我会在这个开关中做几件事.

switch(con,
       val1={
         filename <- 'SILAC-DML_with_PDF.R'
       },
       val2={
         filename <- 'SILAC-DML_with_PDF.R'
       },
       stop(sprintf('"%s" is an unknown condition type, please use one of "%s".\n',
                    con, paste(c('val1','val2'), collapse=', '))))
Run Code Online (Sandbox Code Playgroud)

我希望有一个列表,我可以粘贴名称,以获得可能的值.所以理想的解决方案有点像这样,但没有错误信息:-)

my_list <- list(val1=filename <- 'a.R',
                val2=filename <- 'b.R')
switch(con,
       my_list,
       stop(sprintf('"%s" is an unknown condition type, please use one of "%s".\n',
                    con, names(my_list), collapse=', '))))
Run Code Online (Sandbox Code Playgroud)

r switch-statement

11
推荐指数
1
解决办法
840
查看次数

如何在C中的switch case语句中使用范围?

我的逻辑是:

if number is between 1 to 10, execute first case statement
if number is from 20 to 30, execute second case statement
Run Code Online (Sandbox Code Playgroud)

有没有下面的解决方案?

case '1' ... '10':
case '20' ... '30':
Run Code Online (Sandbox Code Playgroud)

c switch-statement

11
推荐指数
3
解决办法
3万
查看次数

强制tablewitch而不是lookupswitch

Scala 2.11将match相对密集Int范围内的表达式编译为lookupswitch:

lookupswitch { // 21
    -12: 200
    -11: 200
    -10: 184
     -9: 190
     -8: 190
     -7: 190
     -6: 190
     -5: 190
     -4: 200
     -1: 200
      2: 195
      3: 195
      4: 195
      5: 195
      6: 184
      7: 184
     12: 184
     13: 184
     18: 184
     21: 184
     25: 184
default: 180
}
Run Code Online (Sandbox Code Playgroud)

而Java 7将等效switch语句编译为tableswitch:

tableswitch { // -12 to 25
    -12: 168
    -11: 168
    -10: 177
     -9: 174
     -8: 174
     -7: …
Run Code Online (Sandbox Code Playgroud)

java jvm scala pattern-matching switch-statement

11
推荐指数
1
解决办法
1018
查看次数