I’m a bit confused with the Explicit enumeration conversions what does that really mean..
I’m trying to use a Enumeration to do a pretty cool expected behavior and act as a nice magic number replacement at the same time.. kill two bird with one stone.
I got something like this.
public enum Size {Bare, Fixed, VariableByte, VariableShort};
The beauty in the expected behavior is in this protocol i’m working with.
If I just send a packet without any opcode command it’s called a Bare packet meaning the buffer size is just payload similar to
new byte[payloadSize]; but [payloadSize+Bare] would be equivalent
byte[] buffer = new byte[payloadSize+Bare]; //Bare = 0
If the packet is Fixed.. it contains a OpCode which has to be appended in front. so it’s
new byte[1+payload];
byte[] buffer = new byte[payloadSize+Fixed] //Fixed of course = 1.
VariableByte contains a opcode in front and a byte to represent the size of the packet. This is for packets in the 255 size range etc.. which is
new byte[2+payload];
VariableShort is same as above except the packet size can be a word 65535 payload. which is
I may be causing myself problems in the future if the protocol ever changes then I may have to assign values to the enum in the future to fix problems but for now it’s a valid enum which starts at index=0 [after googling I heard that if you don’t start a enum at the default value of 0] it’s not a correct valid enum.. lol idk what that means.. probably puts stress on enum operation.
Anyways back to my question.
as I already know I can do such as
int Ret = (int)Size.Bare;
or
int Ret = Convert.ToInt32(Size.Bare);
byte[] packet = new byte[payloadSize+Ret]
or best.
byte[] packet = new byte[payloadSize+(int)Size.Bare]
I’m trying to avoid as much amount of lines of coding as possible maybe even increase performance.
I was reading MSDN and found you can do this.. I haven’t really discovered it’s purpose but I am predicting what it may mean..
public enum Size : int {Bare, Fixed, VariableByte, VariableShort};
I assume this would be possible
byte[] packet = new byte[payloadSize+Size.Fixed];
byte[] packet = new byte[payloadSize+Size.VariableShort];
etc…
But unfortunately it isn’t and I get this old error
Operator ‘+’ cannot be applied to operands of type ‘int’ and Packet.Size
so I wonder what does it really mean to put a byte casting in front of a Enum?
As it really doesn’t make me do any cool things like I attempted above.
Does it just limit it to be casted to byte only? or something or it’s plain old just for looks?
Doesn’t seem to limit anything.
byte[] buffer = new byte[payloadZie+(byte)Size.Fixed];
Still works and of course int != byte.
Just wondering whats this could mean.
Thanks sorry if I ask too much questions :\
You guys provide better answers then any forum period.
An enum provides for the switch statements and comparisons where you are comparing logical states or values. (i.e ConnectionState == Connected vs ConnectionState == 1).
What you want here are integer values. You can however write a class with public fields and/or properties to do what you want. Make it static and you will provide the compiler/CLR with a good optimisation as well.
You can use them the same way but now as Sizes.Bare is an integer with value 0 you can use it in an addition.