Partager via


Version.Parse Method

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Converts the string representation of a version number to an equivalent Version object.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)

Syntax

'Declaration
Public Shared Function Parse ( _
    input As String _
) As Version
public static Version Parse(
    string input
)

Parameters

  • input
    Type: System.String
    A string that contains a version number to convert.

Return Value

Type: System.Version
An object that is equivalent to the version number specified in the input parameter.

Exceptions

Exception Condition
ArgumentNullException

input is nulla null reference (Nothing in Visual Basic).

ArgumentException

input has fewer than two or more than four version components.

ArgumentOutOfRangeException

At least one component in input is less than zero.

FormatException

At least one component in input is not an integer.

OverflowException

At least one component in input represents a number that is greater than Int32.MaxValue.

Remarks

The input parameter must have the following format:

major.minor[.build[.revision]]

where major, minor, build, and revision are the string representations of the version number's four components: major version number, minor version number, build number, and revision number. Optional components are shown in square brackets ([ and ]). The components must appear in the specified order, and must be separated by periods.

Important noteImportant Note:

Because the string representation of a version number must conform to a recognized pattern, applications should always use exception handling when calling the Parse method to parse user input. Alternatively, you can call the TryParse method to parse the string representation of a version number and return a value that indicates whether the parse operation succeeded.

The Parse method is a convenience method; it is equivalent to calling the Version(String) constructor.

Examples

The following example uses the Parse method to parse a number of strings that contain version information.

Module Example
   Public Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
      Dim input As String = "4.0"
      ParseVersion(outputBlock, input)

      input = "4.0."
      ParseVersion(outputBlock, input)

      input = "1.1.2"
      ParseVersion(outputBlock, input)

      input = "1.1.2.01702"
      ParseVersion(outputBlock, input)

      input = "1.1.2.0702.119"
      ParseVersion(outputBlock, input)

      input = "1.3.5.2150000000"
      ParseVersion(outputBlock, input)
   End Sub

   Private Sub ParseVersion(ByVal outputBlock As System.Windows.Controls.TextBlock, ByVal input As String)
      Try
         Dim ver As Version = Version.Parse(input)
         outputBlock.Text += String.Format("Converted '{0} to {1}.", input, ver) & vbCrLf
      Catch e As ArgumentNullException
         outputBlock.Text &= "Error: String to be parsed is null." & vbCrLf
      Catch e As ArgumentOutOfRangeException
         outputBlock.Text += String.Format("Error: Negative value in '{0}'.", input) & vbCrLf
      Catch e As ArgumentException
         outputBlock.Text += String.Format("Error: Bad number of components in '{0}'.", _
                           input) & vbCrLf
      Catch e As FormatException
         outputBlock.Text += String.Format("Error: Non-integer value in '{0}'.", input) & vbCrLf
      Catch e As OverflowException
         outputBlock.Text += String.Format("Error: Number out of range in '{0}'.", input) & vbCrLf
      End Try
   End Sub
End Module
' The example displays the following output:
'       Converted '4.0 to 4.0.
'       Error: Non-integer value in '4.0.'.
'       Converted '1.1.2 to 1.1.2.
'       Converted '1.1.2.01702 to 1.1.2.1702.
'       Error: Bad number of components in '1.1.2.0702.119'.
'       Error: Number out of range in '1.3.5.2150000000'.
using System;

public class Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      string input = "4.0";
      ParseVersion(outputBlock, input);

      input = "4.0.";
      ParseVersion(outputBlock, input);

      input = "1.1.2";
      ParseVersion(outputBlock, input);

      input = "1.1.2.01702";
      ParseVersion(outputBlock, input);

      input = "1.1.2.0702.119";
      ParseVersion(outputBlock, input);

      input = "1.3.5.2150000000";
      ParseVersion(outputBlock, input);
   }

   private static void ParseVersion(System.Windows.Controls.TextBlock outputBlock, string input)
   {
      try
      {
         Version ver = Version.Parse(input);
         outputBlock.Text += String.Format("Converted '{0} to {1}.", input, ver) + "\n";
      }
      catch (ArgumentNullException)
      {
         outputBlock.Text += "Error: String to be parsed is null." + "\n";
      }
      catch (ArgumentOutOfRangeException)
      {
         outputBlock.Text += String.Format("Error: Negative value in '{0}'.", input) + "\n";
      }
      catch (ArgumentException)
      {
         outputBlock.Text += String.Format("Error: Bad number of components in '{0}'.",
                           input) + "\n";
      }
      catch (FormatException)
      {
         outputBlock.Text += String.Format("Error: Non-integer value in '{0}'.", input) + "\n";
      }
      catch (OverflowException)
      {
         outputBlock.Text += String.Format("Error: Number out of range in '{0}'.", input) + "\n";
      }
   }
}
// The example displays the following output:
//       Converted '4.0 to 4.0.
//       Error: Non-integer value in '4.0.'.
//       Converted '1.1.2 to 1.1.2.
//       Converted '1.1.2.01702 to 1.1.2.1702.
//       Error: Bad number of components in '1.1.2.0702.119'.
//       Error: Number out of range in '1.3.5.2150000000'.

Version Information

Silverlight

Supported in: 5, 4

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.