How to Determine the Size of a Structure

Posted on 2005-05-16
Last Modified: 2010-04-23
Hello All,

I need a way to determine the size of a structure, in bytes, so I can use the result in a read statement.

My structure looks something like this

Public Structure MyRec
  Dim MyObj as MyClass
End Structure

I then have a sub in which I want to read MyRec blocks from a file, like this

Private Sub ReadFile()
        Dim objFile As System.IO.File
        Dim fs As System.IO.FileStream
        Dim objMyRec As New MyRec
        fs = objFile.OpenRead(strFileName)
        fs.Read(objMyRec, 0, ???????????????)
End Sub

As I understand it, I need to put the size of what I am reading where the ???????? is above.  If this is right, can someone please tell me how to do this.

Question by:quiTech
    LVL 85

    Assisted Solution

    by:Mike Tomlinson
    LVL 13

    Accepted Solution

    you can get the size of a structure through System.Runtime.InteropServices.Marshal.SizeOf(...)

    However, I don't think you can save an object like that -- you'll need to serialize it, something like this:

    Add this right on the line above the Class definition:

    then to save:
    Dim myFileStream As New  FileStream("C:\MyFileName.dat",  FileMode.CreateNew)
    Dim MyFormatter As New BinaryFormatter()
    MyFormatter.Serialize(myFileStream, MyObject)

    to read:
    Dim myFileStream As New  FileStream("C:\MyFileName.dat",  FileMode.Open)
    Dim MyFormatter As New BinaryFormatter()
    Dim GenericObject As Object
    GenericObject =  MyFormatter.Deserialize(myFileStream)
    Dim MyObject As MyObjectType
    MyObject = CType(GenericObject, MyObjectType)

    (from )

    Does that help?

    Author Comment

    thanks, softplus!  

    What happens if the file is empty (or EOF reached) on the read?  Is there a way I can check for that?
    LVL 13

    Expert Comment

    If your file is damaged (i.e. empty, missing parts, etc) Deserialize will throw an exception (as always, put your code sections between try...catch wrappers) :)

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Find Ransomware Secrets With All-Source Analysis

    Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

    It’s quite interesting for me as I worked with Excel using for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
    Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
    how to add IIS SMTP to handle application/Scanner relays into office 365.
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    9 Experts available now in Live!

    Get 1:1 Help Now