In Visual Basic .NET, a struct is a user-defined data type that can hold multiple values. The values in a struct can be of different data types, such as integers, strings, or other structs.
To create a struct in Visual Basic .NET, you can use the Structure keyword followed by the name of the struct. Here's an example of a struct that holds a person's name and age:
Structure Person
Dim Name As String
Dim Age As Integer
End Structure
Once you've defined your struct, you can create variables of that type and set their values:
Dim John As Person
John.Name = "John Smith"
John.Age = 30
You can also initialize a struct variable when you declare it:
Dim Bob As New Person With {.Name = "Bob Johnson", .Age = 45}
You can access the values in a struct using the dot syntax:
Console.WriteLine(John.Name & " is " & John.Age & " years old.")
You can also use a struct as a parameter in a function or subroutine:
Sub PrintPerson(p As Person)
Console.WriteLine(p.Name & " is " & p.Age & " years old.")
End Sub
' Usage:
PrintPerson(John)
PrintPerson(Bob)