Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5946143
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T16:46:51+00:00 2026-05-22T16:46:51+00:00

I have large switch statement in which I create UIElements based on input value

  • 0

I have large switch statement in which I create UIElements based on input value from XElement:

public static UIElement CreateElement(XElement element) {
            var name = element.Attribute("Name").Value;
            var text = element.Attribute("Value").Value;
            var width = Convert.ToDouble(element.Attribute("Width").Value);
            var height = Convert.ToDouble(element.Attribute("Height").Value);
            //...
            switch (element.Attribute("Type").Value) {
                case "System.Windows.Forms.Label":
                    return new System.Windows.Controls.Label() {
                        Name = name,
                        Content = text,
                        Width = width,
                        Height = height
                    };
                case "System.Windows.Forms.Button":
                    return new System.Windows.Controls.Button() {
                        Name = name,
                        Content = text,
                        Width = width,
                        Height = height
                    };
                    //...
                default:
                    return null;
            }
        }

I am creating a lot controls like this and as you can see, too much repetition is going on.

Is there some way to avoid this repetition? Thanks in advance for ideas.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T16:46:52+00:00Added an answer on May 22, 2026 at 4:46 pm

    You could create a generic function that does the create:

    private static Create<T>(string name, string text, double width, double height) where T: Control, new()
    {
       return new T { Name = name, Content = text, Width = width, Height = height }
    }
    

    Your switch then becomes:

    switch (element.Attribute("Type").Value) {
      case "System.Windows.Forms.Label" : return Create<System.Windows.Forms.Label>(name, text, width, height);
      etc.
    }
    

    You could also adapt this to pass in the XElement, whichever you prefer.

    If the Type attribute is always the name of the System.Type you want, then you could just do

    Control ctrl = (Control) Activator.CreateInstance(Type.GetType(element.Attribute("Type").Value));
    ctrl.Name = name;
    etc.
    

    If there’s a one to one mapping between the value of the attribute and the type you want, then you can declare a readonly static field with the mapping:

    private static readonly uiTypeMapping = new Dictionary<string,Type> {
      { "System.Windows.Forms.Label", typeof(System.Windows.Controls.Label) },
      { "System.Windows.Forms.Button", typeof(System.Windows.Controls.Button) },
      { etc. }
    };
    

    And use

    UIElement elem = (UIElement) Activator.CreateInstance(uiTypeMapping[element.Attribute("Type").Value]);
    etc.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this existing code which works very well. public static T SessionGet<T>(string key)
I'm trying to create a faster query, right now i have large databases. My
I have several statements which access very large Postgresql tables i.e. with: SELECT a.id
We have large C++ project that we used to compile with the /MP switch
I have a method that checks for a null value from an editText on
I have a large typed dataset which I want to partially fill for unit
I have a large project which I am replacing 2 parts of with better
Say I have a large struct, which includes other struct, etc. Would gcc -os
I have recently learned that you can use a neat switch statement with fallthrough
I have large data sets (10 Hz data, so 864k points per 24 Hours)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.