-2

I started programming just about 2 weeks ago and my friend said I could ask you guys for help if I need any.

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

    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                int x;
                string s;
                do
                {
                    Console.WriteLine("input a number in the range of 0 -100");
                    s = Console.ReadLine();
                    x = s.ToInt32();
                }
                while (x < 0 || x > 100);
                Console.WriteLine("ok you have chosen");
                Console.ReadKey();
                }
        }
    }

My first time running into that x=s.ToInt32 (I've read that int is supposed to contain a number and not a string or letters..) and the error:

'string' does not contain a definition for 'ToInt32' and no extension method 'ToInt32' 
accepting a first argument of type 'string' could be found (are you missing a using 
directive or an assembly reference?)
C:\Users\user\Documents\Visual Studio11\Projects\ConsoleApplication1\ConsoleApplication1\Program.cs 19  23  ConsoleApplication1
Cyral
  • 13,999
  • 6
  • 50
  • 90

4 Answers4

5

You can use int.TryParse to convert a string to an int

s = Console.ReadLine();
int.TryParse(s, out x);

or Convert.ToInt32

x = Convert.ToInt32(s)

The reason you are getting the error you are getting, and what it means is that there is no method called ToInt32() in the string class.

TryParse() also gives a boolean value showing if the conversion was successful (ie, the variable was a string, that was a number).

However you can create that method that dosen't exist, using an extension method. The this keyword means it references the object that is calling it.

public static int ToInt32(this string s)
{
       return Convert.ToInt32(s);
}

All of these will do pretty much what you need, NetStarter's answer does provide a link to the differences, which I found pretty interesting. Also these two tutorials:

Good luck and welcome to Stack Overflow!

Community
  • 1
  • 1
Cyral
  • 13,999
  • 6
  • 50
  • 90
3

Try using either Convert.ToInt32() or Int32.TryParse(). Either will accept a string and convert it to an Int32 instance. Convert.ToInt32 takes a string (among its many overloads) and returns the corresponding integer value if a conversion is possible; Int32.TryParse() takes a string and an output variable (the out x in @Cyral's answer) and returns a boolean which indicates whether or not the conversion was successful.

Also, yes, we are generally happy to help here but you are also expected to try for yourself first and to state your problem clearly. Always try to provide a MWE (Minimal Working Example) that illustrates the problem you are having. Posting almost a wall of code when a few lines will illustrate the issue you are having is generally frowned upon. It might not always be easy especially if you are still learning but in doing so you may very well discover the solution yourself which will almost certainly help you learn much better than having the answer spoon-fed.

Community
  • 1
  • 1
user
  • 6,897
  • 8
  • 43
  • 79
2

Yes there is no ToInt32 method for the string class, and correctly the compiler refuses to work with this.

The correct approach is to use Int32.TryParse to be sure that the input is effectively a number

....
int x;
string s;
do
{
    Console.WriteLine("input a number in the range of 0 -100");
    s = Console.ReadLine();
    if(!Int32.TryParse(s, out x)
        x = -1; // Not a integer, force another loop
}
while (x < 0 || x > 100);
...

Int32.TryParse will try to convert the string passed in an integer number. If it succeeds you have your number in the x variable passed as an out parameter while the method returns true. This method is preferable to Int32.Parse or Convert.ToInt32 because it doesn't throw exceptions if the input string is not a number and the exceptions are pretty expensive for the performance of any application.

Steve
  • 213,761
  • 22
  • 232
  • 286
1

Use int.TryParse

string text2 = "10000";
int num2;
if (int.TryParse(text2, out num2))
{
    // It was assigned.
}

//
// Display both results.
//
Console.WriteLine(num1);
Console.WriteLine(num2);

Or Convert.ToInt32

result = Convert.ToInt32("String");

What is the difference in both ?

Read it here -> What's the difference between Convert.ToInt32 and Int32.Parse?

Community
  • 1
  • 1
NetStarter
  • 3,189
  • 7
  • 37
  • 48
  • @Cyral thanks, I had actually answered a question for the same so thought of providing a link so OP will have some clarity. – NetStarter Jun 01 '13 at 13:29