CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 2 FirstFirst 12
Results 16 to 30 of 30
  1. #16
    Join Date
    Aug 2010
    Posts
    13

    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

  2. #17
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Why wont my enum be what it is ? I have to cast it to get the number

    Looks clean enough.

    I'd get rid of the 255 and 0x00 magic numbers though.

  3. #18
    Join Date
    Jun 2008
    Posts
    2,477

    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.

  4. #19
    Join Date
    Aug 2010
    Posts
    13

    Re: Why wont my enum be what it is ? I have to cast it to get the number

    Quote Originally Posted by BigEd781 View Post
    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.

  5. #20
    Join Date
    Aug 2010
    Posts
    13

    Re: Why wont my enum be what it is ? I have to cast it to get the number

    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

  6. #21
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Why wont my enum be what it is ? I have to cast it to get the number

    Quote Originally Posted by zonemikel View Post
    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?

  7. #22
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Why wont my enum be what it is ? I have to cast it to get the number

    Quote Originally Posted by zonemikel View Post
    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 )

  8. #23
    Join Date
    Jun 2008
    Posts
    2,477

    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?

  9. #24
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Why wont my enum be what it is ? I have to cast it to get the number

    Quote Originally Posted by BigEd781 View Post
    How about Byte.MaxValue and Byte.MinValue instead?
    Even better.

  10. #25

    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.

  11. #26
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Why wont my enum be what it is ? I have to cast it to get the number

    Quote Originally Posted by ramkumarradhakrishnan View Post
    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.

  12. #27

    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.

  13. #28
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Why wont my enum be what it is ? I have to cast it to get the number

    Quote Originally Posted by ramkumarradhakrishnan View Post
    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!
    }

  14. #29

    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..

  15. #30
    Join Date
    Jan 2010
    Posts
    1,133

    Re: Why wont my enum be what it is ? I have to cast it to get the number

    Quote Originally Posted by ramkumarradhakrishnan View Post
    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 View Post
    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 View Post
    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 View Post
    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.
    Last edited by TheGreatCthulhu; August 24th, 2010 at 05:16 PM.

Page 2 of 2 FirstFirst 12

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured