create a constant from two strings...
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;
答案
The literal conversion is "\u0000\u0001"
case "\u0000\u0001":
// whatever
Coding Light - Illuminated Ideas and Algorithms in Software
Coding Light Wiki • LinkedIn • ForumsBrowser- 已标记为答案Ji.ZhouMSFT, 版主2009年11月10日 2:17
- 已建议为答案Matthew Watson 2009年11月4日 15:48
- 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
全部回复
- 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." 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 ZhouMSDN 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.- 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
The literal conversion is "\u0000\u0001"
case "\u0000\u0001":
// whatever
Coding Light - Illuminated Ideas and Algorithms in Software
Coding Light Wiki • LinkedIn • ForumsBrowser- 已标记为答案Ji.ZhouMSFT, 版主2009年11月10日 2:17
- 已建议为答案Matthew Watson 2009年11月4日 15:48
- Thank you David. I was going to use;
const string str1 = "\x0\x1";
- 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
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

