Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have the following structure:
VB
Public Structure Aliment
              Public Alino As Integer
              <VBFixedString(83)> Public NomUS As String
              <VBFixedString(50)> Public NomFR As String
              <VBFixedString(24)> Public Categ As String
              Public Paramali() As Double
        End Structure

Then I have an array of this structure

Public TousALI() As Aliment

The array Paramali() is redimensioned:

ReDim Preserve TousALI.Paramali(80)

The data stored in the TousALI array, comes from a .txt file.
I load it, display it in a datagridview and it works perfectly.

But now I want to store the data in a random access file:

FileOpen(1, FileAliRand, OpenMode.Random, OpenAccess.Write, , 813)

But the next line :
FilePut(1, TousALI(I)) gives me an error: Incorrect record length.

the record lenght, 813 is the total number of bytes.

How to calculate the actual number of bytes of my array ?

Thanks for help

What I have tried:

I have tried many different length, but it doesn't work
Posted
Updated 28-Apr-17 18:23pm
v2
Comments
NightWizzard 29-Apr-17 3:39am    
Well, I don't know how you came to a total length of 813? If you calculate the byte count in memory, an Integer will take 2 bytes and each double 8 bytes. In your example above this will give a total of 829 bytes in memory: 640 for 80 doubles, 2 for the int and for each string it's length plus 10 bytes overhead. If you're writing a binary file, the record length should be 829. If you're writig a text file, the numeric parameters have to be converted to string - in this case the string length for each numerical value can differ. For example: setting the Integer to 1000 still needs 2 bytes in memory for the Integer but the string will have a length of 4 plus the 10 bytes overhead = 14!!
In VB.NET you also can try to use either the length property:
size = Aliment.Length
or this method:
Marshal.SizeOf(Aliment)
Hope this will help you to solve the problem - good luck!
Richard MacCutchan 29-Apr-17 3:56am    
Integers take 4 bytes, or 8 in 64-bit mode.
NightWizzard 29-Apr-17 4:24am    
Thanks Richard - you're right. I confused this with old VB6/VBA - maybe I'm doing this job too much years. For Michel here the link to the .NET datatype summarize:
https://msdn.microsoft.com/en-us/library/s3f49ktz.aspx
Maciej Los 30-Apr-17 5:20am    
If you're using VB.NET, you should use BinaryFormater instead of FileOpen method, etc.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900