我正在为兼职编程课程编写作业。我的代码的问题是array.find()和搜索结果。它应该(在我的理论中)搜索数组然后将它们发布给用户,但是从所有搜索中得出的结果是相同的:ass2task1.Program + customer这里只是部分代码因为老师告诉我们只要不发布我们的整个代码就可以在互联网上发帖提问
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);
答案 0 :(得分:1)
将对象转换为字符串("The forename resuts:" + resultforename
)时,它会调用对象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;
}
}
答案 1 :(得分:1)
Array.Find()
将返回与谓词匹配的对象,如果您需要属性值,则需要执行以下操作:resultforename.customerforname
或类似的内容。
如果找不到,则返回默认值,因此请检查空值等。
答案 2 :(得分:0)
(尝试)扩展Ric和clcto的答案。您在
中获取结构名称的原因Console.WriteLine("The forename resuts:" + resultforename);
Console.WriteLine("The surname resuts:" + resultsurname);
您的resultforename是struct customer - 默认情况下,Console.WriteLine(struct)不知道如何将复杂对象表示为字符串。
根据建议你可以做
Console.WriteLine("The forename resuts:" + resultforename.customerforname);
或者像clcto指出的那样为结构提供你自己的.ToString()方法 - 这样做你基本上告诉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"