Computer Forums

Member Login

Remember Me? Sign Up! | Forgot Password
 
Slogan
 
Closed Thread
Old 12-14-2004, 10:12 AM   #1 (permalink)
 
Master Techie

Join Date: Mar 2004

Posts: 2,069

rookie1010

Default select(VB) / switch(C)

Hi
why do we have the case structure with a select in Visual Basic and switch in C?
rookie1010 is offline  
Old 12-15-2004, 10:28 AM   #2 (permalink)
 
Ultra Techie

Join Date: Sep 2003

Location: Bamberg, Germany

Posts: 549

Iron_Cross

Send a message via ICQ to Iron_Cross Send a message via MSN to Iron_Cross Send a message via Yahoo to Iron_Cross
Default

Why use it? Because it simplifies a long if-else if-else if-else statement.

(I'm talking about a C# statement, but you'll get the idea)
It's easier for me to do this:
Code:
switch(someString)
{
    case "Case 1":
         //some statements if someString == Case 1
         break;
    case "Case 2":
         //some statemsnt if someString == Case 2
         break;
    default:
          // some statements if someString doesn't match anything else
          break;
}
Over doing something like this:
Code:
if(someString == "Case 1")
{
    // some statements
}else if(someString == "Case 2")
{
    //some statements
}else
{
   //some statements
}
Now that's just simple example where it didn't really matter, but imagine a situation where you had to check 50 different values of someString, a switch statement would be much faster and easier to execute.
__________________

See today\'s Penny-Arcade!(May contain foul lanuage)
Pain is weakness leaving the body.

PM Me for my MSN
Iron_Cross is offline  
Old 12-15-2004, 12:10 PM   #3 (permalink)
 
Master Techie

Join Date: Mar 2004

Posts: 2,069

rookie1010

Default

what i meant is why do we have different keywords select in VB and switch in C
rookie1010 is offline  
Old 12-15-2004, 08:42 PM   #4 (permalink)
 
Monster Techie

Join Date: Sep 2004

Posts: 1,447

Alex81388

Send a message via AIM to Alex81388
Default

Simply because different people made different languages, whats the point of a new laungage if you have one just like it?

Also, the big thing a case statement is good for is when you need to add up a multitude of things, like say:

$500 = plasma tv
$400 = computer
$300 = a bike
$200 = 20" tv
$100 = pair of shoes

If someone has a value of 500, they get All of that, by placing it in a case statement, starting at 100, through 500 at the end, if they have 400, they get all the values, and after the variable does not equal 500, they hit the statement 'break' (which the guys used above) to exit the whole switch. This is especially useful, say if you had things rangin from $100,000 to $50, where the prizes were cumulative.
__________________
Sig removed due to foul language. Please read the rules regarding the allowable content of sigs before reposting your COMPLIANT sig.
~Trotter
4/21/06
Alex81388 is offline  
 
Closed Thread

Thread Tools
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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On