2

Is there a way to cast a string to an interpolation object? example: $"{912+8/2}"

I've tried to put the raw text from my textbox into the interpolation $"{results.Text}" (in the calculator you can type only numbers and .,/,*,-,+).

Is there a way to let the system make the math?

/// <summary>
/// Logica di interazione per MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{    
    public MainWindow()
    {
        InitializeComponent();
    }

    //define onclick listener for the numbers
    private void b0_Click(object sender, RoutedEventArgs e)
    {
        results.Text = results.Text + "0";
    }
    private void b1_Click(object sender, RoutedEventArgs e) 
    {
        results.Text = results.Text + "1";
    }

    private void b2_Click(object sender, RoutedEventArgs e) 
    {
        results.Text = results.Text + "2";
    }

    private void b3_Click(object sender, RoutedEventArgs e) 
    {
        results.Text = results.Text + "3";
    }

    private void b4_Click(object sender, RoutedEventArgs e) 
    {
        results.Text = results.Text + "4";
    }

    private void b5_Click(object sender, RoutedEventArgs e) 
    {
        results.Text = results.Text + "5";
    }

    private void b6_Click(object sender, RoutedEventArgs e) 
    {
        results.Text = results.Text + "6";
    }

    private void b7_Click(object sender, RoutedEventArgs e) 
    {
        results.Text = results.Text + "7";
    }

    private void b8_Click(object sender, RoutedEventArgs e) 
    {
        results.Text = results.Text + "8";
    }

    private void b9_Click(object sender, RoutedEventArgs e) 
    {
        results.Text = results.Text + "9";
    }

    //define onclick listener for the operations and special char
    private void ba_Click(object sender, RoutedEventArgs e) 
    {
        results.Text = results.Text + "+";
    }

    private void bs_Click(object sender, RoutedEventArgs e) 
    {
        results.Text = results.Text + "-";
    }

    private void bm_Click(object sender, RoutedEventArgs e) 
    {
        results.Text = results.Text + "*";
    }

    private void bf_Click(object sender, RoutedEventArgs e) 
    {
        results.Text = results.Text + "/";
    }

    private void bd_Click(object sender, RoutedEventArgs e) 
    {
        results.Text = results.Text + ".";
    }

    private void bc_Click(object sender, RoutedEventArgs e) 
    {
        results.Text = "";
    }

    private void be_Click(object sender, RoutedEventArgs e) 
    {
        results.Text = $"{results.Text}"; //<-- Problem here
    }    
}
maccettura
  • 10,514
  • 3
  • 28
  • 35
Racerider
  • 116
  • 2
  • 11
  • No, interpolation doesn't work like that. It takes *compile-time* expressions and evaluates them at execution time. – Jon Skeet May 10 '18 at 17:32
  • There is no such thing as an "interpolation object". All what the `$""` thing does is eye-candy to a call to `String.Format`, and that's not a code compiler. You need to do the parsing and interpret the operators yourself. – Alejandro May 10 '18 at 17:32
  • It sounds like you want to evaluate math expressions at runtime: https://stackoverflow.com/questions/5838918/evaluate-c-sharp-string-with-math-operators – Joe Phillips May 10 '18 at 17:35
  • 2
    Possible duplicate of [Evaluate C# string with math operators](https://stackoverflow.com/questions/5838918/evaluate-c-sharp-string-with-math-operators) – Joe Phillips May 10 '18 at 17:36
  • 1
    @Alejandro: It's *slightly* more than that, if you include `FormattableString` conversions. But it's certainly not the sort of dynamic evaluation that the OP wants. – Jon Skeet May 10 '18 at 17:40

0 Answers0