How to generate a GUID in VB.Net
VB.Net is a simple, modern, object-oriented computer programming language developed by Microsoft to combine the power of .NET Framework and the common language runtime with the productivity benefits that are the hallmark of Visual Basic. It is designed to create a wide range of Windows, Web, and mobile applications built on the .NET Framework.
Generating a GUID in VB.Net
Here is an example of how to generate a UUID in VB.Net:
vbnet
Imports System
Module Module1
Sub Main()
Dim guidValue As Guid = Guid.NewGuid()
Console.WriteLine(guidValue.ToString())
End Sub
End ModuleThis program uses the Guid.NewGuid() method to generate a new GUID and then displays its value using the Console.WriteLine() method.
Explanation
- Line #1 imports the
Systemnamespace, which contains fundamental classes and base classes that define commonly-used value and reference data types, events and event handlers, interfaces, attributes, and processing exceptions. - Line #2 defines a module named
Module1. A module is a container for procedures and functions. - Line #3 defines the
Mainsubroutine, which is the entry point of the program. - Line #4 declares a variable named
guidValueof typeGuidand assigns it a new GUID value generated by calling theGuid.NewGuid()method. - Line #5 uses the
Console.WriteLine()method to write the string representation of theguidValuevariable to the standard output stream.
How to convert from a string to GUID in VB.Net
This program defines a string representation of a GUID and then uses the Guid constructor to create a new Guid value from the string. The value of the Guid is then displayed using the Console.WriteLine():
vbnet
Imports System
Module Module1
Sub Main()
Dim guidString As String = "0a6b6ead-9517-4fbc-ad41-e8f4c00d1c09"
Dim guidValue As Guid = New Guid(guidString)
Console.WriteLine(guidValue.ToString())
End Sub
End ModuleExplanation
- Line #1 imports the
Systemnamespace, which contains fundamental classes and base classes that define commonly-used value and reference data types, events and event handlers, interfaces, attributes, and processing exceptions. - Line #2 defines a module named
Module1. A module is a container for procedures and functions. - Line #3 defines the
Mainsubroutine, which is the entry point of the program. - Line #4 declares a variable named
guidStringof typeStringand assigns it the value"0a6b6ead-9517-4fbc-ad41-e8f4c00d1c09", which is a string representation of a GUID. - Line #5 declares a variable named
guidValueof typeGuidand assigns it a new GUID value created by calling the Guid constructor with theguidStringvariable as an argument. - Line #6 uses the
Console.WriteLine()method to write the string representation of theguidValuevariable to the standard output stream.