Home Forums C# Programming Cannot implicitly convert type int to string? why?

Viewing 1 reply thread
  • Author
    Posts
    • #2114
      CRavi
      Participant

      Hello i am still new to C#, has been a month now studying this from VS 2008 C# but now i am concentrating more on code than gui interface, i am looking right now if and else and switch, i decied to use a switch since it is new to me, it keeps giving me that error “implicitly convert type into string, for all 4 cases i made, why? all of the code is below:


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

      namespace ConsoleApplication1{
      class Program{

      static void Main(string[] args){
      string firstn;
      string secondn;
      string thirdn;
      int first;
      int second;
      int third;
      string sign;
      int signn;
      begin:
      switch (thirdn){
      case 1:
      Console.WriteLine("Please enter the first number:");
      firstn = Console.ReadLine();
      first = int.Parse(firstn);
      Console.WriteLine("Please enter if you would like to Add, subtract, multiply, or divide?");
      sign = Console.ReadLine();
      signn = Int32.Parse(sign);
      Console.WriteLine("Please enter the number to add with " + first);
      secondn = Console.ReadLine();
      second = int.Parse(secondn);
      third = first + second;
      Console.WriteLine(third);
      Console.Read();
      break;
      case 2:
      Console.WriteLine("Please enter the first number:");
      firstn = Console.ReadLine();
      first = Int32.Parse(firstn);
      Console.WriteLine("Please enter if you would like to Add, subtract, multiply, or divide?");
      sign = Console.ReadLine();
      Console.WriteLine("Please enter the second number to subtract with " + first);
      secondn = Console.ReadLine();
      second = Int32.Parse(secondn);
      third = first - second;
      Console.WriteLine(third);
      Console.Read();
      break;
      case 3:
      Console.WriteLine("Please enter the first number:");
      firstn = Console.ReadLine();
      first = Int32.Parse(firstn);
      Console.WriteLine("Please enter if you would like to Add, subtract, multiply, or divide?");
      sign = Console.ReadLine();
      Console.WriteLine("Please enter the second number to multiply with " + first);
      secondn = Console.ReadLine();
      second = Int32.Parse(secondn);
      third = first * second;
      Console.WriteLine(third);
      Console.Read();
      break;
      case 4:
      Console.WriteLine("Please enter the first number:");
      firstn = Console.ReadLine();
      first = Int32.Parse(firstn);
      Console.WriteLine("Please enter if you would like to Add, subtract, multiply, or divide?");
      sign = Console.ReadLine();
      Console.WriteLine("Please enter the second number to divide with " + first);
      secondn = Console.ReadLine();
      second = Int32.Parse(secondn);
      third = first / second;
      Console.WriteLine(third);
      Console.Read();
      break;
      default:
      Console.WriteLine("Please enter a number");
      }

      decide:
      Console.Write("Type "continue" to go on or "quit" to stop: ");

      firstn = Console.ReadLine();

      // switch with string type

      switch (firstn){

      case "continue":
      goto begin;
      case "quit":
      Console.WriteLine("Good Bye");
      Console.Read();
      break;
      default:
      Console.WriteLine("Your input {0} is incorrect.", firstn);
      goto decide;

      }

    • #3405
      msaqib
      Participant

      HelloYou are trying to use string type variable in your switch statement which is expecting an int type variable. Here is how you are doing

      string thirdn;
      switch (thirdn){
      case 1:
      //code
      case 2:
      //code
      }

      Now the problem is you can not use string type variable in switch statement. You could use it like


      string thirdn;
      switch (thirdn){
      case "1":
      //code
      case "2":
      //code
      }

      Or you could have use int type variable like


      int thirdn;
      switch (thirdn){
      case 1:
      //code
      case 2:
      //code
      }
Viewing 1 reply thread
  • The forum ‘C# Programming’ is closed to new topics and replies.