6 Replies - 689 Views - Last Post: 19 July 2012 - 05:11 AM Rate Topic: -----

#1 thefifthhjohn  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 13-July 12

invalidcastexception was unhandled

Posted 19 July 2012 - 02:15 AM

I've had this error for today and I don't know how to correct it. Would someone please help me? My program would run but it would freeze for sometime

Dim welcomemessage As String = "QUANTITY DESCRIPTION TOTAL" & vbCr
        Dim totalprice As Integer
        If Checkbox1.Checked = True Then welcomemessage = TextBox2.Text & vbTab & Checkbox1.Text & (Checkbox1.Text * TextBox2.Text).ToString & vbCr
        totalprice = Checkbox1.Text * TextBox2.Text



Is This A Good Question/Topic? 0
  • +

Replies To: invalidcastexception was unhandled

#2 nK0de  Icon User is offline

  • Catch me As Exception
  • member icon

Reputation: 205
  • View blog
  • Posts: 823
  • Joined: 21-December 11

Re: invalidcastexception was unhandled

Posted 19 July 2012 - 02:21 AM

what are you trying to do here?

(Checkbox1.Text * TextBox2.Text).ToString

Was This Post Helpful? 1
  • +
  • -

#3 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1819
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: invalidcastexception was unhandled

Posted 19 July 2012 - 02:22 AM

Look at this line:
totalprice = Checkbox1.Text * TextBox2.Text

What are you doing here? Try to use parsing methods to cast strings to numeric value.
Also look you are taking text of the checkbox1 and do arithmetic with it?!
Was This Post Helpful? 1
  • +
  • -

#4 thefifthhjohn  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 13-July 12

Re: invalidcastexception was unhandled

Posted 19 July 2012 - 02:54 AM

View Postsmohd, on 19 July 2012 - 02:22 AM, said:

Look at this line:
totalprice = Checkbox1.Text * TextBox2.Text

What are you doing here? Try to use parsing methods to cast strings to numeric value.
Also look you are taking text of the checkbox1 and do arithmetic with it?!

I changed it into
Dim welcomemessage As String = "QUANTITY DESCRIPTION TOTAL" & vbCr
        Dim totalprice As Integer
        If Checkbox1.Checked = True Then welcomemessage = TextBox2.Text & vbTab & Checkbox1.Text & Str(Val(Checkbox1.Text) & Val(TextBox2.Text)) & vbCr
        totalprice = Str(Val(Checkbox1.Text) & Val(TextBox2.Text))
        MessageBox.Show(welcomemessage & totalprice)
but it seems that when I place a value on the check box the welcomemessage will change into what I've typed in
Was This Post Helpful? 0
  • +
  • -

#5 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1819
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: invalidcastexception was unhandled

Posted 19 July 2012 - 03:01 AM

That is what you are telling it to do here:
welcomemessage = TextBox2.Text & vbTab & Checkbox1.Text &

What do you want to do with your code?
Was This Post Helpful? 0
  • +
  • -

#6 thefifthhjohn  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 13-July 12

Re: invalidcastexception was unhandled

Posted 19 July 2012 - 04:24 AM

View Postsmohd, on 19 July 2012 - 03:01 AM, said:

That is what you are telling it to do here:
welcomemessage = TextBox2.Text & vbTab & Checkbox1.Text &

What do you want to do with your code?

what should I do? Remove it or?
Was This Post Helpful? 0
  • +
  • -

#7 DarenR  Icon User is offline

  • D.I.C Lover

Reputation: 485
  • View blog
  • Posts: 3,250
  • Joined: 12-January 10

Re: invalidcastexception was unhandled

Posted 19 July 2012 - 05:11 AM

are the 2 other text boxes supposed to be doubles? strings? ints?

total price is int so you have to make the other 2 ints as well and to put it ina message you would put it as totalprice.tostring
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1