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

  • Home
  • SEARCH
  • 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 6212581
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T06:32:33+00:00 2026-05-24T06:32:33+00:00

I save data using binary serialization. Now I have changed a field in the

  • 0

I save data using binary serialization. Now I have changed a field in the program from Int32 to a struct. But I still want to save the field as Int32 to be backward compatible. How do I do that?

background information
My program is ported from Delphi and uses a lot of arrays. The problem is that the lists in Delphi start counting from 1, vilkort has made it difficult to keep track of the indexes when I am now developing in C#.So I use a struct to make it easier to deal with the 1-based index of the 0-based arrays until I have corrected the whole program.

The code

    public void Save()
    {
            using (var writer = File.Open(Path, FileMode.Create))
            {
                var serializer = new BinaryFormatter();
                serializer.Serialize(writer, _data);
            }
    }

    private void Load()
    {
        using (var reader = File.Open(Path, FileMode.Open))
        {
                var deserializer = new BinaryFormatter();
                _data = (Foo) deserializer.Deserialize(reader);
        }
    }

public struct Indexer
{
    //0-baserat index
    private int _index;

    public Indexer(int index)
    {
        _index = index;
    }

    static public implicit operator Indexer(int index)
    {
        return new Indexer(index);
    }

    static public implicit operator int(Indexer indexer)
    {
        return indexer._index;
    }

    public override string ToString()
    {
        return (_index + 1).ToString();
    }

    ...
  }
  • 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-24T06:32:33+00:00Added an answer on May 24, 2026 at 6:32 am

    You can add a legacy field of Int32 type, and also have the struct field. Make a method with the [OnDeserialized] attribute (and the correct arguments) and initialize the struct field from the int field under some circumstances, for example that the int field is nonzero and the struct has default value after deserialization, indicating that the data is from the older format.

    private int intValue;                // Legacy field
    private SomeStruct structValue;      // New field
    
    [OnDeserialized]
    private void OnDeserialized(StreamingContext context)
    {
      if (intValue != 0)
      {
          // Old format, initialize struct with int value.
          structValue = new SomeStruct(intValue);
      }
    }
    

    This is not exactly elegant and there are other ways, but if you don’t mind keeping two fields this is one of the simpler ways. Note that the above code assumes that zero indicates a “missing” value for the old int field.

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

Sidebar

Related Questions

I'm using serialization for save feature in my application. But when the data is
I have a problem using boost serialization using binary archives. It works when using
I would like to save data in cookies (user name, email address, etc...) but
I am trying to save data to a database on a button push, but
I have three buttons and need to save some data. I have a idea,
We have the requirement to take a form submission and save some data, then
I'd like to save some simple text data to a file from my Silverlight
i've got some binary data which i want to save as an image. When
I have a set of classes I wish to serialize the data from. There
I am trying to scrape an html table and save its data in a

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.