我似乎找不到我试图在c#中修复的代码背后的错误,它使用了几个由hashtag符号分隔的类
我试图让代码发布一个与早餐有关的句子,但找不到问题。 在公共部分类debug_5:form底部的文本lbltodayssspecial=(“今天我们有{1}表示{2}”,special.Name,special.Price.ToString(“C2”);)中有一个错误;。我无法确切地找出这个声明的问题所在。非常感谢您的帮助我似乎找不到我试图在c#中修复的代码背后的错误,它使用了几个由hashtag符号分隔的类,c#,C#,我试图让代码发布一个与早餐有关的句子,但找不到问题。 在公共部分类debug_5:form底部的文本lbltodayssspecial=(“今天我们有{1}表示{2}”,special.Name,special.Price.ToString(“C2”);)中有一个错误;。我无法确切地找出这个声明的问题所在。非常感谢您的帮助 using System.Collections.Generic; using System.Linq; using System.Text; using System.Th
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Debug_5
{
class Breakfast
{
public static string INFO = "Breakfast is the most important meal of the day.";
private string name;
private decimal price;
// Breakfast constructor requires a
// name, e.g "French toast", and a price
public Breakfast(string name, double price)
{
Name = name;
Price = price;
}
public string Name {get; set;}
public double Price{get; set;}
}
}
#####################
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Debug_5
{
public partial class Debug5: Form
{
Breakfast special;
public Debug5()
{
InitializeComponent();
special = Breakfast("French Toast", 4.99);
lblInfoMessage.Text = Breakfast.INFO;
}
private void btnShowSpecial_Click(object sender, EventArgs e)
{
lblTodaysSpecial = ("Today we have {1} for {2}", special.Name, special.Price.ToString("C2"));
}
}
}
##########################
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Debug_5
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Debug5());
}
}
}'''
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
名称空间调试单元5
{
班级早餐
{
public static string INFO=“早餐是一天中最重要的一餐。”;
私有字符串名称;
私人十进制价格;
//早餐建造师需要
//名称,例如“法式土司”,以及价格
公共早餐(字符串名称,双倍价格)
{
名称=名称;
价格=价格;
}
公共字符串名称{get;set;}
公共双价{get;set;}
}
}
#####################
使用制度;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统图;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用System.Windows.Forms;
名称空间调试单元5
{
公共部分类Debug5:表单
{
特色早餐;
公众谘询委员会5()
{
初始化组件();
特色早餐(“法式吐司”,4.99);
lblInfoMessage.Text=breakth.INFO;
}
私有void btnShowSpecial_单击(对象发送者,事件参数e)
{
lbltodayssspecial=(“今天我们有{2}的{1}”,special.Name,special.Price.ToString(“C2”);
}
}
}
##########################
使用制度;
使用System.Collections.Generic;
使用System.Linq;
使用System.Threading.Tasks;
使用System.Windows.Forms;
名称空间调试单元5
{
静态类程序
{
///
///应用程序的主要入口点。
///
[状态线程]
静态void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
运行(new Debug5());
}
}
}'''
您忘记了String.Format
和访问.Text
以及从0开始索引
lbltodayssspecial.Text=string.Format(“今天我们有{1}的{0}”、special.Name、special.Price.ToString(“C2”);
杰克是对的,他打败了我
但是,如果您的目标是避免键入string.Format
,也可以这样做:
var lbltodayssspecial=$“今天我们有{special.Price:C2}的{special.Name}”代码>
这被称为“字符串插值”,我认为在C#6.0+中可用。作为额外的一点,应该可以直接在字符串中使用Price
格式,如下所示:string.format(“今天我们有{0}表示{1:C2}”、special.Name、special.Price)代码>