Click here to Skip to main content
15,942,156 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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.");
                case 1:
                    Zaber.Settings1.Default.name1 = newName;
                case 2:
                    Zaber.Settings1.Default.name2 = newName;
                case 3:
                    Zaber.Settings1.Default.name3 = newName;
                    Zaber.Settings1.Default.name6 = newName;


1 solution

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.
Share this answer
Member 8151917 22-Aug-11 19:24pm    
Thanks for the response, and I apologize for the lack of context, the program is somewhat large, so I'm primarily hoping that someone has experienced a similar problem and determined the cause. As I said, I have already verified the content of <pre lang="c#">Conversation.Device.DeviceNumber</pre> to be what I expect it to be. I'm going through now and checking the call stack.
GParkings 1-Sep-11 9:03am    
At what point have you verified Conversation.Device.DeviceNumber? was it at the point at which it is referred to within the switch statement? if it is correct at that point then stepping through the code line by line should confirm that the correct case is being entered (i can see no way in which that would not be the case).

Assuming that the above has been done then it is likely that either:

- something else is setting the Zaber.Settings1.Default.nameX properties
- This code is being unexpectedly run again (probably multiple times) and is setting the properties to the values you are seeing
- Zaber, Zaber.Settings1 or Zaber.Settings1.Default is being recreated and you are seeing the default (post instantiation) values
- your Zaber.Settings1.Default.nameX properties (I hope they are properties and not public fields) have incorrect setter code that is not actually setting their underlying field
GParkings 1-Sep-11 9:06am    
OR ... having re-read your symptoms:

the Zaber.Settings1.Default.nameX property getters are all returning the underlying field of Zaber.Settings1.Default.name6 (copy and paste error)

if that were the case, changing any other other property fields would appear to have no effect as the value returned for each would be the name6 value (i assume it has the same default as the other properties and therefore would give the appearance of no change), but changing name6 would mean that every nameX property returned the new value for name6

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900