Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: VB7
Expertise: Intermediate
Jul 6, 2002



Building the Right Environment to Support AI, Machine Learning and Deep Learning

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.InteropServices

Module 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 Sub
End Module

Francesco Balena
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date