Jump to content

Welcome to Geeks to Go - Register now for FREE

Geeks To Go is a helpful hub, where thousands of volunteer geeks quickly serve friendly answers and support. Check out the forums and get free advice from the experts. Register now to gain access to all of our features, it's FREE and only takes one minute. Once registered and logged in, you will be able to create topics, post replies to existing threads, give reputation to your fellow members, get your own private messenger, post status updates, manage your profile and so much more.

Create Account How it Works
Photo

C# Case Break


  • Please log in to reply

#1
DevonDaDude

DevonDaDude

    New Member

  • Member
  • Pip
  • 5 posts
I need someone to explain to me

Console.Write("Please enter a number between 1 and 3: ");
myInput = Console.ReadLine();
myInt = Int32.Parse(myInput);




First, which variable is it assign to? I see myInput and myInt.
I really do not understand why he declared 2 variable at the top, could someone please clarify this for me, please? Thank You

using System;
using System.Collections.Generic;
using System.Text;

namespace caseSwitch
{
class Program
{
static void Main(string[] args)
{

//Declare Variables
string myInput;
int myInt;

//Label begin
begin:

Console.Write("Please enter a number between 1 and 3: ");
myInput = Console.ReadLine();
myInt = Int32.Parse(myInput);

//Switch with integer type
switch (myInt)
{
case 1:
Console.WriteLine("Your number is {0}.", myInt);
break;
case 2:
Console.WriteLine("Your number is {0}.", myInt);
break;
case 3:
Console.WriteLine("Your number is {0}.", myInt);
break;
default:
Console.WriteLine("Your number is {0} is not between 1 and 3.", myInt);
break;
}


//label decide
decide:

Console.Write("Type \"contine\" to go on or \"quit\" to stop: ");
myInput = Console.ReadLine();

//swtich with string type
switch (myInput.ToLower())
{
case "continue":
//goto label begin
goto begin;
case "quit":
Console.WriteLine("Good Bye!!");
break;
default:
Console.WriteLine("Your input {0} is incorrect.", myInput);
goto decide;

}


}
}
}


  • 0

Advertisements


#2
InsaneKaos

InsaneKaos

    Member

  • Member
  • PipPipPip
  • 120 posts

Console.Write("Please enter a number between 1 and 3: ");
myInput = Console.ReadLine();


myInput is now the string 1, 2 or 3 (123)

myInt = Int32.Parse(myInput);

This converts myInput to Int32, so that the variable myInt contains the number 1, 2 or 3 as an integer.

This is needed for the switch-function. (//Switch with integer type)

mfg, Kaos
  • 0

#3
DevonDaDude

DevonDaDude

    New Member

  • Topic Starter
  • Member
  • Pip
  • 5 posts
Thank You, InsaneChaos :).
Why not use myInt as the assign variable? instead of using myInput as a string and converting it into a int? instead of using Int.Parse32, can we use Convert.ToInt32()?

Edited by DevonDaDude, 24 November 2009 - 08:45 PM.

  • 0

#4
Microsd

Microsd

    Member

  • Member
  • PipPip
  • 22 posts
You can use this:
myInput = Convert.ToInt32(Console.ReadLine());

  • 0

#5
DevonDaDude

DevonDaDude

    New Member

  • Topic Starter
  • Member
  • Pip
  • 5 posts
Thanks, everyone(InsaneKaos and MircoSd) :).. I've emailed my teacher as to why he did the following.


Console.Write("Please enter a number between 1 and 3: ");
myInput = Console.ReadLine();
myInt = Int32.Parse(myInput);


instead of

Console.Write("Please enter a number between 1 and 3: ");
myInt = Console.Int32(Console.ReadLine());


Int32.Parse does the something as Convert. Both convert string value into integer with a little difference. The difference between the two is in how they handle null values. Convert will do all it can in it's power to change what you passed in to the thing you ultimately asked for, so it will check for a null and upon finding one will simply return 0. Int32.Parse on the other hand expects a valid integer value from the caller and will throw and exception when you pass in null.


Hopefully, if there's someone like me, got to know why the teacher did it this way, hopefully this will answer your questions as to why.
  • 0






Similar Topics

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

As Featured On:

Microsoft Yahoo BBC MSN PC Magazine Washington Post HP