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

  • SEARCH
  • Home
  • 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 9272755
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T15:56:33+00:00 2026-06-18T15:56:33+00:00

I’m working on building a dynamic calculation framework, that will build a generic GUI

  • 0

I’m working on building a dynamic calculation framework, that will build a generic GUI based on the calculation types and attributes on those types.

For instance, I might have a simple Adder calculator (forgive the simplicity, but when putting together frameworks, I like to start simple and work my way up), that looks like this:

[CalculatorAttribute("This calculator add X+Y=Z")]
class AdderCalculator : CalculatorBase
{
    [CalculationValueAttribute(InputOutputEnum.InputValue, "First Input")]
    public int? X 
    {
        set { m_X = value; m_Z = null;}
        get { return m_X; }
    }

    [CalculationValueAttribute(InputOutputEnum.InputValue, "Second Input")]
    public int? Y 
    {
        set{ m_Y = value; m_Z = null;}
        get { return m_Y; }
    }

    [CalculationValueAttribute(InputOutputEnum.OutputValue, "Output")]
    public int? Z 
    {
        get { return m_Z; }
    }

    public AdderCalculator()
    {
        m_X = m_Y = m_Z = null;
    }

    public override Boolean ReadyToCalc()
    {
        return (m_X != null && m_Y != null);
    }

    public override Boolean NeedToCalc()
    {
        return (m_Z == null);
    }

    public override void Calculate()
    {
        if(ReadyToCalc()){ m_Z = m_X + m_Y;}
    }

    private int? m_X, m_Y, m_Z;
}

(forgive the whitespace, I tried to reduce the size as much as possible without sacrificing too much readability).

I’m using the nullable type to allow me to distinguish unset values from set values

Now, in my GUI, I’m using TextBoxes to collect the input information. From TextBox, I can only get the Text (String). Using reflection, I know the name of the property and can get it’s setter (set_X or set_Y in this case).

The type of the field is (of course) int? (nullable int), but I can use:

 PropertyInfo.PropertyType.GetGenericArguments();

method to get the Int32 type (or Int64, whichever may be the case).

So, given that I can end up with a instance of a Type object, can anyone recommend a generic way to convert String to that type

What I have considered is:

  1. Implement a string setter method in my AdderCalculator: set_X(String s)
  2. Change my control type to NumericUpDown, as it will return a Decimal
  3. change the type to Decimal, but Decimal can’t directly convert from String either (it can use parse, but that is tougher from a generic standpoint

Can anyone provide additional insight?

  • 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-06-18T15:56:34+00:00Added an answer on June 18, 2026 at 3:56 pm

    Just to get the answer out where it can be clearly seen, the code I ended up with is:

                // collect relevant inputs
                foreach (Control c in fPanelIn.Controls)
                {
                    if (c.Tag is PropertyInfo) 
                    {
                        PropertyInfo pi = c.Tag as PropertyInfo;
    
                        if(c.Text.Length>0)
                        {
                            Type ti = pi.PropertyType;
    
                            if (ti.IsGenericType)
                            {
                                ti = ti.GetGenericArguments()[0];
                            }
                            object o = Convert.ChangeType(c.Text, ti);
    
                            pi.SetValue(Calculator, o, null);
    
                            //MethodInfo mi = calcType.GetMethod(ConstructMethodName(pi), new Type[] { typeof(String) });
    
                            //mi.Invoke(Calculator, new object[] { c.Text });
                        }
                        else
                        {
                            pi.SetValue(Calculator, null, null);
                        }
                    }
                }
    

    I still need to add some exception protection for invalid values, but this works with the property type being any numeric type (byte, short, int, int32, int64, float, double, Decimal, etc…)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a small JavaScript validation script that validates inputs based on Regex. I
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I need a function that will clean a strings' special characters. I do NOT
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into

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.