Results 1 to 3 of 3

Thread: Converting String from InputBox to Integer

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2013
    Posts
    1

    Question Converting String from InputBox to Integer

    I am currently trying to use the Integer Try Parse Method to convert strings entered via an inputbox so that they can be put into an array. However, when I try the following code:
    Dim Count As Integer

    Code:
           For Count = 0 To domestic.Length - 1
                domestic(Count) = Integer.TryParse(InputBox("Keep entering the sales for each month.  If nothing typed, will be interpeted as zero", "New data entry"))
    
            Next Count
    I get the error message: Overload resolution failed because no accessible 'TryParse' accepts this number of arguments.
    How do I fix this?

  2. #2
    Fanatic Member
    Join Date
    Mar 2009
    Posts
    739

    Re: Converting String from InputBox to Integer

    Tryparse does NOT return a number. It returns a boolean.

    It takes two args. As well as the string You have to pass a numeric variable in with the arguments which will be used to hold the result of the call.

    The reason you get a boolean as the return value is because you will always get a real number in the result arg. For example you will always get zero if the string wasnt numeric so the boolean needs to be used to tell you if the number is good or bad.

    You need to do the tryparse first. Check the boolean to make sure its true and then assign the numeric arg to your array.
    Last edited by IanS; May 14th, 2013 at 04:33 AM.

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Converting String from InputBox to Integer

    What IanS says is correct. Think about it. The point of TryParse is to TRY to parse the text into a number. If your code worked, what exactly would you expect to be put into your array if that attempt failed?

    You can find examples of the use of TryParse all over the web and this forum too. Barely a day goes by that at least one question doesn't involve TryParse in the answer so a search will turn up loads of examples.

    You shouldn't even need that though. Whenever you are using a new type or member, especially if it doesn't work the way you expect, the very first thing you should do is read the documentation for that type or member. The Help menu is not there for decoration. The documentation will explain what the type or member does and often provide examples so that is quite often all you need.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

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



Click Here to Expand Forum to Full Width