Cannot implicitly convert type "string" to "int" - Page 2
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 2 FirstFirst 12
Results 16 to 17 of 17

Thread: Cannot implicitly convert type "string" to "int"

  1. #16
    Join Date
    Jan 2010
    Posts
    1,099

    Re: Cannot implicitly convert type "string" to "int"

    Aah, I missed that bit.

    Look what u have:
    Code:
    01:         int guess;
    02:         int.TryParse(Console.ReadLine(), out guess);
                
                //...
    
                // then later, in do-while:
    
    03:                guess = Console.ReadLine();
    Line 01 declares the guess to be an integer.

    Line 02 is equivalent to:

    string temp = Console.ReadLine();
    int.TryParse(temp, out guess);
    ReadLine returns a string, and try parse expects a string as its first parameter (this is the string you want to parse and convert to an integer). This is why, instead of the temp variable used here, you can put in the ReadLine() call directly - the method returns a string, and this result is passed as the parameter. Guess is passed by reference via the out keyword, which enables the TryParse() method to change the value of the variable internally, in a way that affects your original guess variable (that is, the method doesn't make it's own copy).
    When TryParse() is done, guess will contain the appropriate value.

    All that means that line 03 is completely redundant, aside from the fact that it won't compile. You can safely delete it. Again, the reason the compiler complains is because ReadLine() returns a string, but the guess variable now accepts only integers, as you yourself have declared in line 01.

  2. #17
    Join Date
    Jun 2011
    Posts
    33

    Re: Cannot implicitly convert type "string" to "int"

    Ok, so I finally got it to work, thanks to Jim's help. The code compiles without error and runs fine. Now, I just need to do some cleanup.

    Here's the completed code. I added a statement where if you enter in "0", it exits the program and I also removed the goto statement. I also increased the number to 500,000, just to make things difficult for the unlucky user who decides to play this insane number game.

    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                Console.WriteLine("You must guess a number from 1 to 50000. If you guess the number correctly, you win.");
                int number;
                int guess;
    
                int.TryParse(Console.ReadLine(), out guess);
                Random rand;
                rand = new Random();
    
                number = rand.Next(1, 50000);
                do
                {
                    string guess_str = Console.ReadLine();
    
                    if (guess == number)
                    {
                        Console.WriteLine("You've won.");
                    }
                    else
                    {
                        Console.WriteLine("Sorry, guess again.");
                    }
                } while (guess != number);
            }
        }
    }
    Thanks for everyone's help.

Page 2 of 2 FirstFirst 12

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center