need help converting label to string to output in another label box for summary: C# [closed]
strFirstName = txtFirstName.Text;
strLastName = txtLastName.Text;
lblSummary = "FirstName:" + strFirstName + Environment.NewLine +
"Last Name:" + strLastName + Environment.NewLine +
"Gross Income:" + decGrossIncome.ToString("c") + Environment.NewLine +
"Taxes Due:" + decTaxesDue.ToString("c") + Environment.NewLine +
"Total Payments:" + decTotalPayments.ToString("c") + Environment.NewLine +
"Total Amount Due:" + decTotalAmountDue.ToString("c");
lblSummary.Text = strSummary;
*note*the following contains the error
"Total Amount Due:" + decTotalAmountDue.ToString("c");
The error I get is:
Error 1 Cannot implicitly convert type 'string' to 'System.Windows开发者_运维技巧.Forms.Label' E:\CIS 162 AD\CS03\CS03\CS03\Form1.cs 79 37 CS03
You're trying to assign a string value straight to a Label
variable. I suspect you meant this:
string strSummary = "FirstName:" + strFirstName + Environment.NewLine +
"Last Name:" + strLastName + Environment.NewLine +
"Gross Income:" + decGrossIncome.ToString("c") + Environment.NewLine +
"Taxes Due:" + decTaxesDue.ToString("c") + Environment.NewLine +
"Total Payments:" + decTotalPayments.ToString("c") + Environment.NewLine +
"Total Amount Due:" + decTotalAmountDue.ToString("c");
lblSummary.Text = strSummary;
I would personally advise using string.Format
, and ditching the pseudo-Hungarian naming by the way.
string summary = string.Format("First Name: {1}{0}" +
"Last Name: {2}{0}" +
"Gross Income: {3:c}{0}" +
"Taxes Due: {4:c}{0}" +
"Total Payments: {5:c}{0}" +
"Total Amount Due: {6:c}",
Environment.NewLine, firstName, lastName,
grossIncome, taxesDue, totalPayments,
totalAmountDue);
// I'm not so hot on naming controls, so I'm not saying this is great - but I
// prefer it not to be control-type-specific; what's important is that it's a
// control we're using to output the summary.
summaryOutput.Text = summary;
精彩评论