开发者

need help converting label to string to output in another label box for summary: C# [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center. Closed 11 years ago.
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;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜