Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6776781
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T16:04:11+00:00 2026-05-26T16:04:11+00:00

Can I, or do I have to declare it as a class with it’s

  • 0

Can I, or do I have to declare it as a class with it’s own SaveToStream method?

It is only data, no functions (although I might now add getters & setters)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T16:04:11+00:00Added an answer on May 26, 2026 at 4:04 pm

    suppose you have the following record

    type
      TMyRecord = record
        FirstName: string[100]; // 100 characters max. for First name
        LastName: string[100]; // 100 characters max. for Last name
        Age: Byte;
        DateOfBirth: TDateTime;
      end;
    const
      // if you are using Delphi 2009 and above, 
      // then either change *string[100]* to *AnsiString[100]* or use a different
      // approach to save the string, read bellow
      szMyRecord = SizeOf( TMyRecord ); // storing it will make your code run faster if you write a lot of records
    

    Now, in order to write the above structure to a stream, you need to:

    procedure WriteRecord(
      const ARecord: TMyRecord;
      const AStream: TStream // can be a TMemoryStream, TFileStream, etc.
    );
    begin
      AStream.Write(ARecord, szMyRecord);
    end;
    

    it is important to note that declaring FirstName as “string” will not save the characters in FirstName, you need to declare FirstName as I did “string[100]” or use a special method to write a string field, for example:

    type
      TMyRecordWithVeryLongStrings = record
        LenFirstName: Integer; // we store only the length of the string in this field
        LenLastName: Integer; // same as above
        Age: Byte;
        DateOfBirth: TDateTime;
        FirstName: string; // we will ignore this field when writing, using it for value
        LastName: string; // same as above
      end;
    
    const
      // we are ignoring the last two fields, since the data stored there is only a pointer,
      // then we can safely assume that ( SizeOf( string ) * 2 ) is the offset
      szMyRecordWithVeryLongStrings = SizeOf( TMyRecordWithVeryLongStrings ) - ( SizeOf( string ) * 2 );
    
    // the difference between this method and above is that we first write the record
    // and then the strings
    procedure WriteRecord(
      ARecord: TMyRecordWithVeryLongStrings;
      AStream: TStream // can be a TMemoryStream, TFileStream, etc.
    );
    const szChar = sizeof(char);
    begin
      // ensure the length of first and Last name are stored in "Len + Name" field
      ARecord.LenFirstName := Length( ARecord.FirstName );
      ARecoord.LenLastName := Length( ARecord.Lastname );
      // write the record
      AStream.Write(ARecord, szMyRecordWithVeryLongStrings);
      // write First name value
      AStream.Write(
        Pointer( ARecord.FirstName )^, // value of first name
        szChar * ARecord.LenFirstName
      );
      // repeat as above for last name
      AStream.Write(
        Pointer( ARecord.LastName )^, // value of first name
        szChar * ARecord.LenLastName
      );
    end;
    

    Now, in order to read the “long strings”, you first read the record:

    procedure ReadRecord(
      ARecord: TMyRecordWithVeryLongStrings;
      AStream: TStream
    );
    begin
      AStream.Read(Arecord, szMyRecordWithVeryLongStrings );
      // now read first and last name values which are right after the record in the stream
      AStream.Read(Pointer(ARecord.FirstName)^, szChar * ARecord.LenFirstName );
      AStream.Read(Pointer(ARecord.,LastrName)^, szChar * ARecord.LenLastName );
    end;
    

    I hope it helps (:

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In C++ we can declare a class in a .h file and can have
For object instances we can have their class declare some protocol conformance as in:
You can have different naming convention for class members, static objects, global objects, and
I have a class Person which can have several Homes, each one with one
With the ThreadStatic attribute I can have a static member of a class with
In languages like Java, you can declare a class without a modifier so it
I have a class, and I declare it like this: public class WhereOrCondition<T> :
I have a situaion in which I want to declare a class member function
I have an abstract base class and I want to declare a field or
How can I create two classes that have member pointers to each other's class

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.