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

Dani AI

Generated

The examples from and show the basics: you can set structure members directly or provide a constructor. Building on that, focus on whether a Structure is the right tool and watch a few VB.NET pitfalls that cause confusing behavior in real code.

Decide Struct vs Class with a short checklist: it should represent a single logical value, be small (Microsoft’s guideline is about 16 bytes or less), be immutable, and not be boxed frequently. If your Employee type contains a reference (like String) or will be mutated often, a Class is usually the safer choice. (learn.microsoft.com)

A common trap: mutable structs used through properties or APIs behave as copies, so calling a mutating method can modify only a temporary copy. The snippet below shows the surprising result — Increment changes the copy, not the held instance.

Structure Counter
    Public Value As Integer
    Public Sub Increment()
        Value += 1
    End Sub
End Structure

Class Holder
    Public Property C As Counter
End Class

' Using:
Dim h As New Holder()
h.C.Increment()
' h.C.Value is still 0 because h.C returned a copy.

Prefer immutable structures: make fields ReadOnly and set them only from a parameterized constructor; this avoids copy/mutation surprises and matches value-type semantics. Also note that VB does not allow initializing nonshared structure members at declaration — initialize via constructor or assignment after creation. If you need reference semantics, larger payloads, or frequent mutation, use a Class. (learn.microsoft.com)

Further reading: Microsoft’s guidance on choosing between classes and structs and the VB Structures documentation are good next steps. (learn.microsoft.com)

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.