2012-04-05 6 views
0

セールスマンボーナスの計算レベルを経験値(1-4)に応じて整数で取得しようとしていますが、売り上げ(1-10000)は小数。なぜこのエラーが出るのですか?ここにコードは...エラーオブジェクト参照がオブジェクトのインスタンスに設定されていません

Public Class Form1 

    Dim intLvlsTextBox1Numbers As String = Nothing And intLvlsTextBox1Numbers = txtBoxLvl.Text 
    Dim decSaleWkTextBox1Numbers As String = Nothing And decSaleWkTextBox1Numbers = txtBoxWklySale.Text 

    Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click 
     'The Try/Catch method is used to catch the "InvalidCastExceptionUnhandeled" in order to use the 
     'exception for detecting letters within the textboxes being understood in logic as of 
     'a wrong type, thus enabling its detection as letter or caracter. 
     Try 
      intLvlsTextBox1Numbers = Convert.ToInt32(txtBoxLvl.Text) 
      decSaleWkTextBox1Numbers = Convert.ToInt32(txtBoxWklySale.Text) 
      'This line is used to validate indetify non-valid data input upon entring numbers 
      'that are out of rang, and will display the warning error message. It goes from 
      'anything not convertable to Integer 32, i.e. letter, signs. 
      MessageBox.Show("Please, input a numerical value") 
      'Reset the cursor position when a non-valid data is inputed. 
      txtBoxLvl.Select() 
      'Catches the EX variable execption,"InvalidCastExceptionUnhandeled". 
     Catch ex As FormatException 

     End Try 

     'procedure call to the ChoiceMenus 
     ChoiceMenus() 

    End Sub 
    Private Sub ChoiceMenus() 
     Select Case intLvlsTextBox1Numbers 
      Case 1 To 4 
     End Select 
     Select Case decSaleWkTextBox1Numbers 
      Case 1 To 100000 
     End Select 
    End Sub 
    Private Sub isValidCalculation() 
     lblTotWkSale.Text = 250 * (intLvlsTextBox1Numbers + 1) + ((intLvlsTextBox1Numbers + 1)/100) * (decSaleWkTextBox1Numbers) 
     decSaleWkTextBox1Numbers = Convert.ToString(lblTotWkSale.Text) 
     lblTotWkSale.Text = decSaleWkTextBox1Numbers.ToString("c") 
    End Sub 
    Private Sub btnClr_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClr.Click 

     clearForm() 'procedure call 

    End Sub 

    Private Sub clearForm() 
     txtBoxWklySale.Text = "" 
     txtBoxLvl.Text = "" 
     lblTotWkSale.Text = "" 
     lblErrorMsg.Text = "" 
     txtBoxLvl.Select() 
     'tbxHome = True 
     'tbx1 = True 
     'tbx2 = True 
    End Sub 

    Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click 

     Me.Close() 'closes the application 
    End Sub 
End Class 

ありがとうございます!

答えて

1

問題は、Convert.ToInt32の使用です。この関数には、IConvertibleインターフェイスを実装するオブジェクトが必要です。それ以外の場合はInvalidCastExceptionがスローされます。

あなたがしたいのは、StringInt32に解析することです。 Int32.ParseまたはInt32.TryParseで最もうまくいっています。後者は、入力が実際に小数値であるかどうか不明な場合に有効です。

だから、最後に、これはあなたがやりたいしたいされています

intLvlsTextBox1Numbers = Int32.Parse(txtBoxLvl.Text) 
decSaleWkTextBox1Numbers = Int32.Parse(txtBoxWklySale.Text)
+0

これはそうではありません。エラーメッセージはInvalidCastExceptionではありません –

1
Dim intLvlsTextBox1Numbers As String = Nothing And intLvlsTextBox1Numbers = txtBoxLvl.Text 

あなたがフィールドを初期化する方法は非常に創造的です。私はそれが何をすべきか理解できませんが。ランタイムはどちらも、フォームのコンストラクターが実行されるときにNREでプログラムを爆破します。デバッガがあなたに良い行を見せてくれないので、診断が難しい。 txtBoxLvl変数がまだ初期化されていないため、InitializeComponent()が実行された後でないと、失敗します。悲しいことに、vb.netが隠すものもあります。

vb.netプログラミングについてのまともな本で見つけることができるより正気なコードを書くようにしてください。これらの変数は、フォームクラス内のフィールドであってはならず、メソッドのローカル変数である必要があります。

Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click 
    Dim intLvlsTextBox1Numbers As Integer '' It is not a string!! 
    Dim levelOk = Integer.TryParse(txtBoxLvl.Text, intLvlsTextBox1Numbers) 
    If Not levelOk Then 
     '' complain... 
     Return 
    End If 
    '' etc... 
End Sub 
関連する問題