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 406127
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T17:27:58+00:00 2026-05-12T17:27:58+00:00

I am storing data with different formats and lengths. I have a class hierarchy

  • 0

I am storing data with different formats and lengths. I have a class hierarchy to represent this:

abstract class BaseDataFormat{ 
    abstract void InitalizeFromBytes(byte [] );
}

class DataFormat1 : BaseDataFormat{...}  // data stored in 3 bytes
class DataFormat2 : BaseDataFormat{...} /// data stored in 4 bytes

When I am reading my data (say from a byte []), I need to know the length (# of bytes) to read and create the corresponding type appropriately. DataFormat1 and DataFormat2 have different lengths, so how do I get this information at runtime? ie.

Fcn<DATAFORMATTYPE>(...)
 where DATAFORMATTYPE: BaseDataFormat, new();
{

DATAFORMATTYPE tmp = new DATAFORMATTYPE();
tmp.InitalizeFromBytes(ReadFromByteBuffer( ... someLength));

}

How do I encode the proper # of bytes to read based on the DATAFORMATTYPE? The length for each feels like it should be a static property of the data format type, but static properties cant be overriden by the derived class, so I am not sure how to do this.

The length can be coded as an instance property, but this seems like it should be knowledge encoded at the class level(ie. static). Is there a design pattern to solve this?

Thanks

  • 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-12T17:27:58+00:00Added an answer on May 12, 2026 at 5:27 pm

    Perhaps have a property in BaseDataFormat called DataLength. To force all inheritors to set a value, take the length in at BaseDataFormat‘s constructor, and then set that property to the data length.

    Example:

    abstract class BaseDataFormat
    { 
        BaseDataFormat(int dataLength)
        {
            DataLength = dataLength;
        }
    
        public int DataLength { get; private set; }
        abstract void InitalizeFromBytes(byte [] );
    }
    
    class DataFormat1 : BaseDataFormat
    {
        public DataFormat1() : base(3)
        {
            // ...
        }
    }
    

    Granted, it’s not at the static level, but it is something that is forced for all inheritors.

    Another way is as how VirtualBlackFox suggested, decorate the class with an attribute. The only problem is that AFAIK you can’t force attributes onto a class, like with abstract members.

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

Sidebar

Related Questions

I'm trying to figure out storage requirements for different storage engines. I have this
I have done this previously but in a different way. I am trying to
I wrote up a quick memory reader class that emulates the same functions as
What would the best way of unpacking a python string into fields I have
I am using Named Pipes to transfer data from a client (C++) to a
I need to read a line like this from a file: 5 Chair 12.49
In ASP.net web service if the above isn't specified , what is the response
I am developing a web application that calls web services developed by a third
Ok, so I've got an Open Source Java client/server program that uses packets to

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.