none
Error System.InvalidCastException RRS feed

  • Question

  • 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!


       

     

    Wednesday, March 8, 2017 8:08 AM

All replies

  •     Dim Parts As Parts 

     

    Before going any further, I strongly suggest that you change your code so
    that you aren't giving an object the same name as a class. This can
    confuse human readers of the code, and in some cases can confuse a
    compiler.

    - Wayne

    Wednesday, March 8, 2017 8:24 AM
  • However, i have no ideas why i constantly have an error call "System.InvalidCastException".

    i dont know why it keeps trying to convert string in textbox1 to int as well.


    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)


    Well look at your class:

        Dim PartName As String    
        
        Public Sub New(ByVal PartName As Integer, ....
        
    - Wayne

    • Proposed as answer by Cor Ligthert Wednesday, March 8, 2017 10:35 AM
    Wednesday, March 8, 2017 8:32 AM
  • Thank you very much for asking my...ridiculous question!

     I reckon i really need to go outside and breath some fresh air!

    Wednesday, March 8, 2017 8:45 AM
  • An InvalidCastException is thrown when cast from one type to another type is not supported. In some reference type conversions , the compiler cannot determine whether a cast will be valid. It is because of the source type cannot be converted to the destination type , so the cast does not succeed.It is important to note that this exception is caused by programming error and should not be handled in a try/catch block; instead, the cause of the exception should be eliminated. C# provides the "is" operator to enable you to test for compatibility before actually performing a cast operation . It will checks if the runtime type of an expression result is compatible with a given type.


    Wednesday, January 15, 2020 5:38 AM