Visual C# Developer Center > Visual C# Forums > Visual C# General > create a constant from two strings...
Ask a questionAsk a question
 

Answercreate a constant from two strings...

  • Tuesday, November 03, 2009 10:54 PMhazz Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    as per this illustration, http://tinyurl.com/yhofu9k , how do I achieve the requirement of the switch statement requirement of a constant.  so how do I concatenate 2 strings so the compiler will be happy?

    Case Chr(&H0) & Chr(&H1)  is what the vb.net compiler is happy with.

    Thank you,

     switch ("string value") 
    
     { 
    
      case Strings.Chr(0x0).ToString() + Strings.Chr(0x1).ToString(): 
    
              useraddress = RXpacket.SourceAddress; 
    
             break; 
    
    
    • Edited byhazz Wednesday, November 04, 2009 2:56 PM
    • Edited byhazz Wednesday, November 04, 2009 2:59 PM
    • Edited byhazz Wednesday, November 04, 2009 3:00 PM
    •  

Answers

  • Wednesday, November 04, 2009 3:04 PMDavid M MortonMVP, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer

    The literal conversion is "\u0000\u0001"

    case "\u0000\u0001":
        // whatever


    Coding Light - Illuminated Ideas and Algorithms in Software
    Coding Light WikiLinkedInForumsBrowser
  • Wednesday, November 04, 2009 4:17 PMRudedog2ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Thanks, David. 
    I would have posted something similar, but we are having power and connection issues today. 
    Big fire at the local electrical sub-station.


        class Class2
        {
            public static void Method1()
            {
                string testASCII = "\x00" + "\x01";
                switch (testASCII)
                {
                    case ASCII.SOH:
                        //useraddress = RXpacket.SourceAddress;
                        break;
                    case ASCII.ACK:
                        //polltype = Strings.Mid(RXpacket.Pdu, 1, 1) + Strings.Mid(RXpacket.Pdu, 2, 1);
                        break;
                    default:
                        break;
                }
            }
        }
        static class ASCII
        {
            private const string NUL = "\x00";
            private const string _SOH = "\x01";
            private const string _ACK = "\x06";
            public const string SOH = NUL + _SOH;  // Start Of Header
            public const string ACK = NUL + _ACK;  // Acknowledgement
        }


    hazz,
    The following two strings do not evaluate as being equal.

    string str1 = "\x00\x01";
    string str2 = "\u0001";

    Not sure about what it is that you are doing.


    Rudy  =8^D

    Mark the best replies as answers. "Fooling computers since 1971."
    • Marked As Answer byhazz Wednesday, November 04, 2009 4:24 PM
    •  

All Replies

  • Tuesday, November 03, 2009 11:36 PMRudedog2ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    I feel your question is a bit off-topic.  Post the question at tinyURL.

    However, it would appear that your error is probably due to the mismatch of types.
    RXpacket.FType does not appear to be a string in the example.

    You also cannot perform a calculation in the Case statement.
    Get rid of the " + " signs and assign the stuff to a variable.


    Mark the best replies as answers. "Fooling computers since 1971."
  • Wednesday, November 04, 2009 2:16 AMJi.ZhouMSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    Hello Hazz,

     

    "Strings.Chr(0x0).ToString() + Strings.Chr(0x1).ToString()" is not considered as a constant expression because they are values returned from a method Strings.Chr(). The C# compiler thinks the method returned value depends on the parameter we passed in.


    If you know what string is expected in the case clause, why not directly put them there? Something like the following codes should work,
     

    -------------------------------------------------------------------------------------------------------------
                String s="";
                int count=0;
                switch (s)
                {
                   case "a"+"b":
                        count++;
                        break;
                    case "b"+"c":
                        count++;
                        break;
                }
    -------------------------------------------------------------------------------------------------------------

    Have a nice day!
     

    Best regards,
    Ji Zhou

    MSDN Subscriber Support in Forum

    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
  • Wednesday, November 04, 2009 2:28 AMhazz Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Thank you Ji.  While I digest what you just said, I had taken a few other snapshots of a developer in distress. ;-)  http://tinyurl.com/yh4278d
  • Wednesday, November 04, 2009 3:04 PMDavid M MortonMVP, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer

    The literal conversion is "\u0000\u0001"

    case "\u0000\u0001":
        // whatever


    Coding Light - Illuminated Ideas and Algorithms in Software
    Coding Light WikiLinkedInForumsBrowser
  • Wednesday, November 04, 2009 3:41 PMhazz Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Has Code
    Thank you David. I was going to use;
     const string str1 = "\x0\x1";
    
  • Wednesday, November 04, 2009 4:17 PMRudedog2ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Thanks, David. 
    I would have posted something similar, but we are having power and connection issues today. 
    Big fire at the local electrical sub-station.


        class Class2
        {
            public static void Method1()
            {
                string testASCII = "\x00" + "\x01";
                switch (testASCII)
                {
                    case ASCII.SOH:
                        //useraddress = RXpacket.SourceAddress;
                        break;
                    case ASCII.ACK:
                        //polltype = Strings.Mid(RXpacket.Pdu, 1, 1) + Strings.Mid(RXpacket.Pdu, 2, 1);
                        break;
                    default:
                        break;
                }
            }
        }
        static class ASCII
        {
            private const string NUL = "\x00";
            private const string _SOH = "\x01";
            private const string _ACK = "\x06";
            public const string SOH = NUL + _SOH;  // Start Of Header
            public const string ACK = NUL + _ACK;  // Acknowledgement
        }


    hazz,
    The following two strings do not evaluate as being equal.

    string str1 = "\x00\x01";
    string str2 = "\u0001";

    Not sure about what it is that you are doing.


    Rudy  =8^D

    Mark the best replies as answers. "Fooling computers since 1971."
    • Marked As Answer byhazz Wednesday, November 04, 2009 4:24 PM
    •  
  • Wednesday, November 04, 2009 4:25 PMhazz Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    Thank you!  This is a serial rs232 application port from vb.net to c# so now I see what the original author was doing!

    Much appreciated !

    Greg