Results 1 to 8 of 8

Thread: Question About Error I am receiving When Calling A Function

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2007
    Posts
    3

    Question About Error I am receiving When Calling A Function

    Hello, I am writing an application to gain an understanding of how to use Functions and Sub procedures. I am receiving the following error when I am trying to call my functions. If anybody can offer any feedback it would be appreciated. Please view my code below.

    Here is the error I am receiving:
    An unhandled exception of type 'System.InvalidCastException' occurred in microsoft.visualbasic.dll

    Additional information: Cast from string "" to type 'Decimal' is not valid.

    The program '[2456] Function_Problem2.exe' has exited with code 0 (0x0).

    Here is the code to Call The Functions
    'Declaring Variables'
    Dim decSubtotal As Decimal ' Holds Services & Labor'
    Dim decPart As Decimal ' Holds parts from other services'
    Dim decTax As Decimal ' Holds tax on parts'
    Dim decTotalFees As Decimal ' Holds the total of everything'

    'The breakpoint at RunTime is going to this First line of code'
    decSubtotal = OilLubeCharges(txtSerLabor.Text) + FlushCharges(txtSerLabor.Text) + MiscCharges(txtSerLabor.Text) + OtherCharges(txtSerLabor.Text)
    decPart = OtherCharges(txtPart.Text)
    decTax = TaxCharges(txtTax.Text)
    decTotalFees = TotalCharges(txtTotalFees.Text)

    Here are the Functions I wrote in my form
    Function OilLubeCharges(ByRef pdecOil As Decimal) As Decimal
    Dim decoilChangeCost As Decimal
    If chkOilChange.Checked = True Then
    decoilChangeCost = " $26.00 "
    Else
    decoilChangeCost = " $18.00 "
    End If

    Return decoilChangeCost
    End Function

    Function FlushCharges(ByRef pdecFlushes As Decimal) As Decimal
    Dim decFlushes As Decimal
    If chkRadFlush.Checked = True Then
    decFlushes = " $30.00 "
    Else
    decFlushes = " $80.00 "
    End If

    Return decFlushes
    End Function

    Function MiscCharges(ByRef pdecMisc As Decimal) As Decimal
    Dim decMisc As Decimal
    If chkInspection.Checked = True Then
    decMisc = " $15.00 "
    ElseIf chkMuffler.Checked = True Then
    decMisc = " $100.00 "

    Else
    decMisc = " $20.00 "
    End If

    Return decMisc
    End Function

    Function OtherCharges(ByRef pdecOther As Decimal) As Decimal
    Dim decPart As Decimal
    Dim decLabor As Decimal
    Dim decTotal As Decimal

    decPart = Val(txtPart.Text)
    decLabor = Val(txtLabor.Text)

    txtPart.Text = decPart
    txtLabor.Text = decLabor
    decTotal = decPart + decLabor

    Return decTotal
    End Function

    Function TaxCharges(ByVal amount As Decimal) As Decimal
    Dim decSalesTax As Decimal
    decSalesTax = amount * 6%
    Return decSalesTax
    End Function

    Function TotalCharges(ByVal total As Decimal) As Decimal
    Dim decTotal As Decimal
    decTotal = txtSerLabor.Text + txtParts.Text + txtTax.Text + txtTotalFees.Text
    Return decTotal
    End Function

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: Question About Error I am receiving When Calling A Function

    It appears that you have Option Strict ON, which is just as it should be, however, it also means that implicit conversions don't work. That's fine, they are slow anyways.

    The problem is that your functions are expecting a decimal value, while the textbox text property returns a string. If you can be certain that what is in the textbox is a valid number, then you can simply call the function like this:


    OilLubeCharges(CDec(txtSerLabor.Text))

    CDec() takes a string (the txtSerLabor.Text in this case), and turns it into a decimal value. The only drawback to this function is that if there are any invalid characters in the textbox (a letter, for instance), CDex will throw an exception. There are a couple of ways to get around that. The worst is to wrap the call in a Try...Catch block. This is bad because the error is preventable, and wrapping in a block like that won't really allow you to fix the problem, it will just be a particularly slow way to identify the problem.

    A better solution would be to use Decimal.TryParse(), which returns True if the string can be converted, and false if it cannot. Unfortunately, in your case, it wouldn't be all that good, because the converted number is returned in one of the arguments to the function, so you can't use it as an argument to a function (at least not in a nice fashion).

    The best solution would be to restrict the textboxes to only allow valid characters, or validate them (perhaps using TryParse()) before you use the data. If you want to restrict the numbers, there are lots of threads in this forum that cover that. On the other hand, if you are just trying things out, you can simply ignore that issue for now and just use CDec.
    My usual boring signature: Nothing

  3. #3

    Thread Starter
    New Member
    Join Date
    Jul 2007
    Posts
    3

    Re: Question About Error I am receiving When Calling A Function

    Thank you, very much for your help and explanation. It was very helpful.

  4. #4

    Thread Starter
    New Member
    Join Date
    Jul 2007
    Posts
    3

    Re: Question About Error I am receiving When Calling A Function

    Hey, another quick question. I took your advice and did the following.

    decSubtotal = OilLubeCharges(CDec(txtSerLabor.Text) + FlushCharges(CDec(txtSerLabor.Text) _
    + MiscCharges(CDec(txtSerLabor.Text) + OtherCharges(txtSerLabor.Text)

    But, I keep getting an error after OtherCharges(txtSerLabor.Text) telling me ')' expected. What am I missing here to keep getting this error. Thank you, for your help again.

  5. #5
    Lively Member
    Join Date
    Oct 2006
    Location
    USA
    Posts
    122

    Re: Question About Error I am receiving When Calling A Function

    it looks like you are missing some closing parens " ) ". you need to add then in what looks like two places.


  6. #6
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Question About Error I am receiving When Calling A Function

    tehehehe.... it's kinda like Where's Waldo ... I found three

    decSubtotal = OilLubeCharges(CDec(txtSerLabor.Text)) + FlushCharges(CDec(txtSerLabor.Text)) _
    + MiscCharges(CDec(txtSerLabor.Text)) + OtherCharges(txtSerLabor.Text)

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  7. #7
    Lively Member
    Join Date
    Oct 2006
    Location
    USA
    Posts
    122

    Re: Question About Error I am receiving When Calling A Function

    ooopsss....I think I should get my eyes checked

  8. #8
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: Question About Error I am receiving When Calling A Function

    There's actually a hidden fourth one, too, because the CDec is missing for the call to OtherCharges(), which would cause the same error that started this thread.
    My usual boring signature: Nothing

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width