Microsoft 开发人员网络 > 论坛主页 > Visual C# General > create a constant from two strings...
提出问题提出问题
 

已答复create a constant from two strings...

  • 2009年11月3日 22:54hazz 用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     

    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; 
    
    
    • 已编辑hazz 2009年11月4日 14:56
    • 已编辑hazz 2009年11月4日 14:59
    • 已编辑hazz 2009年11月4日 15:00
    •  

答案

  • 2009年11月4日 15:04David M MortonMVP, 版主用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     已答复

    The literal conversion is "\u0000\u0001"

    case "\u0000\u0001":
        // whatever


    Coding Light - Illuminated Ideas and Algorithms in Software
    Coding Light WikiLinkedInForumsBrowser
  • 2009年11月4日 16:17Rudedog2版主用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     已答复
    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."
    • 已标记为答案hazz 2009年11月4日 16:24
    •  

全部回复

  • 2009年11月3日 23:36Rudedog2版主用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     
    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."
  • 2009年11月4日 2:16Ji.ZhouMSFT, 版主用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     

    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.
  • 2009年11月4日 2:28hazz 用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     
    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
  • 2009年11月4日 15:04David M MortonMVP, 版主用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     已答复

    The literal conversion is "\u0000\u0001"

    case "\u0000\u0001":
        // whatever


    Coding Light - Illuminated Ideas and Algorithms in Software
    Coding Light WikiLinkedInForumsBrowser
  • 2009年11月4日 15:41hazz 用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     包含代码
    Thank you David. I was going to use;
     const string str1 = "\x0\x1";
    
  • 2009年11月4日 16:17Rudedog2版主用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     已答复
    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."
    • 已标记为答案hazz 2009年11月4日 16:24
    •  
  • 2009年11月4日 16:25hazz 用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     

    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