我有一个错误,上面写着:" InvalidCastExceptionWasUnhandled" (VB)

时间:2015-12-24 04:38:50

标签: vb.net

当我运行我的计算器程序而不是在减号中说InvalidCastExceptionWasUnhandled但我认为没有错,这是代码:

Public Class FormCalc

Private Sub btnkali_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnkali.Click
    Dim num1 As Double = txtnum1.Text
    Dim num2 As Double = txtnum2.Text
    txthasil.Text = num1 * num2
End Sub

Private Sub btnbagi_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnbagi.Click
    Dim num1 As Double = txtnum1.Text
    Dim num2 As Double = txtnum2.Text
    txthasil.Text = num1 / num2
End Sub

Private Sub btntambah_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btntambah.Click
    Dim num1 As Double = txtnum1.Text
    Dim num2 As Double = txtnum2.Text
    txthasil.Text = num1 + num2
End Sub

Private Sub btnkurang_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnkurang.Click
    Dim num1 As Double = txtnum1.Text
    Dim num2 As Double = txtnum2.Text
    txthasil.Text = num1 - num2
End Sub

Private Sub btnmod_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnmod.Click
    Dim num1 As Double = txtnum1.Text
    Dim num2 As Double = txtnum2.Text
    txthasil.Text = num1 Mod num2
End Sub

Private Sub btnpi_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnpi.Click
    Dim num1 As Double = txtnum1.Text
    Dim num2 As Double = txtnum2.Text
    txthasil.Text = num1 * Math.PI
    txtnum2.Text = ""
End Sub

Private Sub FormCalc_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

End Sub

结束班

如果我使用函数但是为什么它不能在sub中运行它可以正常工作???有人可以解释一下吗?谢谢! :)

1 个答案:

答案 0 :(得分:0)

您必须确保在将字符串转换为double时,您在字符串中有一个数字。检查您的代码。本网站不适用于您的程序!