devxlogo

Determine the size of a structure

Determine the size of a structure

Unlike previous Visual Basic versions, under VB.NET you can’t use the Len function to calculate the length of a Structure or a class. However, it is easy to get this information with the SizeOf static method of the System.Runtime.InteropServices.Mashal class, as in:

Imports System.Runtime.InteropServicesModule Module1    ' Notice that this structure contains 2 padding bytes between    ' the first two elements    Structure MyStruct        Public el1 As Short        Public el2 As Integer        Public el3 As String    End Structure    Sub Main(ByVal args() As String)        Dim struct As mystruct        Console.WriteLine(Marshal.SizeOf(struct))   ' => 12    End SubEnd Module

devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist