Switch not entering cases but values match

321 views Asked by At

I have an issue with the code below. I have a switch statement that isn't entering any of its cases. Say the value of propType is "ID", the switch should match this to case "id" but it isn't doing so. What have I done wrong?

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;
1

There are 1 answers

1
Shrivallabh On BEST ANSWER

This might be due to extra space in your string try this propType.Trim().ToLower()