Microsoft Developer Network > Página principal de foros > Visual C# General > create a constant from two strings...
Formular una preguntaFormular una pregunta
 

Respondidacreate a constant from two strings...

  • martes, 03 de noviembre de 2009 22:54hazz Medallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuario
     

    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; 
    
    
    • Editadohazz miércoles, 04 de noviembre de 2009 14:56
    • Editadohazz miércoles, 04 de noviembre de 2009 14:59
    • Editadohazz miércoles, 04 de noviembre de 2009 15:00
    •  

Respuestas

  • miércoles, 04 de noviembre de 2009 15:04David M MortonMVP, ModeradorMedallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuario
     Respondida

    The literal conversion is "\u0000\u0001"

    case "\u0000\u0001":
        // whatever


    Coding Light - Illuminated Ideas and Algorithms in Software
    Coding Light WikiLinkedInForumsBrowser
  • miércoles, 04 de noviembre de 2009 16:17Rudedog2ModeradorMedallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuario
     Respondida
    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."
    • Marcado como respuestahazz miércoles, 04 de noviembre de 2009 16:24
    •  

Todas las respuestas

  • martes, 03 de noviembre de 2009 23:36Rudedog2ModeradorMedallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuario
     
    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."
  • miércoles, 04 de noviembre de 2009 2:16Ji.ZhouMSFT, ModeradorMedallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuario
     

    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.
  • miércoles, 04 de noviembre de 2009 2:28hazz Medallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuario
     
    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
  • miércoles, 04 de noviembre de 2009 15:04David M MortonMVP, ModeradorMedallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuario
     Respondida

    The literal conversion is "\u0000\u0001"

    case "\u0000\u0001":
        // whatever


    Coding Light - Illuminated Ideas and Algorithms in Software
    Coding Light WikiLinkedInForumsBrowser
  • miércoles, 04 de noviembre de 2009 15:41hazz Medallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuario
     Tiene código
    Thank you David. I was going to use;
     const string str1 = "\x0\x1";
    
  • miércoles, 04 de noviembre de 2009 16:17Rudedog2ModeradorMedallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuario
     Respondida
    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."
    • Marcado como respuestahazz miércoles, 04 de noviembre de 2009 16:24
    •  
  • miércoles, 04 de noviembre de 2009 16:25hazz Medallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuario
     

    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