Array.find()提供了奇怪的结果
我正在为我的兼职编程课程写作业。我的代码的问题是array.find()和搜索的结果。它应该(在我的理论中)在数组中搜索信息,然后将它们发布给用户,但是所有搜索结果都是一样的:ass2task1.Program + customer这里只有代码的一部分,因为老师告诉我们,我们可以在互联网上,只要张贴问题,因为我们当您转换对象为字符串("The forename resuts:" + resultforename
)不要张贴我们的整个代码Array.find()提供了奇怪的结果
struct customer
{
public int customernumber;
public string customersurname;
public string customerforname;
public string customerstreet;
public string customertown;
public DateTime customerdob;
}
static void Main(string[] args)
{
customer[] customerdetails = new customer[99];
int selector = 0;
int selector2 = 0;
string vtemp = "";
string ctemp = "";
int searchnumber;
string searchforename; //variable/ array declaring
string searchsurname;
string searchtown;
DateTime searchdob;
customer resultnumber;
customer resultforename;
customer resultsurname;
customer resulttown;
customer resultdob;
if (selector2 == 2)
{
Console.Clear();
Console.WriteLine("Enter the forename you are looking for: ");
searchforename = (Console.ReadLine());
resultforename = Array.Find(customerdetails, customer => customer.customerforname == searchforename);
Console.Clear();
Console.WriteLine("Enter the surname you are looking for: "); // all of the searches comes out with ass2task1.Program+customer result
searchsurname = (Console.ReadLine());
resultsurname = Array.Find(customerdetails, customer => customer.customersurname == searchsurname);
Console.WriteLine("The forename resuts:" + resultforename);
Console.WriteLine("The surname resuts:" + resultsurname);
Array.Find()
将返回匹配的断言,如果你想要的属性值,你需要做的是这样的对象:resultforename.customerforname
或类似的东西。
如果没有找到它,那么默认值将被退回,所以检查空值等
谢谢,添加到代码,它现在完美。 – Skretek112 2014-08-29 16:11:16
很高兴我能帮到你。 – Ric 2014-08-29 21:25:22
它调用对象的方法ToString()
。定义一个适当的ToString()
方法:
struct customer
{
public int customernumber;
public string customersurname;
public string customerforname;
public string customerstreet;
public string customertown;
public DateTime customerdob;
public override string ToString()
{
return customersurname + ", " + customerforname;
}
}
要在里克和clcto的回答(尝试)扩展。在
Console.WriteLine("The forename resuts:" + resultforename);
Console.WriteLine("The surname resuts:" + resultsurname);
你resultforename你得到的结构名称的原因是客户结构的 - 默认Console.WriteLine(结构)不知道如何表达一个复杂的对象为字符串。
至于建议你可以做
Console.WriteLine("The forename resuts:" + resultforename.customerforname);
或为结构提供自己的ToString()方法clcto指出的 - 这样做基本上就Console.WriteLine(或任何字符串表示)如何将客户的结构表示为字符串。
不知道这是否会有所帮助,或使其更加清晰。但给定:
public struct Foo
{
public string Bar { get; set; }
}
public struct FooTwo
{
public string Bar { get; set; }
public override string ToString()
{
return "This is how to represent a Foo2 as string: " + Bar;
}
}
Foo[] foos = new Foo[99];
Foo foundFoo = foos[0]; // This is equivalent to your find statement... setting a foundFoo local variable to a Foo struct
string foundBar = foos[0].Bar; // This is another way to get to what you're trying to accoomplish, setting a string value representation of your found object.
Console.WriteLine(foundFoo); // Doesn't know how to deal with printing out a Foo struct - simply writes [namespace].Foo
Console.WriteLine(foundFoo.Bar); // Outputs Foo.Bar value
Console.WriteLine(foundBar); // Outputs Foo.Bar value
FooTwo foo2 = new FooTwo();
foo2.Bar = "test bar";
Console.WriteLine(foo2); // outputs: "This is how to represent a Foo2 as string: test bar"
实际上你是否正在向任何客户填充客户详细信息数组?代码只显示你声明一个数组,它将容纳99个项目 – 2014-08-29 14:50:17
在你的注释行“ass2task1.Program + customer”什么是ass2task1?另外,你将selecttor2设置为0,然后有一个if块,只有当selector2 var是2时才会被命中,你能提供更相关的代码来解决你的问题吗?另外你是用数据填充你的数组吗? – Kritner 2014-08-29 14:54:23
@Kritner'ass2task1'可能是命名空间。 – Ric 2014-08-29 14:55:47