错误System.InvalidCastException [英] Error System.InvalidCastException

查看:74
本文介绍了错误System.InvalidCastException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是刚开始做vb的初学者自上周以来,我已经在这里停留了几个小时.

我尝试创建对象调用部件有四个变量,零件名称,单位数量,成本单位和重量,并使用另外四个文本框填写值.

但是,我不知道为什么我会不断错误调用" System.InvalidCastException ".

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

此处是我的代码

公共班级主要
   将零件变暗为零件
    Dim Str As String
      Private Sub Calculate_Click(作为对象发送,作为EventArgs发送)处理ComfirmInput.Click
        Str = TextBox1.Text
       将Part2设为新零件(Str,Convert.ToInt32(TextBox2.Text),Convert.ToDouble(TextBox3.Text),Convert.ToDouble(TextBox4.Text))
   结束子
结束班

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

我只尝试将textbox2、3和4转换为int和double,但我不知道为什么它一直尝试将textbox1中的字符串也转换为int.它假设仅尝试将textbox2,3,4中的值转换为int和double并单独保留字符串,以便我可以创建 一个对象匹配Parts(String,int,int,double)

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)

这是我的零件课:

请帮助!

谢谢!


 

推荐答案

   将零件变暗作为零件

 

    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


这篇关于错误System.InvalidCastException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆