(可能)奇怪的开关/案例问题 [英] (Possibly) Strange switch/case problem

查看:71
本文介绍了(可能)奇怪的开关/案例问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为一个更大的电机控制程序的一部分,我有一个切换语句,该语句使用当前电机的ID号并分配一个名称.基本上,用户在文本框中输入一个新名称,然后单击按钮(下面的代码),然后更改了电动机名称的默认设置.有一系列具有顺序ID号的电动机(0表示所有电动机).

从逻辑上(至少在我有限的C#知识基础上),如果"Conversation.Device.DeviceNumber"值为1,则开关应转到情况1并执行命令,然后中断.

但是,似乎正在发生的事情是,对于给定的电动机链,尝试更改任何编号较低的电动机的名称均无效,而更改编号最大的电动机名称将更改所有电动机名称.经过一段时间的程序摆弄,没有任何进展,我只是假设我错过了一些明显的东西.帮助会很棒.注意,我已经测试了"Conversation.Device.DeviceNumber"结果,它为每个选定的电动机提供了正确的数字.谢谢.


As part of a larger motor control program I have a switching statement that takes the current motor''s id number and assigns a name. Basically, the user enters a new name into a textbox and clicks the button(code below), and the default setting for a motor name is changed. There is a chain of motors with sequential id numbers (0 refers to all motors).

Logically (at least with my limited knowledge of C#) if the ''Conversation.Device.DeviceNumber'' value is 1, the switch should go to case 1 and execute the command, and then break.

What seems to be happening, though, is that, for a given chain of motors, attempting to change the name for any of the lower numbered motors has no effect, and changing the name of the highest number changes all of the motor names. After fiddling around with the program for a while and making no progress, I''m just assuming I''m missing something obvious. Help would be awesome. As a note, I have tested the ''Conversation.Device.DeviceNumber'' result, and it is giving me the correct number for each selected motor. Thanks.


        private void motorRenameButton_Click(object sender, EventArgs e)
        {
            string newName = newNameTextBox.Text;
            switch (Conversation.Device.DeviceNumber)
            {
                case 0:
                    MessageBox.Show("You have selected all motors. The name of this property cannot be changed.");
                    break;
                case 1:
                    Zaber.Settings1.Default.name1 = newName;
                    break;
                case 2:
                    Zaber.Settings1.Default.name2 = newName;
                    break;
                case 3:
                    Zaber.Settings1.Default.name3 = newName;
                    break;
                default:
                    Zaber.Settings1.Default.name6 = newName;
                    break;
            }

            Zaber.Settings1.Default.Save();
}

推荐答案

使用调试器验证Conversation.Device.DeviceNumber的内容.

即使您已尽力描述您的问题,也没有任何相关信息可帮助我们找到您的问题. switch语句确实按照描述的方式工作...因此,如果您有问题,则其他地方出了问题.

首先验证您的变量和属性确实包含期望值.

另外,取决于代码的完成方式,可能会有一些冲突……就像您不期望的那样调用处理程序.

设置断点并查看调用堆栈对于此类问题有很大帮助.是否在需要时调用处理程序.
Verify the content of Conversation.Device.DeviceNumber with a debugger.

Even though you''ve made an effort ot describe your problem, there is no pertinent information to help us find your problem. Switch statements do works as described... thus if you have a problem, something is wrong elsewhere.

Start by verifying that your variables and properties do contains the expected values.

Also depending on how your code is done there might be some conflicts... like an handler that is called when you don''t expect.

Setting breakpoints and looking the call stack help a lot for that kind of problem. Do the handler is called when expected.


这篇关于(可能)奇怪的开关/案例问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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