How to Determine the Size of a Structure

Posted on 2005-05-16
Medium Priority
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
  • 2
LVL 86

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 400 total points
ID: 14014557
LVL 13

Accepted Solution

softplus earned 1600 total points
ID: 14014586
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 http://www.fawcette.com/reports/vsliveor/2002/09_18_02/hollis/ )

Does that help?

Author Comment

ID: 14021989
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

ID: 14022114
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) :)

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Loops Section Overview

807 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