Click here to Skip to main content
15,399,959 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have an char array array.
char[] ReadBuffers = new char[10];

i will get commads though serial port as a string.

string HDR_CMD = "0x00010001"
string DBCMD = "0x00020002"
string DBBdta_CMD = "0x00030003"
string ADDR_TBL_CMD = "0x00040004"
string RESEND_CMD = "0x00050005"

i want to put conditions.

i will get command through serial port only that command should read through char buffer.
and then write through serial port.

how can we do this.

please help me.
OriginalGriff 17-Nov-13 2:44am
What have you tried?
Where are you stuck?
What part of this do you need help with?
And...are you sure that your command values are strings? Because those look line integers to me...
MCY 17-Nov-13 4:17am
you can use "if". You can also use "switch". You can even use "#if". However you like, you can do it.
Ron Beyer 17-Nov-13 19:53pm
You cannot use #if for conditionally running code, only conditionally compiling it...
Bernhard Hiller 18-Nov-13 3:41am
string HDR_CMD = "0x00010001"
Are you sure? The value looks rather like an Int32, in hexadecimal format - not like a string...
bunty swapnil 18-Nov-13 3:57am
yes command was uint32 but i am not able to convert from uint32 to char array so i made string i.e that command mentioned in double quotes.
bunty swapnil 18-Nov-13 3:59am

1 solution

You can use either, or a non-control flow structure approach. It depends on your solution. For example, what are you going to do when the command comes, will commands ever be added, etc. What I am proposing below may be unnecessary -- just over-engineering.

This is something off the top of my head, but it'll give you an idea. You don't have to use an ICommand (it can be anything including an Action<t> or Func<t>).

static class CommandRepository {
   Dictionary<string, ICommand> commands;

   static CommandRepository() {
      commands = new Dictionary<string, ICommand>(5);
      commands.Add("0x00010001", new ...);
      commands.Add("0x00020002", new ...);

   public static ICommand GetCommand(string cmdStr) {
      if (commands.ContainsKey(cmdStr))
         return commands[cmdStr];
         return null;

You would then use this class in your code.
void ExecuteCommand(string cmdStr) {
   var cmd = CommandRepository.GetCommand(cmdStr);

   if (cmd != null)
      throw new Exception(); // do something when invalid cmdStr

You can take this further if you plan on adding commands a lot or perhaps dynamically. By using an IoC container like Unity. You can do your own research on that, but you could through a configuration file register multiple commands with different registration names and use that to resolve the objects instead of using a Dictionary.
Suvabrata Roy 17-Nov-13 23:41pm
My vote 5...
tgrt 18-Nov-13 10:44am
Thank you!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900