none
An unhandled exception of type 'System.InvalidCastException' occurred in Microsoft.VisualBasic.dll

    Question

  • I'm a beginner to Visual Basic. In here I want to recreate a piggy bank which tells me how much money do I have.  I created a function but the program tells me :

    An unhandled exception of type 'System.InvalidCastException' occurred in Microsoft.VisualBasic.dll

    Additional information: Conversion from string "" to type 'Integer' is not valid.

    I've been working on this for a few hours.  I hope you can help me.

    Code:

    Public Class Form1

        Public Function calcular(ByVal pesetas As Integer, ByVal dimes As Integer, ByVal vellones As Integer, ByVal centavos As Integer) As Double
            Dim total_calcular As Double
            total_calcular = (pesetas * 0.25 + dimes * 0.1 + vellones * 0.05 + centavos * 0.01)

            MsgBox("Usted tiene: " & FormatCurrency(total_calcular, 2),
    MsgBoxStyle.Information)

            Return total_calcular

        End Function
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim pesetas As String
            Dim dimes As String
            Dim vellones As Object
            Dim centavos As Object

            If (pesetas1.Text.Length = 0) Then
                MsgBox("Tiene que llenar la casilla de las pesetas", MsgBoxStyle.Exclamation, )
                pesetas1.Focus()
            End If

            If Not IsNumeric(pesetas1.Text) Then
                pesetas1.Text = ""
                MsgBox("Solo acepta formato en números", MsgBoxStyle.Exclamation, )
            End If

            If (dimes1.Text.Length = 0) Then
                MsgBox("Tiene que llenar la casilla de los dimes", MsgBoxStyle.Exclamation, )
                dimes1.Focus()
            End If

            If Not IsNumeric(dimes1.Text) Then
                dimes1.Text = ""
                MsgBox("Solo acepta formato en números", MsgBoxStyle.Exclamation, )
            End If

            If (vellones1.Text.Length = 0) Then
                MsgBox("Tiene que llenar la casilla de los vellones", MsgBoxStyle.Exclamation, )
                vellones1.Focus()

            End If

            If Not IsNumeric(vellones1.Text) Then
                vellones1.Text = ""
                MsgBox("Solo acepta formato en números", MsgBoxStyle.Exclamation, )
            End If


            If (centavos1.Text.Length = 0) Then
                MsgBox("Tiene que llenar la casilla de los centavos", MsgBoxStyle.Exclamation, )
                centavos1.Focus()
            End If

            If Not IsNumeric(centavos1.Text) Then
                centavos1.Text = ""
                MsgBox("Solo acepta formato en números", MsgBoxStyle.Exclamation, )
            End If


            pesetas = pesetas1.Text
            vellones = vellones1.Text
            dimes = dimes1.Text
            centavos = centavos1.Text


            total1.Text = FormatCurrency((calcular(pesetas, dimes, vellones, centavos)), 2)


        End Sub



    End Class

                  
    Sunday, February 26, 2012 3:18 AM

Answers

  • Your function "calcular" expects to be passed an integer for pesetas, among other things...
     
    When you call this in Button1_Click, the parameter you are passing is the text property from a textbox,saved into a string.  There are many ways to parse a textbox to an integer value.  Here is one method, since you have already determined the string is an iteger.  This is from a Microsoft walkthrough: http://msdn.microsoft.com/en-us/library/1aey0kb6(v=vs.90).aspx

    --
    Mike
    • Marked as answer by PandaVega Tuesday, February 28, 2012 3:47 AM
    Sunday, February 26, 2012 4:05 AM
  • The first action after installing VB should be setting Option Strict On as the default value. For already created projects you can enable it in the project properties, or on a per-file basis by putting "Option Strict On" at the top of a code file. With Option Strict Off, your code may fail because you've activated unsafe programming, disabled compiler checks and enabled automatic and unattended generation of implicit conversions that may fail or not, or may give unexpected or undesirable results. It is often hard for people trying to help if the code is not compilable and errors have to be fixed first.

    Armin

    • Marked as answer by PandaVega Tuesday, February 28, 2012 3:47 AM
    Monday, February 27, 2012 3:05 AM

All replies

  • Your function "calcular" expects to be passed an integer for pesetas, among other things...
     
    When you call this in Button1_Click, the parameter you are passing is the text property from a textbox,saved into a string.  There are many ways to parse a textbox to an integer value.  Here is one method, since you have already determined the string is an iteger.  This is from a Microsoft walkthrough: http://msdn.microsoft.com/en-us/library/1aey0kb6(v=vs.90).aspx

    --
    Mike
    • Marked as answer by PandaVega Tuesday, February 28, 2012 3:47 AM
    Sunday, February 26, 2012 4:05 AM
  • The first action after installing VB should be setting Option Strict On as the default value. For already created projects you can enable it in the project properties, or on a per-file basis by putting "Option Strict On" at the top of a code file. With Option Strict Off, your code may fail because you've activated unsafe programming, disabled compiler checks and enabled automatic and unattended generation of implicit conversions that may fail or not, or may give unexpected or undesirable results. It is often hard for people trying to help if the code is not compilable and errors have to be fixed first.

    Armin

    • Marked as answer by PandaVega Tuesday, February 28, 2012 3:47 AM
    Monday, February 27, 2012 3:05 AM