I have been working on a project, where I am able to choose the different positions of the body parts of a robot to form a dance step. The head with 3 movements. The left and right arm, shoulder and elbow with 4, 4 and 3 movements respectively. The waist with 5 movements and the direction with 5 movements too.

Now, I'm facing a problem, I want to be able to see the preview of the robot, which involves using images and LOTS of if-statements. However, that will involve alot of if-statements to write, which will be VERY time consuming and messy. I tried using a switch statement but I realized using a switch statement doesn't work for a string.

I would like to ask if there is any available conditional statements or any available alternatives I can use so as to decrease the codes I need to write. The values used to compare will be strings FYI. I am using .NET Framework 4 and Microsoft Visual Studio 2008, Windows 7 if the information is necessary.

Thanks alot of helping and pardon my english :S

Recommended Answers

All 5 Replies

Switch/case works for Strings, so I'm not sure what your issue is. You can also use enumerated types (which is probably a better solution than using strings).

So my questions for you are:
1) Why do you have to use strings?
2) Why do you think you can't use the String type in a switch/case?
3) Can you give an example of what you are doing for one of your parts?

1) Unfortunately, I used strings for saving the dance steps

2) My bad on this one, i forgot to add a break;

3)

switch (sm.arm_action.leftArm)
            {
                case "1":
                    if (sm.head_action.headTilt == "1" && sm.arm_action.leftShoulder == "1" && sm.arm_action.leftArm == "1" && sm.arm_action.leftElbow == "1" && sm.waist_action.waistTilt == "1" && sm.wheel_action.direction == "w")
                    {

                        Preview.Image = RoboDancer.Properties.Resources.Middle;

                    }
                    break;
            }

and etc etc.
I have no idea if this helps, but please advice, thanks alot

What you use for persistence (storage) of your data and what you use in your code do not have to be the same.
Using enumerated types is probably better and will make the code more readable.
I.E. instead of case "1": you could have case ArmAction.ArmUp: which is easier to understand.

To convert from a string (i.e. your saved data format) to an enum type is relatively easy.
The Enum.Parse method does a lot of the work for you.

enum ArmAction
        {
            ArmUp = 1,
            ArmDown = 2,
            ArmMiddle = 3
        }

        private void test()
        {
            string test1 = "2";
            string test2 = "ArmUp";
            ArmAction a;
            ArmAction b;
            a = (ArmAction)Enum.Parse(typeof(ArmAction), test1);
            // a is now ArmAction.ArmDown
            b = (ArmAction)Enum.Parse(typeof(ArmAction), test2);      
            // b is now ArmAction.ArmUp
        }

Thanks for the enumerated types example, i will look into that further, but is there any alternatives for me to use for comparing the values and producing an output rather then thousands of if-statements in my program?

It is also possible to use the Flags attribute on an Enum type to allow the values to be merged together.
This could simplify your comparisons but would mean a bit of conversion code to match with your current stored data format.

[Flags]
        enum RobotActions
        {
            LeftArmUp = 0x1,
            LeftArmDown = 0x2,
            LeftArmMiddle = 0x4,
            HeadUp = 0x20,
            HeadDown = 0x40
        }

        private const RobotActions LArmUpHeadDown = RobotActions.LeftArmUp | RobotActions.HeadDown;

        private void test2()
        {
            RobotActions a = RobotActions.HeadDown;
            RobotActions b = RobotActions.LeftArmUp;
            RobotActions testVal = a | b;
            if (testVal == LArmUpHeadDown)
            {
                MessageBox.Show("Test OK");
            }
        }

Each value of a Flags based Enum need not represent a single bit only; although they usually do.
It would be possible to code the LArmUpHeadDown in to the RobotActions type but I think this might clutter the type up too much.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.