Valeria Kaya
Valeria Kaya

Reputation: 131

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.

Upvotes: 0

Views: 2047

Answers (2)

Jade
Jade

Reputation: 2992

Try this

Function IsValidDecimal(ByVal txt As String) As Boolean
    Try
        Dim t As Decimal = 0
        If Decimal.TryParse(txt, t) Then
            If t >= 0 Then
                Return True
            End If
        End If
    Catch ex As Exception
    End Try
    Return False
End Function    

Function CalcPartsCharges() As Decimal
    If Me.IsValidDecimal(Me.txtParts.Text) Then Return CDec(Me.txtParts.Text)
    Return 0
End Function

Function CalcLaborCharges() As Decimal
    If Me.IsValidDecimal(Me.txtLabor.Text) Then Return CDec(Me.txtLabor.Text) * 20
    Return 0
End Function

OR simply

Function GetDecimal(ByVal txt As String) As Decimal
    Dim t As Decimal = 0
    Try
        If Not Decimal.TryParse(txt, t) Then Return 0
        If t < 0 Then t = 0
    Catch ex As Exception
    End Try
    Return t
End Function

Function CalcPartsCharges() As Decimal
    Return Me.GetDecimal(Me.txtParts.Text)
End Function

Function CalcLaborCharges() As Decimal
    Return Me.GetDecimal(Me.txtLabor.Text) * 20
End Function

Upvotes: 1

Mark Hall
Mark Hall

Reputation: 54562

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

Upvotes: 1

Related Questions