Export (0) Print
Expand All

Guid Constructor (String)

Initializes a new instance of the Guid structure by using the value represented by the specified string.

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

'Declaration
Public Sub New ( _
	g As String _
)

Parameters

g
Type: System.String

A string that contains a GUID in one of the following formats ("d" represents a hexadecimal digit whose case is ignored):

32 contiguous digits:

dddddddddddddddddddddddddddddddd

-or-

Groups of 8, 4, 4, 4, and 12 digits with hyphens between the groups. The entire GUID can optionally be enclosed in matching braces or parentheses:

dddddddd-dddd-dddd-dddd-dddddddddddd

-or-

{dddddddd-dddd-dddd-dddd-dddddddddddd}

-or-

(dddddddd-dddd-dddd-dddd-dddddddddddd)

-or-

Groups of 8, 4, and 4 digits, and a subset of eight groups of 2 digits, with each group prefixed by "0x" or "0X", and separated by commas. The entire GUID, as well as the subset, is enclosed in matching braces:

{0xdddddddd, 0xdddd, 0xdddd,{0xdd,0xdd,0xdd,0xdd,0xdd,0xdd,0xdd,0xdd}}

All braces, commas, and "0x" prefixes are required. All embedded spaces are ignored. All leading zeros in a group are ignored.

The digits shown in a group are the maximum number of meaningful digits that can appear in that group. You can specify from 1 to the number of digits shown for a group. The specified digits are assumed to be the low-order digits of the group.

ExceptionCondition
ArgumentNullException

g is Nothing.

FormatException

The format of g is invalid.

OverflowException

The format of g is invalid.

The alphabetic hexadecimal digits in the g parameter can be uppercase or lowercase. For example, the following strings represent the same GUID:

"ca761232ed4211cebacd00aa0057b223"

"CA761232-ED42-11CE-BACD-00AA0057B223"

"{CA761232-ED42-11CE-BACD-00AA0057B223}"

"(CA761232-ED42-11CE-BACD-00AA0057B223)"

"{0xCA761232, 0xED42, 0x11CE, {0xBA, 0xCD, 0x00, 0xAA, 0x00, 0x57, 0xB2, 0x23}}"

The following example passes each string listed in the Remarks section to the Guid(String) constructor.

Module Example
   Public Sub Main()
      Dim guidStrings() As String = { "ca761232ed4211cebacd00aa0057b223",
                                      "CA761232-ED42-11CE-BACD-00AA0057B223", 
                                      "{CA761232-ED42-11CE-BACD-00AA0057B223}", 
                                      "(CA761232-ED42-11CE-BACD-00AA0057B223)", 
                                      "{0xCA761232, 0xED42, 0x11CE, {0xBA, 0xCD, 0x00, 0xAA, 0x00, 0x57, 0xB2, 0x23}}" }
      For Each guidString In guidStrings
         Dim guid As New Guid(guidString)
         Console.WriteLine("Original string: {0}", guidString)
         Console.WriteLine("Guid:            {0}", guid)
         Console.WriteLine()
      Next 
   End Sub 
End Module 
' The example displays the following output: 
'    Original string: ca761232ed4211cebacd00aa0057b223 
'    Guid:            ca761232-ed42-11ce-bacd-00aa0057b223 
'     
'    Original string: CA761232-ED42-11CE-BACD-00AA0057B223 
'    Guid:            ca761232-ed42-11ce-bacd-00aa0057b223 
'     
'    Original string: {CA761232-ED42-11CE-BACD-00AA0057B223} 
'    Guid:            ca761232-ed42-11ce-bacd-00aa0057b223 
'     
'    Original string: (CA761232-ED42-11CE-BACD-00AA0057B223) 
'    Guid:            ca761232-ed42-11ce-bacd-00aa0057b223 
'     
'    Original string: {0xCA761232, 0xED42, 0x11CE, {0xBA, 0xCD, 0x00, 0xAA, 0x00, 0x57, 0xB2, 0x23}} 
'    Guid:            ca761232-ed42-11ce-bacd-00aa0057b223

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

.NET for Windows Store apps

Supported in: Windows 8

Supported in: Windows Phone 8.1

Supported in: Windows Phone Silverlight 8.1

Supported in: Windows Phone Silverlight 8

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft