c# - 无法在我的 c# 程序中为产品添加数字
问题描述
我正在尝试制作购物清单并向其中添加不同的商品,但我不知道如何添加商品的价格并获得结果。
这是我正在使用的代码,它当前给了我这个错误,“输入字符串的格式不正确”
public partial class Form1 : Form
{
List<string> Items;
public Form1()
{
InitializeComponent();
Items = new List<string>();
}
private void BtnAdd_Click(object sender, EventArgs e)
{
double num;
num = double.Parse(txtItems.Text);
Items.Add(txtItems.Text);
listBox1.Items.Clear();
for(int i =0;i< Items.Count; i++)
{
listBox1.Items.Add(Items[i] + num);
}
}
解决方案
如果您需要关联两个值,例如名称和价格,您应该创建一个包含这两个值的类。如果您命名 this class Item
,那么您可以将 aList<Item> Items = new List<Item>();
与所有项目一起存储。
我建议您阅读有关类的更多信息以了解其工作原理。
推荐阅读
- apache-spark - 数据帧计数会触发 spark.drive.maxResultSize 限制吗?
- node.js - 从猫鼬查询内部更新外部数组时遇到问题
- mongodb - 颤振和mongodb连接的问题
- tensorflow2.0 - 将 TensorFlow Hub 与 TF2 和 Universal Sentence Encoder v4 一起使用
- python - 如何防止 Sentry 针对特定类型的错误对错误/警报进行分组?
- sql-server - SQL Server如何处理角色权限和用户权限不一致的问题
- oracle - 搜索字符串以匹配 Oracle SQL 中视图中所有现有列和行中的数据
- java - maven-cargo2-plugin 如何识别我的 Spring Boot 应用程序已经运行了嵌入式 Tomcat?
- haskell - JSON Payload POST 请求总是超时 AWS ApiGateway
- string - 在 Clojure 的函数中返回一个字符串