none
Cannot implicitly convert type 'string' to 'int' RRS feed

  • Question

  • Trying to learn C# and as such i have deicded to re-create and old game i used to play called "Hampsters"

    My problem is this:

    "testing.cs(15,23): error CS0029: Cannot implicitly convert type 'string' to 'int'"

    Code Below. Can someone please explain the answer, without giving me the answer if possible? IE Just tell me what i am doing wrong, dont give me the correct code.

    Kindest regards,

    C.

    using System;
    
    	namespace testing
    	{
    
    		class Program
    		{	
    			
    			static void Main(string[] args)
    			{
    				
    				int hampstersBought;
    				
    				Console.WriteLine("How many hampsters would you like? : ");
    				hampstersBought = Console.ReadLine();
    				Console.Write(hampstersBought);
    								
    			}				
    		}
    	}



    • Edited by Chris LTQ Thursday, April 2, 2020 4:09 PM
    Thursday, April 2, 2020 4:03 PM

Answers

All replies

  • Well, first of all

    https://docs.microsoft.com/en-us/dotnet/api/system.console.readline?view=netframework-4.8

    (Look at its output type).

    Normally, you need to explicitly convert the value to int using (int) boxing or alternatively Convert.ToInt or any other method of converting string value to integer.


    For every expert, there is an equal and opposite expert. - Becker's Law


    My blog


    My TechNet articles

    • Marked as answer by Chris LTQ Thursday, April 2, 2020 4:31 PM
    Thursday, April 2, 2020 4:16 PM
    Moderator
  • Hello,

    Never assume user input is correct type.

    For older versions of C#

    int hampstersBought;
    string userInput = "20";
    if (int.TryParse(userInput,out hampstersBought) )
    {
        // valid int
           
    }
    else
    {
        // not valid int
    }

    4.7 Framework

    string userInput = "20";
    if (int.TryParse(userInput,out var hampstersBought) )
    {
        // valid int
        Console.WriteLine($"{hampstersBought} is valid");
           
    }
    else
    {
        Console.WriteLine($"{userInput} is not an int");
    }


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Thursday, April 2, 2020 4:31 PM
    Moderator
  • Awesome! Thanks for the nudge!

    So I worked it out. Solution Below! Didnt realise the thing about "Reeadline" defautling to "string"

    Star! thankyou!

    using System;
    
    	namespace testing
    	{
    
    		class Program
    		{	
    			
    			static void Main(string[] args)
    			{
    				
    				int hampstersBought;
    				
    				Console.Write("How many hampsters would you like? : ");
    				hampstersBought = Convert.ToInt32(Console.ReadLine());
    				Console.Write(hampstersBought);
    								
    			}				
    		}
    	}


    Thursday, April 2, 2020 4:31 PM
  • Chris,

    Use int.TryParse() instead of Convert.ToInt32() --- looks like you skipped or didn't see Karen's reply.

    The reason is: if something other than a number is entered by the user, the Convert will crash (throw an exception), whereas the TryParse() will gracefully set your hampstersBought to a zero and return a false.


    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    Friday, April 3, 2020 5:01 AM
    Moderator