Enum Question

A

AMP

Hello,
I have the following Enum:

public enum BslCommand : byte
{
BSL_TXPWORD = 0x10,
BSL_TXBLK = 0x12,
BSL_RXBLK = 0x14,
BSL_MERAS = 0x18,
BSL_ECHECK = 0x1C

}


Then I try to use it:


BslCommand cmd = BslCommand.BSL_RXBLK;
q.bslTxRx(cmd, g, 0x00FA, null, blkin);


But I get the folowing Error:

Error 12 Argument '1': cannot convert from 'Daya.MainForm.BslCommand'
to 'byte'


What am I doing wrong?
If I make them const's ints (const int BSL_RXBLK = 0x14;) it works but
the function definition is looking for a byte:

public int bslTxRx(byte cmd, uint addr, ushort len, byte[] blkout,
byte[] blkin)

Just a little confused.

Thanks
Mike
 
J

Jeroen Mostert

AMP said:
Hello,
I have the following Enum:

public enum BslCommand : byte
{
BSL_TXPWORD = 0x10,
BSL_TXBLK = 0x12,
BSL_RXBLK = 0x14,
BSL_MERAS = 0x18,
BSL_ECHECK = 0x1C

}


Then I try to use it:


BslCommand cmd = BslCommand.BSL_RXBLK;
q.bslTxRx(cmd, g, 0x00FA, null, blkin);
May I just say this seems optimized to look maximally ugly and unreadable.
Is this code converted from C?
But I get the folowing Error:

Error 12 Argument '1': cannot convert from 'Daya.MainForm.BslCommand'
to 'byte'
Enums can be converted to and from their underlying types, but a cast is
needed. C# isn't C. You should redeclare the "bslTxRx" method to take this
enum instead of a byte so no casting is necessary in the first place.
What am I doing wrong?
If I make them const's ints (const int BSL_RXBLK = 0x14;) it works but
the function definition is looking for a byte:

public int bslTxRx(byte cmd, uint addr, ushort len, byte[] blkout,
byte[] blkin)
It didn't occur to you to make them "const byte"? That would have solved the
problem too. (But using an enum is more appropriate in this case.)

Incidentally, if this interface does what I think it does (send or receive
data based on a command flag) it's pretty bletcherous indeed. Instead of
taking a mode parameter, this should be split up into multiple methods. I'm
going to assume this isn't going to happen because this is either an
interface to unmanaged code (not shown here) or you're "converting" this
code from C by copy-paste and changing the minimum amount of bits necessary
to get it to work.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top