tags:

views:

319

answers:

6

I've got a wrapper class which encapsulates a piece of information that needs to be transmitted as a byte array.

In that way, the class encapsulates the necessary header (with fields like DATA_LENGTH or MESSAGE_TYPE) into the corresponding byte positions. For that I want to define positions and length in constants, for example:

HEADER_DATA_LENGTH_IX = 0;
HEADER_DATA_LENGTH_LENGTH = 2;

which means DATA_LENGTH starts at 0 and takes two bytes.

but so far I'm struggling with making them constants or static readonly fields. Const cannot be protected, therefore I won't be able to derive a new class and change the constants if a use them, on the other way I might declare new constants in the derived class and the use them.

What will be your approach?

Edit: Sorry, this is .NET (C#)

+1  A: 
protected static final

in java!

Ido
A: 

I wouldn't make these constant because they simply aren't constants. When declaring something as const you should ask yourself: can this change? Your message lengths might change one day, so they are better to be made readonly.

Gerrie Schenck
They can't change, well they might but then the idea is to derive a new class to handle the changes, therefore, for the class life the values will hold, will be constant and are only there to avoid "magic numbers"
Jorge Córdoba
+4  A: 

if you want to change the value of these params in a derived class, you can make them readonly and change them in the c'tor of the derived class

I wouldn't make them const anyhow, because they're not...

oz radiano
If you want to set the value in a derived class, you don't want it to be static, as this can only be set in the static constructor
Patrick McDonald
+4  A: 

The basic difference is when the variable is initialized. 'readonly' is set at initialization, or in the contructor, while 'const' is set at compile time.

I think the big decision is if you want to inherit the class and override the value. If you do, go readonly. Otherwise I don't think it really matters.

readonly C#ref: http://msdn.microsoft.com/en-us/library/acdd6hb7.aspx

const C# ref: http://msdn.microsoft.com/en-us/library/e6w8fe1b.aspx

Jason
A: 

Create an inner class with the constants. The deriving classes can then later override the inner class and change the constants as necessary.

e.g. base class:

public class Stuff
{
  public class HeaderInformation
  {
    public const int HEADER_DATA_LENGTH_IX = 0;
    public const int  HEADER_DATA_LENGTH_LENGTH = 2;
  }
}

Then the derived class can do this:

public class DerivedStuff : Stuff
{
  public new class HeaderInformation : Stuff.HeaderInformation
  {
    public new const int HEADER_DATA_LENGTH_IX = 10;
  }
}

This way, you have flexibility. In DerivedStuff, the HeaderInformation class has all of the constants in the base Stuff.HeaderInformation class, but can change any of them, or keep the ones it has.

Robert C. Barth
+2  A: 

This exact question is answered by the official c# faq on msdn

Brann