我有这段代码
class Player
{
private string first_name;
private string middle_name;
private string last_name;
private DateTime dob;
private string nat;
private char gender;
public Player(string first_name, string last_name, string middle_name, DateTime dob, string nat, char gender)
{
this.first_name = first_name;
this.last_name = last_name;
this.middle_name = middle_name;
this.dob = dob;
this.nat = nat;
this.gender = gender;
}
public override string ToString()
{
return first_name + " " + middle_name + " " + last_name + " " + dob + " " + nat + " " + gender;
}
private List<string> content = new List<string>();
public string FileName { get; set; }
public string Delimiter { get; set; }
public ReadCSVFile(string fn, string delim = "|")
{
FileName = fn;
Delimiter = delim;
}
public void Load()
{
TextFieldParser par = new TextFieldParser(FileName);
par.TextFieldType = FieldType.Delimited;
par.SetDelimiters(Delimiter);
while (!par.EndOfData)
{
string[] fields = par.ReadFields();
foreach (string field in fields)
{
Console.WriteLine(field);
}
}
par.Close();
}
static void RunReadCSVFile()
{
var f = new ReadCSVFile(@"C:\Temp\car1.txt");
f.Load();
}
在ToString方法之后我遇到了问题。我尝试创建方法ReadCSVFile时出错。 IDE说“方法必须有一个返回值”。但是,我无法弄明白。
答案 0 :(得分:1)
您尝试使用ReadCVSFile
,就像它是一个类
var f = new ReadCSVFile(@"C:\Temp\car1.txt");
f.Load();
但没有课程ReadCVSFile
。
此行表示您要使用班级
public ReadCVSFile(string filename, string delim = "|") {
}
因为这是构造函数的结构。
但是你没有在任何地方定义课程ReadCVSFile
。
我会建议这样的事情
class Player {
//player properties
// ...
public Player(...) { ... }
public string ToString() { ...}
} //end of class player
class ReadCVSFile {
private List<string> content = new List<string>();
public string FileName { get; set; }
public string Delimiter { get; set; }
public ReadCSVFile(string fn, string delim = "|")
{
FileName = fn;
Delimiter = delim;
}
public void Load() { ...}
static void RunReadCSVFile()
{
var f = new ReadCSVFile(@"C:\Temp\car1.txt");
f.Load();
}
}
你的代码在这里没有明确播放器类的作用吗? cvs文件应该包含播放器吗?
答案 1 :(得分:0)
方法ReadCSVFile
必须关联返回类型,如果方法没有返回任何内容,则将其标记为void
。 void指定该方法不返回值。
通过指定访问级别(如public或private),可选修饰符(如abstract或sealed),返回值,方法名称和任何方法参数,在类或结构中声明方法。这些部分一起是该方法的标志。请阅读here了解更多信息。
public void ReadCSVFile(string fn, string delim = "|")
{
FileName = fn;
Delimiter = delim;
}
答案 2 :(得分:0)
public class Player
{
private string first_name;
private string middle_name;
private string last_name;
private DateTime dob;
private string nat;
private char gender;
public Player(string first_name, string last_name, string middle_name, DateTime dob, string nat, char gender)
{
this.first_name = first_name;
this.last_name = last_name;
this.middle_name = middle_name;
this.dob = dob;
this.nat = nat;
this.gender = gender;
}
public override string ToString()
{
return first_name + " " + middle_name + " " + last_name + " " + dob + " " + nat + " " + gender;
}
private List<string> content = new List<string>();
public string FileName { get; set; }
public string Delimiter { get; set; }
//public static class ReadCSVFile(string fn, string delim = "|")
//{
// FileName = fn;
// Delimiter = delim;
//}
private void Load()
{
TextFieldParser par = new TextFieldParser(FileName);
par.TextFieldType = FieldType.Delimited;
par.SetDelimiters(Delimiter);
while (!par.EndOfData)
{
string[] fields = par.ReadFields();
foreach (string field in fields)
{
Console.WriteLine(field);
}
}
par.Close();
}
public void RunReadCSVFile(string fn, string delim = "|")
{
//var f = ReadCSVFile(@"C:\Temp\car1.txt");
//f.Load();
FileName = fn;
Delimiter = delim;
Load();
}
static void Main(string[] args)
{
Player plr = new Player("First","Last","Middle",DateTime.Now,"nat",'G');
plr.RunReadCSVFile(@"C:\temp\car1.txt");
}