开关不输入大小写但值匹配
Switch not entering cases but values match
我对下面的代码有疑问。我有一个 switch 语句,它没有进入它的任何情况。假设 propType
的值为 "ID",开关应将其与大小写 "id" 相匹配,但它没有这样做。我做错了什么?
case "button":
Button ctrlButton = new Button();
for (int j = 0; j < ctrlProperties.Length; j++)
{
string currentProperty = ctrlProperties[j];
int propDelimiterPos = currentProperty.IndexOf(propDelimiter);
string propType = currentProperty.Substring(0, propDelimiterPos);
string propValue = currentProperty.Substring(propDelimiterPos + 2);
switch (propType.ToLower())
{
#region PROPERTY PARAMETERS LEVEL
case "text":
ctrlButton.Text = propValue;
break;
case "font":
int fontDelimiterPos = propValue.IndexOf(pntDelimiter);
string fontName = propValue.Substring(0, fontDelimiterPos);
string fntSize = propValue.Substring(fontDelimiterPos + 1);
int fontSize = int.Parse(fntSize);
ctrlButton.Font = new Font(fontName, fontSize);
break;
case "bounds":
string[] boundsValues = propValue.Split(pntDelimiter);
Rectangle bounds = new Rectangle(
new Point(Convert.ToInt32(boundsValues[0]), Convert.ToInt32(boundsValues[1])),
new Size(Convert.ToInt32(boundsValues[2]), Convert.ToInt32(boundsValues[3])));
ctrlButton.Bounds = bounds;
break;
case "id":
ctrlButton.Name = propValue;
break;
#endregion
}
this.Controls.Add(ctrlButton);
ctrlButton.Show();
}
break;
这可能是由于您的字符串中有额外的 space 试试这个 propType.Trim().ToLower()
我对下面的代码有疑问。我有一个 switch 语句,它没有进入它的任何情况。假设 propType
的值为 "ID",开关应将其与大小写 "id" 相匹配,但它没有这样做。我做错了什么?
case "button":
Button ctrlButton = new Button();
for (int j = 0; j < ctrlProperties.Length; j++)
{
string currentProperty = ctrlProperties[j];
int propDelimiterPos = currentProperty.IndexOf(propDelimiter);
string propType = currentProperty.Substring(0, propDelimiterPos);
string propValue = currentProperty.Substring(propDelimiterPos + 2);
switch (propType.ToLower())
{
#region PROPERTY PARAMETERS LEVEL
case "text":
ctrlButton.Text = propValue;
break;
case "font":
int fontDelimiterPos = propValue.IndexOf(pntDelimiter);
string fontName = propValue.Substring(0, fontDelimiterPos);
string fntSize = propValue.Substring(fontDelimiterPos + 1);
int fontSize = int.Parse(fntSize);
ctrlButton.Font = new Font(fontName, fontSize);
break;
case "bounds":
string[] boundsValues = propValue.Split(pntDelimiter);
Rectangle bounds = new Rectangle(
new Point(Convert.ToInt32(boundsValues[0]), Convert.ToInt32(boundsValues[1])),
new Size(Convert.ToInt32(boundsValues[2]), Convert.ToInt32(boundsValues[3])));
ctrlButton.Bounds = bounds;
break;
case "id":
ctrlButton.Name = propValue;
break;
#endregion
}
this.Controls.Add(ctrlButton);
ctrlButton.Show();
}
break;
这可能是由于您的字符串中有额外的 space 试试这个 propType.Trim().ToLower()