13 Replies - 4788 Views - Last Post: 22 March 2010 - 08:18 PM Rate Topic: -----

#1 mbrother64  Icon User is offline

  • D.I.C Head

Reputation: -9
  • View blog
  • Posts: 78
  • Joined: 30-August 09

Conversion from String to Integer not valid

Posted 19 March 2010 - 01:44 AM

Heres my code:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim product As String
        Dim sales As Integer
        product = InputBox("Please Enter Your Products Name: ")
        sales = InputBox("Please Enter Your Product Sales: ")
        
    End Sub


Its says conversion from String to Integer not valid for this line:
sales = InputBox("Please Enter Your Product Sales: ")

Also, when the first messagebox comes, how can I make it when you select "cancel", the programme then exits the sub routine and a second messagebox dosent come up?

This post has been edited by mbrother64: 19 March 2010 - 02:23 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Conversion from String to Integer not valid

#2 motcom  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 290
  • View blog
  • Posts: 1,371
  • Joined: 16-June 08

Re: Conversion from String to Integer not valid

Posted 19 March 2010 - 02:07 AM

Hi,

Your "last" is dimmed as an integer and not as string....
Was This Post Helpful? 0
  • +
  • -

#3 mbrother64  Icon User is offline

  • D.I.C Head

Reputation: -9
  • View blog
  • Posts: 78
  • Joined: 30-August 09

Re: Conversion from String to Integer not valid

Posted 19 March 2010 - 02:25 AM

I want it as an integer though sorry. They enter the product name in String and the sales in integer. And how about only having to click on the cancel button once?
Was This Post Helpful? 0
  • +
  • -

#4 motcom  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 290
  • View blog
  • Posts: 1,371
  • Joined: 16-June 08

Re: Conversion from String to Integer not valid

Posted 19 March 2010 - 02:30 AM

Well, first of all the input box does not do well with validation.... so i would not use it (i actually never use it) it's also vb legacy code, meaning it still comes from the old vb6 days.

Rather use normal textboxes on a form and do proper validation on the values the user types in...
Was This Post Helpful? 0
  • +
  • -

#5 mbrother64  Icon User is offline

  • D.I.C Head

Reputation: -9
  • View blog
  • Posts: 78
  • Joined: 30-August 09

Re: Conversion from String to Integer not valid

Posted 19 March 2010 - 03:17 AM

I'm afraid I have no choice in the matter. And the results are to appear in a listbox. But first things first. Whats wrong with my code?
Was This Post Helpful? 0
  • +
  • -

#6 Bort  Icon User is offline

  • Ill-informed Mongoloid
  • member icon

Reputation: 401
  • View blog
  • Posts: 2,965
  • Joined: 18-September 06

Re: Conversion from String to Integer not valid

Posted 19 March 2010 - 07:22 AM

Any input from an InputBox is considered a string. That is all that is wrong there. Something like this should work:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim product, salesstring As String
        Dim sales As Integer
        product = InputBox("Please Enter Your Products Name: ")
        salesstring = InputBox("Please Enter Your Product Sales: ")
        sales = CInt(salesstring)
        
    End Sub



However this doesn't give any verification to make sure 'salesstring' can be converted into an integer without throwing a pile of errors. I'd use a Try...Catch block in case some clever sod types a word into your sales InputBox.
Was This Post Helpful? 0
  • +
  • -

#7 ZRonZ  Icon User is offline

  • D.I.C Head

Reputation: 28
  • View blog
  • Posts: 206
  • Joined: 09-January 09

Re: Conversion from String to Integer not valid

Posted 19 March 2010 - 08:49 AM

View PostBort, on 19 March 2010 - 06:22 AM, said:

Any input from an InputBox is considered a string. That is all that is wrong there. Something like this should work:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim product, salesstring As String
        Dim sales As Integer
        product = InputBox("Please Enter Your Products Name: ")
        salesstring = InputBox("Please Enter Your Product Sales: ")
        sales = CInt(salesstring)
        
    End Sub



However this doesn't give any verification to make sure 'salesstring' can be converted into an integer without throwing a pile of errors. I'd use a Try...Catch block in case some clever sod types a word into your sales InputBox.


Which would be better for him to learn: CInt(salesstring) or integer.parse(salesstring)? Is there a difference?
Was This Post Helpful? 0
  • +
  • -

#8 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1639
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Conversion from String to Integer not valid

Posted 19 March 2010 - 09:23 AM

CInt is a legacy function left over from the VB6 days so stay away from that. Stick with the native .Net Libraries, here's an example

rivate Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    Dim product, salesstring As String
    Dim sales As Integer
    product = InputBox("Please Enter Your Products Name: ")
    salesstring = InputBox("Please Enter Your Product Sales: ")
    
    Dim validNum = Integer.TryParse(salesstring, sales)
    
    If Not validNum Then
    	MessageBox.Show("Please provide a valid number")
    End If                
End Sub



Now as long as the parse was successful your variable sales has your integer value, otherwise the MessageBox lets the user know they need to enter a valid value. Hope that helps :)
Was This Post Helpful? 1
  • +
  • -

#9 mbrother64  Icon User is offline

  • D.I.C Head

Reputation: -9
  • View blog
  • Posts: 78
  • Joined: 30-August 09

Re: Conversion from String to Integer not valid

Posted 21 March 2010 - 10:33 PM

When I click on my button and then click on the X it crashes, highlights the "For i = 1 To Subject" line and says "Conversion from String to Integer not valid". Other than that it does what I want it to. Whats wrong with that line?

Heres my code so far:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim Subject As String
        Subject = InputBox("How many")
        Dim i As Integer
        Dim num As Integer
        Dim word As String


        For i = 1 To Subject '< The line where it turns yellow and comes up with the error message
            word = (InputBox("word yo"))
            num = Val(InputBox("This many"))
            ListBox1.Items.Add(num & ControlChars.Tab & word)
            Select Case num
                Case Is >= 50
                    ListBox2.Items.Add(num & ControlChars.Tab & word)
                Case Is <= 50
                    ListBox3.Items.Add(num & ControlChars.Tab & word)
            End Select
        Next
    End Sub

This post has been edited by mbrother64: 21 March 2010 - 11:50 PM

Was This Post Helpful? 0
  • +
  • -

#10 muballitmitte  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 174
  • View blog
  • Posts: 470
  • Joined: 05-November 08

Re: Conversion from String to Integer not valid

Posted 21 March 2010 - 11:51 PM

This should fix that error. InputBox returns a string. Use CInt or any other conversion function to convert it to a int. I presume that you want to read a number.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim Subject As String
        Subject = InputBox("How many")
        Dim i As Integer
        Dim subb As Integer
        Dim num As Integer
        Dim word As String

        subb = CInt(Subject)

        For i = 1 To subb
            word = (InputBox("word yo"))
            num = Val(InputBox("This many"))
            ListBox1.Items.Add(num & ControlChars.Tab & word)
            Select Case num
                Case Is >= 50
                    ListBox2.Items.Add(num & ControlChars.Tab & word)
                Case Is <= 50
                    ListBox3.Items.Add(num & ControlChars.Tab & word)
            End Select
        Next
    End Sub

This post has been edited by muballitmitte: 21 March 2010 - 11:53 PM

Was This Post Helpful? 0
  • +
  • -

#11 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2257
  • View blog
  • Posts: 9,445
  • Joined: 29-May 08

Re: Conversion from String to Integer not valid

Posted 22 March 2010 - 01:02 AM

Have a look at moving away from using the InputBox as it very vb6, creating your own InputBox is really simple.
Was This Post Helpful? 0
  • +
  • -

#12 mbrother64  Icon User is offline

  • D.I.C Head

Reputation: -9
  • View blog
  • Posts: 78
  • Joined: 30-August 09

Re: Conversion from String to Integer not valid

Posted 22 March 2010 - 08:07 PM

I figured out what to do. All I had to do was write down the try function to bypass that stupid yellow error.

Try
'Main code written here
Catch ex As Exception
            Exit Sub
        End Try


Remember this for the next time some one asks this question.
Was This Post Helpful? 0
  • +
  • -

#13 mbrother64  Icon User is offline

  • D.I.C Head

Reputation: -9
  • View blog
  • Posts: 78
  • Joined: 30-August 09

Re: Conversion from String to Integer not valid

Posted 22 March 2010 - 08:10 PM

I figured out what to do. All I had to do was write down the try function to bypass that stupid yellow error.

Try
'Main code written here

Catch ex As Exception
            
Exit Sub
        
End Try


Remember this for the next time some one asks this question.

This post has been edited by mbrother64: 22 March 2010 - 08:11 PM

Was This Post Helpful? 0
  • +
  • -

#14 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1639
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Conversion from String to Integer not valid

Posted 22 March 2010 - 08:18 PM

that is horrible programming practices. Never ever just swallow an error because you dont feel like dealing with it. Always, and I mean always deal with errors that happen, dont just find a way to bypass them

Also, please dont create duplicate topics for the same issue :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1