【发布时间】:2015-06-09 11:21:55
【问题描述】:
我对下面的代码有疑问。我有一个 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;
【问题讨论】:
-
在
switch的行上放置一个断点并检查变量propType。它不会是您期望的值。 -
那么我可以假设您的 propType 不是“ID”。
-
@AntP:阅读问题。代码调用
.ToLower;这不是外壳问题。 -
@AntP 他在那里有一个 ToLower()。
-
可能是 currentProperty 中没有属性类型“id”。您可以在调用开关之前将属性类型打印到控制台以查看正在切换的值。
标签: c# switch-statement case