c# - 如何将数据存储在数组的单行上?
问题描述
我需要能够将数据存储在数组中以显示在列表框中,到目前为止我只能获取文本文件的第一个单词(名称)。
我一直在使用 Line Split 方法,虽然这会将所有部分拆分为自己的行,但执行此方法也会带来“索引超出数组边界”。请帮忙,我已经被困在这整整两天了。
// - - - - - - - Selection Screen - - - - - - -
public SelectionScreen()
{
InitializeComponent();
//Loading from text file.
string[] PrimaryArray = new string[12];
PrimaryArray = System.IO.File.ReadAllLines(@"Test1.txt");
foreach (string line in PrimaryArray)
{
string[] Parts = line.Split(',');
CharStats temp = new CharStats();
temp.Name = Parts[0];
temp.Description = Parts[1];
temp.Notes = Parts[2];
temp.MaxHP = Convert.ToInt16(Parts[3]);
temp.MaxMP = Convert.ToInt32(Parts[4]);
temp.Atk = Convert.ToInt32(Parts[5]);
temp.Def = Convert.ToInt32(Parts[6]);
temp.Mat = Convert.ToInt32(Parts[7]);
temp.Mdf = Convert.ToInt32(Parts[8]);
temp.Agi = Convert.ToInt16(Parts[9]);
temp.Luk = Convert.ToInt16(Parts[10]);
temp.Lvl = Convert.ToInt16(Parts[11]);
ListboxCharacterList.Items.Add(temp.Name);
}
}
// - - - - - - - Writing stats - - - - - - -
public string WriteStats()
{
return Environment.NewLine + Name + "," + Description + "," + Notes + "," +
MaxHP + "," + MaxMP + "," + Atk + "," + Def + "," + Mat + "," +
Mdf + "," + Agi + "," + Luk + "," + Lvl;
}
// - - - - - - - TextFile - - - - - - -
//Mike,Desc.,Notes,1000,500,6,7,3,2,6,9
解决方案
AListBox
可以包含 any 的任何实例class
。你可以添加你CharStats
的。您只需覆盖ToString()
执行哪个以显示框中的值。如果你想选择SelectedItem
(s) 你必须投它CharStats
。
CsvHelper
使用( NuGet
-package)可以轻松读取 Csv 文件。这里是一个例子:
public class CharStats
{
public string FirstName { get; set; }
public string LastName { get; set; }
public int Age { get; set; }
public override string ToString()
{
return this.FirstName + " " + this.LastName;
}
}
public Form1()
{
InitializeComponent();
// From File:
// FirstName;LastName;Age
// d;D;4
// e;E;5
using (StreamReader sr = new StreamReader(@"C:\Temp\myfile.csv"))
{
using (CsvHelper.CsvReader csvReader = new CsvHelper.CsvReader(sr))
{
IEnumerable<CharStats> statList = csvReader.GetRecords(typeof(CharStats)).Cast<CharStats>();
foreach (CharStats cs in statList)
{
this.listBox1.Items.Add(cs);
}
}
}
this.listBox1.Items.Add(new CharStats() { FirstName = "a", LastName = "A", Age = 1 });
this.listBox1.Items.Add(new CharStats() { FirstName = "b", LastName = "B", Age = 2 });
this.listBox1.Items.Add(new CharStats() { FirstName = "c", LastName = "C", Age = 3 });
this.listBox1.SelectedIndex = 1;
var item = this.listBox1.SelectedItem;
CharStats selected = item as CharStats;
if (selected != null)
MessageBox.Show("Worked. We selected: " + selected.FirstName + " " + selected.LastName + " (" + selected.Age + ")");
}
这将显示一个这样的 ListBox:
一个
乙乙
c C
并且选择将是正确的对象。将MessageBox
显示
b B (2)
推荐阅读
- python - 在 Google Colab Pymc3 中未安装错误 Arviz
- css - 处理 div 的重叠
- php - Laravel 是开始和结束之间的时间(防止重复预订)
- ios - Ionic 4 - 购买前无法显示应用内购买产品信息
- javascript - 映射的 JSON 对象中的表,包含许多数组
- kong - config.duration 未在 KONG 的 Canary 发布插件中应用
- python - tkinter 算法编写者
- python - 码头工人,决斗烧瓶,ValueError:尝试相对导入超出顶级包
- android-studio - 如何使用 Android Studio 编辑共享首选项文件?
- angular - 播放/停止按钮