|
Determine the size of a structureAverage Rating: 3.2/5 | Rate this item | 5 users have rated this item.
|
Tip formerly from VB2TheMax
Expertise: Intermediate
Language: VB7
July 6, 2002
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
If you have a hot tip and we publish it, we'll pay you. However, due to accounting overhead we no longer pay $10 for a single tip submission. You must accumulate 10 acceptable tips to receive payment. Be sure to include a clear explanation of what the technique does and why it's useful. If it includes code, limit it to 20 lines if possible. Submit your tip here.
|