Create/call functions, use TryParse

Hekzdaddy 121 Reputation points
2021-03-17T19:51:49.003+00:00

I am building a calculator for an auto repair shop (homework assignment) and I have to use create a function/call a function and use a Try Parse method.

Any tips/suggestions. Here are my variables :

Private dblOilChange As Double = 36.0
Private dblLubeJob As Double = 26.0
Private dblRadiatorFlush As Double = 50.0
Private dblTransmissionFlush As Double = 120.0
Private dblInspection As Double = 15.0
Private dblReplaceMuffler As Double = 200.0
Private dblTireRotation As Double = 20.0
Private dblParts As Double
Private dbltxtBoxParts As Double
Private dbltxtBoxLabor As Double
Private intTotal As Integer 

Thank you in advance,

Regards,

Hector B.

Developer technologies VB
{count} votes

1 answer

Sort by: Most helpful
  1. Dewayne Basnett 1,381 Reputation points
    2021-03-18T14:47:21.827+00:00

    This uses Nullable Value Types

        Private Function ToDouble(strVal As String) As Double?  
            ' Nullable Value Types  
            ' https://learn.microsoft.com/en-us/dotnet/visual-basic/programming-guide/language-features/data-types/nullable-value-types  
            Dim rv As Double?  
            Dim d As Double  
            If Double.TryParse(strVal, d) Then  
                rv = d  
            End If  
            Return rv  
        End Function  
      
    

    To use it

            Dim TheVal As Double  
            Dim valid As Double?  
            valid = ToDouble("ABD")  
            If valid.HasValue Then TheVal = valid.Value  
            valid = ToDouble("123.456")  
            If valid.HasValue Then TheVal = valid.Value  
      
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.