Link to home
Start Free TrialLog in
Avatar of Software Engineer
Software Engineer

asked on

VB.Net "Casting"

Hi:
Below is my "if, then" statement in VB.Net code.  
What "cast" syntax can I add to either the _TOTAL_LABOR_ or GBL_TEC fields if I want to change their data types from, say, text to numeric?
Thanks!
Software Engineer

if _TOTAL_LABOR_ > GBL_TEC then 
return false
else
return true
end if

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Éric Moreau
Éric Moreau
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of noci
noci

Which are actualy data conversions.
A Cast is an other way to interpret the data without conversion.

(short) (long) 1234;    means to use the short version of the long  of value 1234.  ( in C), no conversion involved.  Just chopping off data  or expanding the sign bit.
you can just use

return Clng(_TOTAL_LABOR_) > Clng(GBL_TEC)

or

return Clnt(_TOTAL_LABOR_) > Clnt(GBL_TEC)

no need for extra if...then...else code here...

What you are wanting is Converting and not Casting.  CDBL and CINT are holdovers from Visual Basic and are still viable along with CLNG, CSHORT; etc. etc.

https://docs.microsoft.com/en-us/dotnet/visual-basic/language-reference/functions/type-conversion-functions

However, Microsoft (and many other developers) recommend using the various .NET methods and/or conversion classes.

Proof of concepts:
Imports System

Module Program
    Sub Main(args As String())
        Dim values = {"21", "2", "21.4", "True"}
        For Each value In values
            Try
                Console.WriteLine($"Converting using CDBL function - {CDbl(value)}")
            Catch ex As Exception
                Console.WriteLine($"Could not convert value - {value}; {ex.Message}")
            End Try
        Next
        Console.WriteLine()

        For Each value In values
            Try
                Console.WriteLine($"Converting using the Convert Class - {Convert.ToDouble(value)}")
            Catch ex As Exception
                Console.WriteLine($"Could not convert value - {value}; {ex.Message}")
            End Try
        Next
        Console.WriteLine()

        For Each value In values
            Try
                Console.WriteLine($"Converting using the Parse Method - {Double.Parse(value)}")
            Catch ex As Exception
                Console.WriteLine($"Could not convert value - {value}; {ex.Message}")
            End Try
        Next
        Console.WriteLine()

        For Each value In values
            Dim converted As Double
            If Double.TryParse(value, converted) Then
                Console.WriteLine($"Converted using the TryParse Method - {converted}")
            Else
                Console.WriteLine($"Could not convert value - {value}; It's not a double value")
            End If
        Next
        Console.ReadLine()
    End Sub
End Module

Open in new window

Produces the following output -User generated imageHTH,

-saige-