Paul Z
Paul Z

Reputation: 11

Produce full equation with answer in C#

Here is my problem

I would like to have a label produce both the formula AND the answer.

Here is what I have...please help.

   int score1 
   int score2

   double addition

   score1 = int.Parse(txbNumber1.Text);
   score2 = int.Parse(txbNumber2.Text);

   addition = score1 + score2;

   lblAdditionResults.Text = addition.ToString();

Upvotes: 0

Views: 68

Answers (3)

Arrakis
Arrakis

Reputation: 43

lblAdditionResults.Text = String.Format("{0} + {1} = {2}", score1, score2, addition);

Upvotes: 0

Bobby Tables
Bobby Tables

Reputation: 3013

Use String.Format:

lblAdditionResults.Text = string.Format("{0} + {1} = {2}",score1,score2,addition)

Upvotes: 1

Benjamin Gruenbaum
Benjamin Gruenbaum

Reputation: 276286

Here is a C# 6 answer using template strings:

lblAdditionResults.Text = $"{addition} = {score1} + {score2}";

See the official guide about interpolated strings.

Upvotes: 1

Related Questions