C# 在不同的课堂上使用列表
我使用下一个脚本列出了一个列表:C# 在不同的课堂上使用列表,c#,list,class,methods,C#,List,Class,Methods,我使用下一个脚本列出了一个列表: private void Skills_Button_Click(object sender, RoutedEventArgs e) { ... var dict = JsonConvert.DeserializeObject<Dictionary<string, Item>>(Database); List<String> Item_Name = new List<String>(); List<doub
private void Skills_Button_Click(object sender, RoutedEventArgs e)
{
...
var dict = JsonConvert.DeserializeObject<Dictionary<string, Item>>(Database);
List<String> Item_Name = new List<String>();
List<double> Sell_Price = new List<double>();
List<double> Buy_Price = new List<double>();
foreach (string key in dict.Keys)
{
Item_Name.Add(dict[key].name);
Sell_Price.Add(Convert.ToDouble(dict[key].sell_average));
Buy_Price.Add(Convert.ToDouble(dict[key].buy_average));
}
}
private void Skills\u按钮\u单击(对象发送者,路由目标)
{
...
var dict=JsonConvert.DeserializeObject(数据库);
列表项_Name=新列表();
列表售价=新列表();
列表购买价格=新列表();
foreach(dict.Keys中的字符串键)
{
项目名称.Add(dict[key].Name);
卖出价格.Add(Convert.ToDouble(dict[key].Sell\u average));
买入价格.Add(Convert.ToDouble(dict[key].Buy_average));
}
}
然后,我创建了一个方法:
public double S(string name, List<String> Item_Name, List<double> Sell_Price)
{
int ind = Item_Name.FindIndex(s=> s==name);
double SP = Sell_Price[ind];
return SP;
}
public double S(字符串名称、列表项名称、列表售价)
{
int ind=Item_Name.FindIndex(s=>s==Name);
双SP=卖出价格[ind];
返回SP;
}
最后,我希望在另一种方法中使用此方法(当在wpf中按下按钮时),但我收到一个错误,即该类中不知道列表项目名称、售价、售价
我怎样才能使它们“全球化”到其他类
我在这里找到的所有答案都是关于创建的总是新的列表
谢谢。在这种情况下,您可以将其提取到一个公共方法中,该方法返回所有这些数据,如下所示,并在需要时调用它
public Tuple<List<String>, List<Double>, List<Double>> GetEventData(Database db)
{
var dict = JsonConvert.DeserializeObject<Dictionary<string, Item>>(db);
List<String> Item_Name = new List<String>();
List<double> Sell_Price = new List<double>();
List<double> Buy_Price = new List<double>();
foreach (string key in dict.Keys)
{
Item_Name.Add(dict[key].name);
Sell_Price.Add(Convert.ToDouble(dict[key].sell_average));
Buy_Price.Add(Convert.ToDouble(dict[key].buy_average));
}
Tuple t = Tuple.Create(Item_Name, Sell_Price, Buy_Price);
return t;
}
公共元组GetEventData(数据库db)
{
var dict=JsonConvert.DeserializeObject(db);
列表项_Name=新列表();
列表售价=新列表();
列表购买价格=新列表();
foreach(dict.Keys中的字符串键)
{
项目名称.Add(dict[key].Name);
卖出价格.Add(Convert.ToDouble(dict[key].Sell\u average));
买入价格.Add(Convert.ToDouble(dict[key].Buy_average));
}
Tuple t=Tuple.Create(商品名称、售价、买入价);
返回t;
}
首先,我将属性包装到一个模型中。请注意,我使用的是decimal
而不是double
,因为这是处理货币时的建议
然后我会设计我的类,让它有一个可枚举的成员来存储结果
class WhateverYourClassIsCalled
{
private IEnumerable<ItemModel> items;
private void Skills_Button_Click(object sender, RoutedEventArgs e)
{
items = GetItems();
}
private IEnumerable<ItemModel> GetItems()
{
IReadOnlyDictionary<string, Item> dictionary = JsonConvert
.DeserializeObject<Dictionary<string, Item>>(database);
foreach (KeyValuePair<string, Item> item in dictionary)
{
yield return new ItemModel(
itemName: item.Key,
sellPrice: item.Value.sell_average,
buyPrice: item.Value.buy_average);
}
}
// You called this method, "S", which is a poor name!
private decimal GetSellPrice(string itemName)
{
// Notice that SingleOrDefault here might be null, so have a think about how you want to handle that.
return items
.SingleOrDefault(item => item.ItemName == itemName)
.SellPrice;
}
}
分类您的分类
{
私人物品;
私有无效技能按钮单击(对象发送者,路由目标)
{
items=GetItems();
}
私有IEnumerable GetItems()
{
IReadOnlyDictionary=JsonConvert
.反序列化对象(数据库);
foreach(字典中的KeyValuePair项)
{
收益-收益新模型(
itemName:item.Key,
售价:item.Value.sell_平均值,
购买价格:物品。价值。购买(平均值);
}
}
//您将此方法称为“S”,这是一个糟糕的名称!
私有十进制GetSellPrice(字符串itemName)
{
//请注意,这里的SingleOrDefault可能为null,因此请考虑如何处理它。
退货项目
.SingleOrDefault(item=>item.ItemName==ItemName)
.售价;
}
}
优势
- 您的项目数据被封装(查找OOP的4个支柱)
- 您的项目数据不会因为存储在不同的成员中而断开连接(您只需处理
项目
,而不是项目名称
、售价
和买入价
)
- 您的
项目
成员是安全的,因为您无法添加、删除或清除集合。只能对其进行迭代或重新分配
- 方法分为明确的责任
- 您的
ItemModel
是不可变的,在大多数情况下这是一件好事
不清楚你在问什么;您定义的变量是名为Skills\u Button\u Click
的方法的局部变量。你的意思是想在同一个类中的方法之间共享这些方法,还是让它们也可用于另一个类,或者其他什么?所有的方法都在wpf主窗口中,所以我想在方法之间共享。更新了问题谢谢。只需将这些局部声明移动到类中,这样它们就在整个类的范围内,而不是方法的局部范围内。将em类级别的变量设置为公共
,如果需要,包括shared
。仅此而已:)@zackraiyan它们不需要是public
,除非它们需要从类外访问,而且在C中没有shared
这样的东西#
class WhateverYourClassIsCalled
{
private IEnumerable<ItemModel> items;
private void Skills_Button_Click(object sender, RoutedEventArgs e)
{
items = GetItems();
}
private IEnumerable<ItemModel> GetItems()
{
IReadOnlyDictionary<string, Item> dictionary = JsonConvert
.DeserializeObject<Dictionary<string, Item>>(database);
foreach (KeyValuePair<string, Item> item in dictionary)
{
yield return new ItemModel(
itemName: item.Key,
sellPrice: item.Value.sell_average,
buyPrice: item.Value.buy_average);
}
}
// You called this method, "S", which is a poor name!
private decimal GetSellPrice(string itemName)
{
// Notice that SingleOrDefault here might be null, so have a think about how you want to handle that.
return items
.SingleOrDefault(item => item.ItemName == itemName)
.SellPrice;
}
}