Q:

VB.Net Boolean Argument

I was working on an assignment. It is already done, but I would like to find out more efficient way to use Boolean in future projects.
I had multiple functions needed validation. I tried argument with no success and had to create individual Boolean per function.
My code is
Function valChargeParts() As Boolean
    Try
        If Decimal.TryParse(CDec(txtParts.Text), 1) Then
            If CDec(txtParts.Text) >= 0 Then
                    Return True
                End If
            End If
    Catch ex As Exception
    End Try
        Return False
End Function

Function valChargeLabor() As Boolean
    Try
        If Decimal.TryParse(CDec(txtLabor.Text), 1) Then
            If CDec(txtLabor.Text) >= 0 Then
                Return True
            End If
        End If
    Catch ex As Exception
    End Try
    Return False
End Function

Function CalcPartsCharges() As Decimal
    Dim totalParts As Integer = 0

    If valChargeParts() = True Then
        totalParts += CDec(txtParts.Text)
    End If

    Return totalParts
End Function

Function CalcLaborCharges() As Decimal
    Dim totalLabor As Integer = 20

    If valChargeLabor() = True Then
        totalLabor *= CDec(txtLabor.Text)
    End If

    Return totalLabor
End Function

I tried many possible ways to my knowledge to create arguments, such as;
Function valChargeLabor(ByVal variable as Decimal) As Boolean

and tried using in function,
If Decimal.TryParse(variable, 1) Then

but none worked. I'm not sure what I'm doing wrong.
Thanks in advance.

A:

Looking at your code you are using the TryParse method which will not give you an error if it can not convert, plus you are using multiple casting to decimal that in my opinion are not needed. I have made a common routine that will return a Boolean if it can convert plus it will pass by reference the actual converted value so it will make it like your own TryParse Statement.
This is a simple Modified application to demonstrate what I am talking about.
Public Class Form1
    Dim totalParts As Decimal = 0
    Function validateInput(value As String, ByRef output As Decimal) As Boolean
        Return Decimal.TryParse(value, output)
    End Function

    Function CalcPartsCharges() As Decimal
        Dim tempParts As Decimal = 0

        If validateInput(txtParts.Text, tempParts) Then
            totalParts += tempParts
        End If

        Return totalParts
    End Function

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Label1.Text = CalcPartsCharges().ToString()
    End Sub
End Class

