I have 2 integers x and y, now I want to read them from the user and the syntax kick me down, I guess u can only read string from the user according to C# syntax rule, I have tried many combinations but none of them are working, so now I want to know how to static cast string to integer coz I have declared those integers as strings and I have to make some calculations which I cant with strings

Lastly I came up with something like string x, y so I wana type cast them to integers so that I can be able to make those calculation..... any help would be highly appreciated.

Note: Im a novice to C#, and am trying to learn it on my own.... And please dont refer me to some other threads or sites, only one line of code would be sufficient

Hi there Traicey :)

Give this a try:

string x = "";    
int parsex = Int32.Parse(x);

Or

string x = "";
int ConvertedIx = Convert.ToInt32(x);

Hope this helps!

I have something like this string x = Console.ReadLine(); and again I have tried int j = int32.Parse(x) and now I have this code int j = Convert.ToInt32(x) so now the problem is when Im trying to execute this statement result = x - y; I got this error '-' cannot be applied to operand of type string and string, And believe me I dont know why its not working really, seems as if it is ignoring all the conversion and focus on the declaration..... 1st I tried to declare my variables as integers and they told me I cant read integer from the user and now I try to type cast it but hey

Where have you cast the variable Y?

commented: Quite helpful +1

Hey.... I got it right man, I was just being stubborn, adopting donkey mentality, I guess I was too tired, Thanx a lot hey :)

you are welcome.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.