TryParse a Double

时间:2013-07-06 04:58:18

标签: vb.net visual-studio-2012 numbers double tryparse

我有一个变量(ApplicationVersionInformationArray(0)),它包含以下值:“1.0.0.1”

确定此值是否大于1.0

的最佳方法是什么

这是我当前的代码(ApplicationVersionInformationNumber是Double类型):

    If Double.TryParse(ApplicationVersionInformationArray(0), ApplicationVersionInformationNumber) = False Then
        Return False
    Else
        If ApplicationVersionInformationNumber < 1 Then Return False
    End If

更新

我发现有一个版本类可以轻松完成此转换。

2 个答案:

答案 0 :(得分:1)

以下是我为了比较版本而长时间写的课程的精简版。

Public Class Version
    Implements IEquatable(Of Version)

    Private _mmajorVersion As Integer = 0
    Private _mminorVersion As Integer = 0
    Private _mrivision As Integer = 0
    Private _mbuild As Integer = 0

    Sub New()
        _mmajorVersion = 0
        _mminorVersion = 0
        _mrivision = 0
        _mbuild = 0
    End Sub
    Sub New(ByVal VersionObj As Object)
        If VersionObj Is DBNull.Value Then
            _mmajorVersion = 0
            _mminorVersion = 0
            _mrivision = 0
            _mbuild = 0
        Else
            ParseVersionString(VersionObj.ToString)
        End If
    End Sub

    Public Sub New(ByVal VersionString As String)
        ParseVersionString(VersionString)
    End Sub

    Public Property MajorVersion As Integer
        Get
            Return _mmajorVersion
        End Get
        Set(ByVal value As Integer)
            _mmajorVersion = value
        End Set
    End Property
    Public Property MinorVersion As Integer
        Get
            Return _mminorVersion
        End Get
        Set(ByVal value As Integer)
            _mminorVersion = value
        End Set
    End Property
    Public Property Revision As Integer
        Get
            Return _mrivision
        End Get
        Set(ByVal value As Integer)
            _mrivision = value
        End Set
    End Property

    Private Sub ParseVersionString(ByVal Version_String As String)
        Dim p() As String = Version_String.Split(New Char() {"."}, 4)
        If IsNumeric(p(0)) Then
            _mmajorVersion = CInt(p(0))
        End If
        If p.Length > 1 Then
            If IsNumeric(p(1)) Then
                _mminorVersion = CInt(p(1))
            End If
        End If
        If p.Length > 2 Then
            If IsNumeric(p(2)) Then
                _mrivision = CInt(p(2))
            End If
        End If
        If p.Length > 3 Then
            If IsNumeric(p(3)) Then
                _mbuild = CInt(p(3))
            End If
        End If
    End Sub

    Public Shared Operator <(ByVal Version1 As Version, ByVal Version2 As Version) As Boolean
        If Version1 = Version2 Then Return False
        If Version1._mmajorVersion < Version2._mmajorVersion Then Return True
        If Version1._mmajorVersion > Version2._mmajorVersion Then Return False
        If Version1._mminorVersion < Version2._mminorVersion Then Return True
        If Version1._mminorVersion > Version2._mminorVersion Then Return False
        If Version1._mrivision < Version2._mrivision Then Return True
        If Version1._mrivision > Version2._mrivision Then Return False
        If Version1._mbuild < Version2._mbuild Then Return True
        If Version1._mbuild > Version2._mbuild Then Return False
        Return False
    End Operator
    Public Shared Operator >(ByVal Version1 As Version, ByVal Version2 As Version) As Boolean
        Return Not (Version1 < Version2 Or Version1 = Version2)
    End Operator

    Public Shared Operator <=(ByVal Version1 As Version, ByVal Version2 As Version) As Boolean
        If Version1 > Version2 Then Return False
        Return True
    End Operator
    Public Shared Operator >=(ByVal Version1 As Version, ByVal Version2 As Version) As Boolean
        If Version1 < Version2 Then Return False
        Return True
    End Operator

    Public Shared Operator =(ByVal Version1 As Version, ByVal Version2 As Version) As Boolean
        If Version1._mmajorVersion = Version2._mmajorVersion And _
            Version1._mminorVersion = Version2._mminorVersion And _
            Version1._mrivision = Version2._mrivision And _
            Version1._mbuild = Version2._mbuild Then
            Return True
        End If
        Return False
    End Operator
    Public Shared Operator <>(ByVal Version1 As Version, ByVal Version2 As Version) As Boolean
        Return Not Version1 = Version2
    End Operator
    Public Overrides Function Equals(ByVal obj As Object) As Boolean
        If obj Is Nothing Then Return False
        If obj.GetType.Name <> Me.GetType.Name Then Return False
        Dim v As Version = DirectCast(obj, Version)
        Return Me = v
    End Function
    Public Overloads Function Equals(ByVal other As Version) As Boolean Implements System.IEquatable(Of Version).Equals
        Return Me = other
    End Function

    Public Overrides Function ToString() As String
        Return String.Format("{0}.{1}.{2}.{3}", _mmajorVersion.ToString, _mminorVersion.ToString, _mrivision.ToString, _mbuild.ToString)
    End Function

    Public Overrides Function GetHashCode() As Integer
        Return Me.ToString.GetHashCode()
    End Function
End Class

你可以像这样打电话给你。

    Return New Version(ApplicationVersionInformationArray(0)) > New Version("1.0.0.0")

答案 1 :(得分:0)

我在另一个论坛中找到了这段代码

    Dim versionText As String = "1.0.0.1"
    Dim currentVersion As Version

    If Version.TryParse(versionText, currentVersion) Then
        Dim minVersion As New Version(1, 0)
        If currentVersion > minVersion Then
            MessageBox.Show("Current version is greater than minimum version.")
        Else
            MessageBox.Show("Current version is NOT greater than minimum version.")
        End If
    Else
        MessageBox.Show("Current version is not valid.")
    End If

如果您做了一些小改动,它应该符合您的目的。 Version类中的Properties,包含版本中的所有字段。

Version class