初学者:在switch的本地范围之外使用数组 [英] Beginner: Using array outside of switch's local scope

查看:96
本文介绍了初学者:在switch的本地范围之外使用数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近从Java过渡。我已经在switch语句的本地范围之外初始化了一个数组。但是在每个开关的情况下,我希望在匹配的情况下填充数组。例如:



Recently transitioned over from Java. I've initialized an array outside the local scope of my switch statement. However in each switch case I wish to populate the array in the case matched. For example:

int[] Stment = {};
....Code
switch (level)
           {
               case 5:
                   Stment = new int[] { 5, 10, 15, 20 };
                   break;
               case 50:
                   Stment = new int[] { 50, 60, 70, 80};
                   break;
           }
           string disp = string.Join(Environment.NewLine, Stment);
           MessageBox.Show(disp);</pre>



有没有办法让我在交换机的本地范围之外使用填充的数组?


Is there a way for me to use the populated array outside the switch's local scope?

推荐答案

有两种方法:第一种是在你定义变量时赋值变量。

There are two ways: first is to assign a value took the variable when you define it.
int[] Stment = null;






Or

int[] Stment = new int[0];



会这样做。

或者,将默认案例添加到开关块:


Will do it.
Alternatively, add a default case to your switch block:

default: Stment = null;






Or

default: Stment = new int[0];



只要编译器可以看到通过代码的任何路由,其中​​变量未在其前面给出值使用时,你会得到那个错误 - 即使逻辑确实阻止它发生。


As long as the compiler can see any route through the code where the variable is not given a value before it is used, you will get that error - even if the logic actually prevents it happening.


作为变量 Stme nt 是在开关上下文声明的,你也可以在它之后使用它。 (尽管您的样本中显示了可怕的,令人无法接受的错误编码样式。我希望它只是例如)。



唯一的问题是如何你试着用它。 System.String.Join 需要非常不同类型的第二个参数, string [] 。但这个小问题与你的问题无关。我甚至不知道你为什么要问它;如果您使用 Stment 对象执行合法操作,则可以轻松验证代码。此外,简单的逻辑应该告诉你这是非常合法的事情。



-SA
As the variable Stment is declared outside the switch context, you can use it after it as well. (Despite horrific, unacceptably bad coding style shown in your sample. I hope it's only for example).

The only problem is how you tried to use it. System.String.Join expects second parameter of very different type, string[]. But this little problem has nothing to do with your question. I don't even know why would you need asking it; you could easily validate your code if you did something legitimate with your Stment object. Besides, simple logic should tell you that this is quite legitimate thing.

—SA


无论你做什么,我建议你测试'switch语句的结果是否在执行依赖的有效结果的代码之前给出了有效的结果::
Whatever you do, I suggest you test whether the result of the 'switch statement gave a valid result before executing any code that depends on a valid result::
int[] Stment = null;

switch (level)
{
    case 5:
        Stment = new int[] {5, 10, 15, 20};
        break;
    case 50:
        Stment = new int[] {50, 60, 70, 80};
        break;
}

if (Stment == null)
{
    // throw an error ?
}
else
{
    string disp = string.Join(Environment.NewLine, Stment);
    MessageBox.Show(disp);
}


这篇关于初学者:在switch的本地范围之外使用数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆