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 7045811
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T02:34:32+00:00 2026-05-28T02:34:32+00:00

I have the following declarations: int value1 =5 , value2 =10 ,value3; string calculate

  • 0

I have the following declarations:

int value1 =5 , value2 =10 ,value3;     
string calculate ="value1 + value2"; // dynamic string

I need to perform

value3 = convert.To int32(calculate );

Is there any possible to compute dynamic variable assigning …

  • 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-28T02:34:33+00:00Added an answer on May 28, 2026 at 2:34 am
    using System;
    using System.CodeDom.Compiler;
    using System.Reflection;
    using Microsoft.CSharp;
    using System.Collections.Generic;
    
    static public class Sample {
        static public double eval(string exp, Dictionary<string,string> varlist){
            CSharpCodeProvider csCompiler = new CSharpCodeProvider();
            CompilerParameters compilerParameters = new CompilerParameters();
            compilerParameters.GenerateInMemory = true;
            compilerParameters.GenerateExecutable = false;
    //      compilerParameters.ReferencedAssemblies.Add("System.dll");
            string temp =
    @"static public class Eval {
        static public double calc() {
            double exp = $exp;
            return exp;
        }
    }";
            string equation = exp;
            foreach(var key in varlist.Keys){
                equation = equation.Replace(key, varlist[key]);
            }
            temp = temp.Replace("$exp", equation);
            CompilerResults results = csCompiler.CompileAssemblyFromSource(compilerParameters,
                new string[1] { temp });
    
            if (results.Errors.Count == 0){
                Assembly assembly = results.CompiledAssembly;
                MethodInfo calc = assembly.GetType("Eval").GetMethod("calc");
                double answer = (double)calc.Invoke(null, null);
                return answer;
            } else {
                Console.WriteLine("expression errors!");
                foreach(CompilerError err in results.Errors){
                    Console.WriteLine(err.ErrorText);
                }
                return Double.NaN;
            }
        }
    }
    class Program {
        static void Main(){
            double value3 = 3;
            double value2 = 2;
            double value8 = 8;
            double value7 = 7;
            double value6 = 6;
            string calculate = " value3 / value2 * value8 / (36 * 840) * value7/ (2.2046 * value6) * value7";
            var vars = new Dictionary<string,string>();
            vars.Add("value3", value3.ToString("F"));
            vars.Add("value2", value2.ToString("F"));
            vars.Add("value8", value8.ToString("F"));
            vars.Add("value7", value7.ToString("F"));
            vars.Add("value6", value6.ToString("F"));
            double result = Sample.eval(calculate, vars);
            Console.WriteLine("{0:F8}", result);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If I have the following pointer variable declarations: int *a; int **c; Regarding to
I have a function with the following declaration: void cleanValid(int valid[][4], int &size, int
I have the following template declaration: template <typename T> void IterTable(int& rIdx, std::vector<double>& rVarVector,
I have a controller/model hypothetically named Pets. Pets has the following declarations: belongs_to :owner
I have the following decleration: private Dictionary<string, Dictionary<string, File>> listFiles = new Dictionary<string,Dictionary<string,File>>(); How
I Have the following code: -(void) changeAnimation:(NSString*)name forTime:(int) times { if(currentAnimation != @attack) {
say I have the following declarations: public enum Complexity { Low = 0, Normal
I have the following simple constructor: public SimpleClass(Type1 arg1, int interval = 1000) {
Suppose we have a couple of arrays according to the following type declarations bool
I have the following class declaration: class DEMData { private: int bitFldPos; int bytFldPos;

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.