Wrox Programmer Forums

Need to download code?

View our list of code downloads.

| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: Stephens' C# Programming with Visual Studio 2010 24-Hour Trainer
This is the forum to discuss the Wrox book Stephens' C# Programming with Visual Studio 2010 24-Hour Trainer by Rod Stephens; ISBN: 9780470596906
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Stephens' C# Programming with Visual Studio 2010 24-Hour Trainer section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old November 29th, 2012, 10:37 PM
Authorized User
Points: 130, Level: 2
Points: 130, Level: 2 Points: 130, Level: 2 Points: 130, Level: 2
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2012
Location: Australia
Posts: 32
Thanks: 1
Thanked 0 Times in 0 Posts
Default Switch Help - Lesson 18

Hi,

I have a good mind for understanding 'if' statements and as such I use them a lot. However, on discovering 'switch' in lesson 18, i can see that it would be great to master this. However, I cant understand where it goes

The book doesnt state whether it should be inside a method (like click) or outside.

I just end up with errors on all lines.

What i want to do is use switch instead of 'if' to perform a task based on if a radio button is checked.

I wrote a basic code on the form load method.

Code:
switch (makeinvisible)
    {
    case makeinvisible.radioButton1:
        PictureBox.visible=false;
        break;

        case makeinvisible.radioButton2:
            PictureBox.Visible=true;
            break;
Basically, when the radio button 1 is checked make the picture box invisible and vice versa.

I am using the example on P224 but it doesnt make sense to me. Somehow i have to put in a radiobutton.checked=true. The example on p224 makes it seem like the word string inside the brackets after switch is a declaration for the rest of the switch cases.

Can someone tell me how I can use switch to make something invisible or visible when a radio button is selected.


...LOST
__________________
"These pretzels are making me thirsty"
  #2 (permalink)  
Old November 30th, 2012, 02:08 PM
Rod Stephens's Avatar
Wrox Author
Points: 3,166, Level: 23
Points: 3,166, Level: 23 Points: 3,166, Level: 23 Points: 3,166, Level: 23
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2006
Location: , , .
Posts: 647
Thanks: 2
Thanked 96 Times in 95 Posts
Default

You can use a switch statement whenever you would otherwise use a series of if-else statements that all check the *same* variable against other values. It should go inside a method such as an event handler or a method that you write yourself.

Consider this series of if-else statements:

Code:
if (name == "Ann")
{
    ...
}
else if (name == "Bob")
{
    ...
}
else if (name == "Cindy")
{
    ...
}
etc.
The switch statement's "parameter" is the variable you want to check. The case statements check the various values that you want to compare to the variable.

The previous code turns into:

Code:
switch (name)
{
    case "Ann":
        ...
        break;
    case "Bob":
        ...
        break;
    case "Cindy
        ...
        break;
    etc.
}
Because your radio buttons are two (or more) different values (not a single one like "name" in the previous example), this probably shouldn't be a switch statement. There's no single good value to use as the "parameter." A series of if-else statements probably works better.

You could use it with a ComboBox, though. As in:

Code:
switch (choiceComboBox.Text)
{
    case "Small":
        ...
        break;
    case "Medium":
        ...
        break;
    case "Large":
        ...
        break;
}
The key is that switch compares a single value (the one in parentheses after the switch keyword) with multiple possible values (the ones in the case statements).

I hope that helps. Let me know if it's still confusing.
__________________
Rod

Rod Stephens, Microsoft MVP

Essential Algorithms: A Practical Approach to Computer Algorithms

(Please post reviews at Amazon or wherever you shop!)
  #3 (permalink)  
Old December 2nd, 2012, 10:27 PM
Authorized User
Points: 130, Level: 2
Points: 130, Level: 2 Points: 130, Level: 2 Points: 130, Level: 2
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2012
Location: Australia
Posts: 32
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Thanks, I will do some testing
__________________
"These pretzels are making me thirsty"
  #4 (permalink)  
Old December 19th, 2012, 07:27 PM
Authorized User
Points: 130, Level: 2
Points: 130, Level: 2 Points: 130, Level: 2 Points: 130, Level: 2
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2012
Location: Australia
Posts: 32
Thanks: 1
Thanked 0 Times in 0 Posts
Unhappy This is so doing my head in

I am still having difficulty understanding switch. I have visited many sites, but no one explains it enough. I have used switch successfully once, but it was different to what i am trying to do now and cant get my head around it.

I have done this:
Code:
switch (e.KeyCode)
            {
                case Keys.NumPad1:
                    tbxDisplay.Text = tbxDisplay.Text + "1";
                    break;
             }
Which enters 1 into my text box when the numpad 1 is pressed. I can understand this as an if statement like:

Code:
if (e.KeyCode == Keys.NumPad1)
            {
                tbxDisplay.Text = tbxDisplay.Text + "1";
             }
That I can understand, effectively the first part of the switch (e.keycode) == the case Keys.Numpad1. So its like the == is being removed.

-------------------------------------------
I am trying a different thing now on another project.

I am building a different type of calculator, the structure is below:
textBox1 - enter first number
comboBox1 - select from x-/+
textBox2 - enter second number
comboBox2 - select from x-/+
textBox3 - enter third number
comboBox3 - select from x-/+
textBox4 - enter fourth number

textBoxTotalCol1 - total of sum depending which operators are selected in the comboBoxes.

Basically, if I type a number in textBox1 and textBox2, the sum is calculated based on the selected operator in comboBox1...and so on. The answer would be entered into textBoxTotalCol1.

The operator is between the textboxes above and below, so comboBox1 (in the middle of the textboxes) calculates textBox1 & 2, combo2 calculates textbox2 & 3 and combo3 is for textbox3 & 4. My intention is to enter enough cases to workout calculation based on the fact that multiplication and division come before plus and minus,

I want to use a switch to make it easier to do the sums based on which operator and how many were selected and which combination. It will calculate when a button is pressed. I have pasted my code below, the red text is where i have the error.

Code:
private void button1_Click(object sender, EventArgs e)
        {
            //parse all text boxes as doubles
            double tbx1 = double.Parse(textBox1.Text);
            double tbx2 = double.Parse(textBox2.Text);
            double tbx3 = double.Parse(textBox3.Text);
            double tbx4 = double.Parse(textBox4.Text);
            
            //create variable for the combo box
            string cbx1 = comboBox1.Text;
            string cbx2 = comboBox2.Text;
            string cbx3 = comboBox3.Text;


            double totalcol1 = double.Parse(textBoxTotalCol1.Text);


            switch (totalcol1)
            {
                case cbx1 = "*" & cbx2 = "" & cbx3 = "":
                    totalcol1 = tbx1 * tbx2;
                    break;

                case cbx1 = "*" & cbx2 = "*" & cbx3 = "":
                    totalcol1 = tbx1 * tbx2 * tbx3;
                    break;

            }
I read it based on my other switch that worked, that if cbx1 has multiply (x) selected and the other combos are empty, the answer totalcol1 is tbx1 * tbx2.

I think i am missing something.
__________________
"These pretzels are making me thirsty"
  #5 (permalink)  
Old December 19th, 2012, 07:55 PM
Rod Stephens's Avatar
Wrox Author
Points: 3,166, Level: 23
Points: 3,166, Level: 23 Points: 3,166, Level: 23 Points: 3,166, Level: 23
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2006
Location: , , .
Posts: 647
Thanks: 2
Thanked 96 Times in 95 Posts
Default

If you hover over the red variable, you should see an error message similar to this:

Quote:
Quote:
A switch expression or case label must be a bool, char, string, integral, enum, or corresponding nullable type.
Basically the idea is that you can use a switch statement when you want to explicitly compare a variable to a list of values. The problem with floating point values is that you can't reliably compare them by using ==.

For example, a variable that holds the value 1.23 might actually be stored in the computer as 1.2300000000000007 or something similar. When you check whether variable == 1.23, the answer is no even though for all practical purposes they are the same.

To compare floating point values such as float, double, and decimal, you need to see if the difference between the two values is very small. In this example, you could do something like:

Code:
if (Math.Abs(value - 1.23) < 0.0001) ...
Now if the value is very close to 1.23, the code can take action.

So that's why it didn't like you using a double for the switch variable.

Back to the switch statement...

If I understand what you're trying to do, I think you have to break this out into a series of if-then-else statements. A case statement cannot perform more than one test in the way you write: cbx1 = "*" & cbx2 = "" & cbx3 = "". I think you're going to need to do something like:

Code:
if (cbx1 = "*" & cbx2 = "" & cbx3 = "")
{
}
else if (cbx1 = "*" & cbx2 = "*" & cbx3 = "")
{
}
else if ...
Perhaps it would help if you think of the switch statement as a physical switch representing some variable's value. The switch can only be in a certain number of fixed positions and there's not a lot of flexibility about them.

For example, a typical real-world light switch can have only two positions: on and off. These switches are a bit fancier so you might have a Color switch that can have the values red, green, blue, or orange. It can't, however, have the value "red and blue" or "not orange."

If you need more flexibility than listing discrete values that the variable could have, then you probably need a series of if-them-else statements.
__________________
Rod

Rod Stephens, Microsoft MVP

Essential Algorithms: A Practical Approach to Computer Algorithms

(Please post reviews at Amazon or wherever you shop!)
  #6 (permalink)  
Old December 19th, 2012, 08:47 PM
Authorized User
Points: 130, Level: 2
Points: 130, Level: 2 Points: 130, Level: 2 Points: 130, Level: 2
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2012
Location: Australia
Posts: 32
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Thanks heaps Rod,

I started with Ifs at first which i am far more comfortable with but thought switch might be able to do it easier.
__________________
"These pretzels are making me thirsty"
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Lesson 18 Exercise 3 SamW BOOK: Stephens' C# Programming with Visual Studio 2010 24-Hour Trainer 6 December 11th, 2012 12:55 AM
Lesson 18 - TryIt sendalot BOOK: Stephens' C# Programming with Visual Studio 2010 24-Hour Trainer 1 June 6th, 2012 10:30 AM
Switch mamatharocks Biztalk 0 May 22nd, 2008 11:29 PM
if or switch Dink Classic ASP Databases 2 February 4th, 2007 04:00 AM
Switch rajuru Beginning PHP 2 February 9th, 2005 11:24 AM



All times are GMT -4. The time now is 06:09 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.