Need help with int.TryParse()

  • (2 Pages)
  • +
  • 1
  • 2

26 Replies - 947 Views - Last Post: 19 November 2015 - 01:06 PM Rate Topic: -----

#1 Hydroify  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 127
  • Joined: 23-July 15

Need help with int.TryParse()

Posted 18 November 2015 - 02:19 PM

I am having trouble understanding int.TryParse() all together.

This post has no code because all I am asking is some tips and examples using the int.TryParse().

Links are well appreciated or just an explanation.

Thanks in advance
Is This A Good Question/Topic? 0
  • +

Replies To: Need help with int.TryParse()

#2 andrewsw  Icon User is online

  • the case is sol-ved
  • member icon

Reputation: 6382
  • View blog
  • Posts: 25,781
  • Joined: 12-December 12

Re: Need help with int.TryParse()

Posted 18 November 2015 - 02:25 PM

http://www.dotnetperls.com/int-parse

the docs

Did you not find these, or similar?

This post has been edited by andrewsw: 18 November 2015 - 02:27 PM

Was This Post Helpful? 1
  • +
  • -

#3 zerocool1  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 10
  • View blog
  • Posts: 54
  • Joined: 31-August 15

Re: Need help with int.TryParse()

Posted 18 November 2015 - 02:47 PM

Try Parse is just going to to try to parse it to a integer. Here is some example code you can run to get the idea. So basically if you were trying to test converting a string to an integer you might use something like this.

public class Program
{
    public static void Main(string[] args)
    {
        
        string s = "10"; //Try putting different ints and strings in and see the different results
        int i = 0; 
        
       bool isSuccess = Int32.TryParse(s, out i);
        
        if(isSuccess)
            
        {
         
            Console.WriteLine("Successfully converted string into the int value of " + i);
            // Do whatever when succeeds
        }
        
        else 
        {
            Console.WriteLine("Failed Convert the string to an int"); // Do whatever when fails 
        }
    }
}


Was This Post Helpful? 1
  • +
  • -

#4 Hydroify  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 127
  • Joined: 23-July 15

Re: Need help with int.TryParse()

Posted 18 November 2015 - 04:01 PM

View Postandrewsw, on 18 November 2015 - 02:25 PM, said:

http://www.dotnetperls.com/int-parse

the docs

Did you not find these, or similar?


I found some of them just wanted to see whatvothers were out there
Was This Post Helpful? 0
  • +
  • -

#5 Hydroify  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 127
  • Joined: 23-July 15

Re: Need help with int.TryParse()

Posted 19 November 2015 - 07:56 AM

Could someone show me on this piece of code? I got lots more but just would like to see it on a piece of the code.

        private static int Month(int month)
        {
           month = Convert.ToInt32(Console.ReadLine());
            
            while (month < MIN_MONTH || month > MAX_MONTH)
            {
                    Console.Write("Invalid value. Please enter month again >> ");
                    month = Convert.ToInt32(Console.ReadLine());
             }
            return month;
        }

Was This Post Helpful? 0
  • +
  • -

#6 andrewsw  Icon User is online

  • the case is sol-ved
  • member icon

Reputation: 6382
  • View blog
  • Posts: 25,781
  • Joined: 12-December 12

Re: Need help with int.TryParse()

Posted 19 November 2015 - 08:17 AM

If you've understood the references for TryParse you should be able to at least make an attempt to use it.
Was This Post Helpful? 0
  • +
  • -

#7 Hydroify  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 127
  • Joined: 23-July 15

Re: Need help with int.TryParse()

Posted 19 November 2015 - 08:56 AM

I done that just when i change the int to string it does it opposite. It throws error to a number but not a letter

I know it should be int.TryParse(string, out int); but not sure if i should use it with a while loop or just if statement
Was This Post Helpful? 0
  • +
  • -

#8 Hydroify  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 127
  • Joined: 23-July 15

Re: Need help with int.TryParse()

Posted 19 November 2015 - 09:05 AM

        private static int Month(int month)
        {
            string month2 = Console.ReadLine();
            // month = Convert.ToInt32(Console.ReadLine());
            
            //while (month < MIN_MONTH || month > MAX_MONTH)
            //{
              //      Console.Write("Invalid value. Please enter month again >> ");
                //    month = Convert.ToInt32(Console.ReadLine());
             //}
            if(int.TryParse(month2, out month))
            {
                Console.Write("Invalid value please try again >> ");
                Console.ReadLine();
            }
            return month;
        }



That is what i came up with just is not working for me like program runs just it throws when i type in a number in and not a letter
Was This Post Helpful? 0
  • +
  • -

#9 andrewsw  Icon User is online

  • the case is sol-ved
  • member icon

Reputation: 6382
  • View blog
  • Posts: 25,781
  • Joined: 12-December 12

Re: Need help with int.TryParse()

Posted 19 November 2015 - 09:10 AM

Quote

I done that just when i change the int to string it does it opposite. It throws error to a number but not a letter

This isn't a clear statement, particularly without any code.
private static int Month(int month)
{
   month = Convert.ToInt32(Console.ReadLine());


There is no point in passing an integer value 'month' to your method, as you immediately (attempt to) overwrite it. So:
private static int Month()
{
    int month = 0;


You will be replacing Convert.ToInt32 with TryParse, so you need to get rid of those lines. You also need to first read the string that the user enters:
private static int Month()
{
    int month = 0;
    string input = Console.ReadLine();


Now you can make use of TryParse to attempt to obtain an integer from 'input'.

Added: You've just posted this:

View PostHydroify, on 19 November 2015 - 04:05 PM, said:

..like program runs just it throws when i type in a number in and not a letter

This isn't clear either. Are you saying it throws an error? What error, and with what input?

This post has been edited by andrewsw: 19 November 2015 - 09:10 AM

Was This Post Helpful? 0
  • +
  • -

#10 Hydroify  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 127
  • Joined: 23-July 15

Re: Need help with int.TryParse()

Posted 19 November 2015 - 09:16 AM

The message in the console app. stating Invalid input... when I type a number in but not a letter

Sorry I meant the error it gives you when you type in the wrong value. "Invalid value. Please enter a month >> "

not an compile error or anything like that. Sorry for not being the clearest

This post has been edited by Hydroify: 19 November 2015 - 09:18 AM

Was This Post Helpful? 0
  • +
  • -

#11 andrewsw  Icon User is online

  • the case is sol-ved
  • member icon

Reputation: 6382
  • View blog
  • Posts: 25,781
  • Joined: 12-December 12

Re: Need help with int.TryParse()

Posted 19 November 2015 - 09:17 AM

me said:

There is no point in passing an integer value 'month' to your method, as you immediately (attempt to) overwrite it.

That is, unless you are attempting to use an out parameter yourself? In which case, you wouldn't return anything.
Was This Post Helpful? 0
  • +
  • -

#12 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 5899
  • View blog
  • Posts: 20,141
  • Joined: 05-May 12

Re: Need help with int.TryParse()

Posted 19 November 2015 - 09:18 AM

Read the documentation. TryParse() returns true if a string was successfully parsed into a number.
Was This Post Helpful? 1
  • +
  • -

#13 andrewsw  Icon User is online

  • the case is sol-ved
  • member icon

Reputation: 6382
  • View blog
  • Posts: 25,781
  • Joined: 12-December 12

Re: Need help with int.TryParse()

Posted 19 November 2015 - 09:20 AM

View PostHydroify, on 19 November 2015 - 04:16 PM, said:

The message in the console app. stating Invalid input... when I type a number in but not a letter

Sorry I meant the error it gives you when you type in the wrong value. "Invalid value. Please enter a month >> "

not an compile error or anything like that. Sorry for not being the clearest

You mean it produces your own message, "Invalid value please try again >> "? That's because your logic is upside-down; you produce this message if the attempt to obtain an integer IS successful.
Was This Post Helpful? 1
  • +
  • -

#14 Hydroify  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 127
  • Joined: 23-July 15

Re: Need help with int.TryParse()

Posted 19 November 2015 - 09:26 AM

WOuld i use something like this?

    {
	// See if we can parse the 'text' string.
	// If we can't, TryParse will return false.
	// Note the "out" keyword in TryParse.
	string text1 = "x";
	int num1;
	bool res = int.TryParse(text1, out num1);
	if (res == false)
	{
	    // String is not a number.
	}

Pulled of site you stated earlier

Alright It works now. Thanks andrewsw for taking your time to explain thoroughly.

This post has been edited by Hydroify: 19 November 2015 - 09:24 AM

Was This Post Helpful? 0
  • +
  • -

#15 andrewsw  Icon User is online

  • the case is sol-ved
  • member icon

Reputation: 6382
  • View blog
  • Posts: 25,781
  • Joined: 12-December 12

Re: Need help with int.TryParse()

Posted 19 November 2015 - 09:33 AM

No problem.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2