Re: Why wont my enum be what it is ? I have to cast it to get the number
Well this is what I did .. i might look into a struct though ..
Code:
public class Dw // dont write
{
// basic defines
public const int RX_PacketSZ = 15; // rx packet size 15 for 16
public const int TX_PacketSZ = 15; // packet size we transmit
public const string RXHeader = "11"; // receiverID,senderID
// indexes of rx packet hence 'i'
public const int RXi_ServerID = 0;
public const int RXi_JinxID = 1;
public const int RXi_Flags = 2;
public const int RXi_Number = 3;
// indexes of tx packet hence 'i'
public const int TXi_JinxID = 0;
public const int TXi_ServerID = 1;
public const int TXi_Flags = 2;
public const int TXi_Number = 3;
public const int TXi_Direction = 4;
public const int TXi_Speed = 5;
public const int TXi_Reserved = 6;
// masks for the flags
public const byte Mask_Battery = 0x01;
public const byte Mask_Accel = 0x02;
public const byte Mask_Sonar = 0x04;
public const byte Mask_Line = 0x08;
public const byte Mask_Gps = 0x10;
public const byte Mask_Move = 0x20;
public const byte Mask_Test = 0x33;
// bytes for movement
public const byte Move_Forward = 0x01;
public const byte Move_Backward = 0x02;
public const byte Move_Left = 0x03;
public const byte Move_Right = 0x04;
}
then i'm using it like so
Code:
public void sendTXPacket()
{
if (_serialPort != null && _serialPort.IsOpen)
{
if(currentTXPacket[Dw.TXi_Number] < 255){
currentTXPacket[Dw.TXi_Number]++;
_serialPort.Write(currentTXPacket,0,Dw.TX_PacketSZ);
}else{
currentTXPacket[Dw.TXi_Number] = 0x00;
currentTXPacket[Dw.TXi_Number]++;
_serialPort.Write(currentTXPacket, 0, Dw.TX_PacketSZ);
}
}
else { Console.WriteLine("serial port not ready"); }
}
seems to be working pretty well ... no casting at least
Re: Why wont my enum be what it is ? I have to cast it to get the number
Looks clean enough. :thumb:
I'd get rid of the 255 and 0x00 magic numbers though.
Re: Why wont my enum be what it is ? I have to cast it to get the number
I wouldn't name my class "Dw" for "Don't write" though. Name it something descriptive, the fact that it contains only read only fields is obvious.
Re: Why wont my enum be what it is ? I have to cast it to get the number
Quote:
Originally Posted by
BigEd781
I wouldn't name my class "Dw" for "Don't write" though. Name it something descriptive, the fact that it contains only read only fields is obvious.
lol, i put DW for dont write meaning i dont have to write stuff .. like a "dont write" class so you dont have to write (int)enum.flag or 0x0003 .. but ya its a lousy name.
Re: Why wont my enum be what it is ? I have to cast it to get the number
Quote:
I'd get rid of the 255 and 0x00 magic numbers though.
for the 255 is the max size of a unsigned byte and the other is just a 0 so it goes back to zero. I need the packet number to go back to zero after it no longer fits in a byte
Re: Why wont my enum be what it is ? I have to cast it to get the number
Quote:
Originally Posted by
zonemikel
lol, i put DW for dont write meaning i dont have to write stuff .. like a "dont write" class so you dont have to write (int)enum.flag or 0x0003 .. but ya its a lousy name.
Right, see how confusing that is? :)
Re: Why wont my enum be what it is ? I have to cast it to get the number
Quote:
Originally Posted by
zonemikel
for the 255 is the max size of a unsigned byte and the other is just a 0 so it goes back to zero. I need the packet number to go back to zero after it no longer fits in a byte
Yep. And that's the reason not to use magic numbers - because they require explanation.
Instead use something like:
Code:
const int MaxByteSize = 255;
if( currentTXPacket[Dw.TXi_Number] < MaxByteSize )
Re: Why wont my enum be what it is ? I have to cast it to get the number
How about Byte.MaxValue and Byte.MinValue instead?
Re: Why wont my enum be what it is ? I have to cast it to get the number
Quote:
Originally Posted by
BigEd781
How about Byte.MaxValue and Byte.MinValue instead?
Even better. :thumb:
Re: Why wont my enum be what it is ? I have to cast it to get the number
zonemikel,
First of all enums are treated as primitive data type in .net. If yo see the intellisense when yo type txMask.battery it will have the return type as txMask only but yo need to return a byte value in that case yo need to cast that one.
Re: Why wont my enum be what it is ? I have to cast it to get the number
Quote:
Originally Posted by
ramkumarradhakrishnan
zonemikel,
First of all enums are treated as primitive data type in .net. If yo see the intellisense when yo type txMask.battery it will have the return type as txMask only but yo need to return a byte value in that case yo need to cast that one.
There is no such thing as a "primitive" in C# like there is in Java. There are value types and reference types.
Re: Why wont my enum be what it is ? I have to cast it to get the number
Ya everything is class but wen yo see the enum its a value type .sorry for wrong ifo...since its a value type yo need to change the value to its specific so y casting is required.
Re: Why wont my enum be what it is ? I have to cast it to get the number
Quote:
Originally Posted by
ramkumarradhakrishnan
Ya everything is class but wen yo see the enum its a value type .sorry for wrong ifo...since its a value type yo need to change the value to its specific so y casting is required.
But again, casting is not required "because it is a value type". A cast is required because it is *not* the underlying integral type, an enum is a type unto itself. It is a strongly typed object and this concept prevents you from doing things like this (which you can do in C and C++):
Code:
enum MyEnum
{
ValA = 0
ValB = 1
}
void SomeFunction( MyEnum value )
{
// do stuff with 'value'
}
void main( ... )
{
SomeFunction( 12 ); // Oops!
}
Re: Why wont my enum be what it is ? I have to cast it to get the number
Hi BigEd781,
But when see in C or C++ the return type is integer or what ever datatype yo given but here the return type is te enum which yo created with that your not going to do anything yo need that integer stored in that particular enum type so casting is required.
If yo see apart from real time objects some things need to take up in mind in any language it is because it was designed by some other and its thier idea and your supposed to follow that.If i could designed .net i wuld certainely followed your idea..
Re: Why wont my enum be what it is ? I have to cast it to get the number
Quote:
Originally Posted by
ramkumarradhakrishnan
Hi BigEd781,
But when see in C or C++ the return type is integer or what ever datatype yo given but here the return type is te enum which yo created with that your not going to do anything yo need that integer stored in that particular enum type so casting is required.
If you're saying that casting is required every time you need to use the defined enum, then you're wrong - you can use it as you would any other type, and you need to cast only if you need to use the actual numerical values, but when enums are properly used, this is not so often the case.
Quote:
Originally Posted by
ramkumarradhakrishnan
If yo see apart from real time objects some things need to take up in mind in any language it is because it was designed by some other and its thier idea and your supposed to follow that.
The idea that enums should be treated as full-fledged types is not some random idea of the designers of C#, it has its roots in OO principles, and best programing practices accumulated with years of experience. For example, it is true that in C/C++ you have, in a way, a greater degree of flexibility in that you don't have to cast, but this very same freedom proved to be a cause of a bunch of buggy programs, and all this just because the language allowed developers to use enums in a wrong and potentially unsafe way, without any kind of warning.
Quote:
Originally Posted by
ramkumarradhakrishnan
If i could designed .net i wuld certainely followed your idea..
Well, his idea is that he likes things just as they are, as he said before, in an earlier post in this same thread [emphasis added by me]:
Quote:
Originally Posted by
BigEd781
An enumerated value is *not* it's underlying integral type, it is a true type. That means that there is no implicit conversion back and forth, and that is a good thing, trust me.