Question Invalid Cast Exception

pythonregius24

New member
Joined
Feb 17, 2010
Messages
3
Programming Experience
Beginner
Hey Guys! I am a newbie. I need help on my homework. Im getting this from the immediate window, "A first chance exception of type 'System.InvalidCastException' occurred in Microsoft.VisualBasic.dll" Help pls.

This is my code

Option Strict On
Option Explicit On
Public Class frmTestScoreAverage

Private Sub btnCalculateAverage_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculateAverage.Click
Try


Dim TestScoreAverage As Double
Dim TestScore1, TestScore2, TestScore3, TestScore4, TestScore5 As Double

TestScore1 = CDbl(txtTestScore1.Text)
TestScore2 = CDbl(txtTestScore2.Text)
TestScore3 = CDbl(txtTestScore3.Text)
TestScore4 = CDbl(txtTestScore4.Text)
TestScore5 = CDbl(txtTestScore5.Text)



TestScoreAverage = (TestScore1 + TestScore2 + TestScore3 + TestScore4 _
+ TestScore5) / 5


lblAverageScore.Text = CStr(TestScoreAverage)

Catch ex As Exception

MessageBox.Show(ex.Message)
End Try
 
: Invalid Cast Exception

Hey Guys! I am a newbie. I need help on my homework. Im getting this from the immediate window, "A first chance exception of type 'System.InvalidCastException' occurred in Microsoft.VisualBasic.dll" Help pls.

This is my code

Option Strict On
Option Explicit On
Public Class frmTestScoreAverage

Private Sub btnCalculateAverage_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculateAverage.Click
Try


Dim TestScoreAverage As Double
Dim TestScore1, TestScore2, TestScore3, TestScore4, TestScore5 As Double

TestScore1 = CDbl(txtTestScore1.Text)
TestScore2 = CDbl(txtTestScore2.Text)
TestScore3 = CDbl(txtTestScore3.Text)
TestScore4 = CDbl(txtTestScore4.Text)
TestScore5 = CDbl(txtTestScore5.Text)



TestScoreAverage = (TestScore1 + TestScore2 + TestScore3 + TestScore4 _
+ TestScore5) / 5


lblAverageScore.Text = CStr(TestScoreAverage)

Catch ex As Exception

MessageBox.Show(ex.Message)
End Try
 
I'm not going to give you the answer (though someone else most likely will very shortly) but I am going to tell you how to find the problem. Place a breakpoint at the beginning of the code you are having problems with then run and step through the code. You will be able to find out where the problem is, If you need more help after that, post your results here for more help.
 
Cast Exception

I have no idea sir. I am a total newbie, but i know that the problem is the a string is trying to convert to double.
 
Last edited:
As you mentioned this was for homework I to feel you should not just get the anwser, However... :-D If you step through the code you should be able to see what is not being able to be converted. Another Hint would be to look at the data you are entering into your text boxes and be sure that the text values can be considered doubles (ie. Numbers)
 
The code works fine for me

Just dropped 5 textboxes onto a form with text values 100, 90, 80, 70, & 60.

VB.NET:
Option Strict On
Option Explicit On

Public Class Form1

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


            Dim TestScoreAverage As Double
            Dim TestScore1, TestScore2, TestScore3, TestScore4, TestScore5 As Double

            TestScore1 = CDbl(TextBox1.Text)
            TestScore2 = CDbl(TextBox2.Text)
            TestScore3 = CDbl(TextBox3.Text)
            TestScore4 = CDbl(TextBox4.Text)
            TestScore5 = CDbl(TextBox5.Text)



            TestScoreAverage = (TestScore1 + TestScore2 + TestScore3 + TestScore4 _
            + TestScore5) / 5


            Label1.Text = CStr(TestScoreAverage)

        Catch ex As Exception

            MessageBox.Show(ex.Message)
        End Try
    End Sub
End Class
 
Please don't post the same question multiple times. Post each question once and once only, in the most appropriate forum. If you feel that you have posted in the wrong forum, send a message to one or more moderators and ask to have it moved. Threads merged.
 
Back
Top