0

I need to be able to make a textbox string take from variables and put the variables into a string.

textBoxEmailTemplateUT.Text = "The documents are as follows: FileNameHere ► {TotalPanelCountUtah} ► {TotalkWUtah} ► ElectricalUsageOffsetHere InformationAboutTheSystemHere If There are any questions or concerns, please reply to this email. Regards YourNameHere");

This is suppose to be a type of email template after someone does a few calculations in the other code (not displayed). The {TotalPanelCountUtah} and {TotalkWUtah} are both double variables that I need to be put into the string in the textbox. Some assistance would be appreciated.

Dmitry
  • 13,797
  • 6
  • 32
  • 48
Sero
  • 161
  • 8
  • 1
    Have a look as string formatting - here, for example: https://msdn.microsoft.com/en-us/library/system.string.format(v=vs.110).aspx. Your string has name placeholders and string.Format likes numbers. – Clay Sep 19 '15 at 19:50
  • Also consider writing a dedicated text builder method (using `StringBuilder`) if you want better control over formatting and plan to get a long message to send, especially if your code will be used by someone else. I find it terribly painfull to parse a lengthy line of code with endless variables and sometimes, with obscure meanings. – Karl Stephen Sep 19 '15 at 20:24

3 Answers3

0

Using C# 6, you can use string interpolation, as such:

string txt = $"The values are: {value1}, {value2}";

The syntax requires that you put a $ before the string and enclose any variables within braces ({}).

Without string interpolation (pre-c#6), you can use:

string txt = String.Format("The values are: {0}, {1}", value1, value2);

This snippet of code does the same as the first one. String.Format will format a string, replacing each of the values between braces with its corresponding argument.

If:

double value1 = 0.5;
double value2 = 1.5;

then, for both of the snippets (interpolation and string.Format):

txt == "The values are: 0.5, 1.5"`
Community
  • 1
  • 1
Fernando Matsumoto
  • 2,697
  • 1
  • 18
  • 24
  • I appreciate everyone's responses. The $ approach worked best for what I'm trying to do with this code. I can understand String.Format would be incredibly useful as well as the .Replace method. I'll have to try playing with these more at a later time. For now, this gets the Calculator I'm building for my job working properly. Again, thank you all of you who came out to help! – Sero Sep 19 '15 at 20:16
0

as Clay mentioned in comment use string.Format

textBoxEmailTemplateUT.Text = string.Format("The documents are as follows: FileNameHere ► {0} ► {1} ► ElectricalUsageOffsetHere InformationAboutTheSystemHere If There are any questions or concerns, please reply to this email. Regards YourNameHere",TotalPanelCountUtah, TotalkWUtah);

where TotalPanelCountUtah TotalPanelCountUtah are double type

Alexander V.
  • 1,518
  • 14
  • 14
0

string.Format is generally preferred, but I included a second approach using string.replace below. It is probably also a good idea to round the values before displaying them.

double val1 = 1.2345;
double val2 = 8.7654;

// recommended
textBox1.Text = string.Format(
    "Value one is {0:0.00}, and value two is {1:0.00}.",
    val1, val2);

// also an option
textBox1.Text = "Value one is val1, and value two is val2."
    .Replace("val1", Math.Round(val1, 2).ToString())
    .Replace("val2", Math.Round(val2, 2).ToString());
tripflag
  • 235
  • 2
  • 8