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.
Here is an example of how to generate a UUID in VB.Net:
- Imports System
- Module Module1
- Sub Main()
- Dim guidValue As Guid = Guid.NewGuid()
- Console.WriteLine(guidValue.ToString())
- End Sub
- End Module
This 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 System namespace, 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 Main subroutine, which is the entry point of the program.
- Line #4 declares a variable named guidValue of type Guid and assigns it a new GUID value generated by calling the Guid.NewGuid() method.
- Line #5 uses the Console.WriteLine() method to write the string representation of the guidValue variable 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()
- 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 Module
Explanation
- Line #1 imports the System namespace, 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 Main subroutine, which is the entry point of the program.
- Line #4 declares a variable named guidString of type String and assigns it the value "0a6b6ead-9517-4fbc-ad41-e8f4c00d1c09", which is a string representation of a GUID.
- Line #5 declares a variable named guidValue of type Guid and assigns it a new GUID value created by calling the Guid constructor with the guidString variable as an argument.
- Line #6 uses the Console.WriteLine() method to write the string representation of the guidValue variable to the standard output stream.