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?
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.
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.