Error System.InvalidCastException

  • Thread starter Thread starter failedRookie
  • Start date Start date
F

failedRookie

Guest
Im a beginner who just started doing vb since lastweek and I have being stuck at this for hours.

I tried to create a object call Parts has four var,partname, numberofunits, costperunit and weight and fill in value by using another four textboxs.

However, i have no ideas why i constantly have an error call "System.InvalidCastException".

here my codes are

Public Class Main
Dim Parts As Parts
Dim Str As String
Private Sub Calculate_Click(sender As Object, e As EventArgs) Handles ComfirmInput.Click
Str = TextBox1.Text
Dim Part2 As New Parts(Str, Convert.ToInt32(TextBox2.Text), Convert.ToDouble(TextBox3.Text), Convert.ToDouble(TextBox4.Text))
End Sub
End Class

I only try to convert textbox2,3,and 4 to int and double but i dont know why it keeps trying to convert string in textbox1 to int as well. it supposes only try to convert value in textbox2,3,4 to int and double and leaves string alone so that i can create an object match Parts(String, int, int, double)


Here is my Parts class:

Public Class Parts
Dim PartName As String
Dim NumberOfUnits As Integer
Dim CostPerUnit As Double
Dim WeightPerUnit As Double
Public Sub New(ByVal PartName As Integer, ByVal NumberOfUnits As Integer, ByVal CostPerUnit As Double, ByVal WeightPerUnit As Double)
Me.PartName = PartName
Me.NumberOfUnits = NumberOfUnits
Me.WeightPerUnit = WeightPerUnit
Me.CostPerUnit = CostPerUnit

End Sub

End Class

Please Help!

Thank you!

Continue reading...
 
Back
Top