Dear Visitore of this Question I m New to VB.net
i have declared a structure in vb.net and i want to give values to its members how could i can do that
help me plz

Structure Employee
dim inta as integer
dim strs as string
end structure

how to intialise the values to inta and strs

Recommended Answers

All 3 Replies

There are a few methods for Structures, but there is more support for Classes. The easiest is to just assign the elements one at a time.

Dim empTest As Employee

empTest.inta = 1
empTest.strs = "Bill White"

To create an initializer, you need to create a Sub New and pass the initialization to it. You also have to declare the variable using the New keyword.

Structure Employee
    Public inta As Integer
    Public strs As String

    Public Sub New(intNew as Integer, strNew As String)
        inta = intNew
        strs = strNew
    End Sub
End Structure

Dim empTest As Employee = New Employee(1, "Bill White")

If you are wanting this kind of construction, it might be time to start considering Classes instead.

That line could be instead:

Dim empTest As New Employee(1, "Bill White")

it is so simple create an instance ot the structure and give the values to the instance
dim emp as employee
emp.inta=10
emp.strs="John"

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.