我是ASP.NET和C#编程的初学者。我有两个clasees电话和报告。 我想在Default.aspx.cs文件中调用函数LookupNumber(Phone p)并获取数组列表的每个返回值,在标签控件中显示返回值。 但是我得到了这个错误:
错误1非静态字段需要对象引用, 方法或财产 ' PhoneLookup.Models.Phone.LookupNumber(Phone.Models.Phone)' C:\ Users \ Erik \ Desktop \ Phone \ Phone \ Default.aspx.cs 24 26电话
错误2非静态字段需要对象引用, 方法或财产 ' PhoneLookup.Models.Phone.LookupNumber(Phone.Models.Phone)' C:\ Users \ Erik \ Desktop \ Phone \ Phone \ Default.aspx.cs 24 26电话
错误3非静态字段需要对象引用, 方法或财产 ' PhoneLookup.Models.Phone.LookupNumber(Phone.Models.Phone)' C:\ Users \ Erik \ Desktop \ Phone \ Phone \ Default.aspx.cs 24 26电话
这是我的源代码:
// Report.cs
namespace PhoneLookup.Models
{
public class Report
{
private String _name_surname;
private String _address;
private String _area_code;
private String _exchange;
private String _service_provider;
public Report()
{
_name_surname = String.Empty; _address = String.Empty; _area_code = String.Empty;
_exchange = String.Empty; _service_provider = String.Empty;
}
public String name_surname { get; set; }
public String address { get; set; }
public String area_code { get; set; }
public String exchange { get; set; }
public String service_provider { get; set; }
}
}
// Phone.cs Class
namespace PhoneLookup.Models
{
public class Phone
{
private Int16 _phone_number;
public Phone()
{
_phone_number = -1;
}
public Int16 phone_number { get; set; }
public Report[] LookupNumber(Phone p)
{
List<Report> lst = new List<Report>();
MySqlConnection Conn = new MySqlConnection(constr);
Conn.Open();
MySqlCommand Cmd = new MySqlCommand("SELECT * FROM Reports WHERE phone_number = @pn", Conn);
Cmd.Parameters.AddWithValue("@pn", p.phone_number);
MySqlDataReader Reader = Cmd.ExecuteReader();
while (Reader.Read())
{
lst.Add(new Report()
{
name_surname = Reader["name_surname"]).ToString(),
address = Reader["address"].ToString(),
area_code = Reader["area_code"].ToString(),
exchange = Reader["exchange"].ToString(),
service_provider = Reader["service_provider"].ToString(),
});
}
return lst.ToArray();
}
}
}
// Default.aspx.cs
protected void Button1_Click(object sender, EventArgs e)
{
Phone p = new Phone();
p.phone_number = Convert.ToInt16(q.Text);
// display name surname
Report[] ns = Phone.LookupNumber(p).Select(x => x.name_surname).ToArray();
// display adddress
Report[] ad = Phone.LookupNumber(p).Select(x => x.address).ToArray();
// display service provider
Report[] sp = Phone.LookupNumber(p).Select(x => x.service_provider).ToArray();
Label1.Text = ns.ToString();
Label2.Text = ad.ToString();
Label3.Text = sp.ToString();
}
感谢所有人!
答案 0 :(得分:1)
查看下面的代码行
Report[] ns = Phone.LookupNumber(p).Select(x => x.name_surname).ToArray();
第一个LookupNumber(p)
是一个实例方法而不是静态方法,因此您应该像p.LookupNumber(p)
一样调用它(或)通过在static
前缀{{1}前面将其声明为静态方法。
其次,LINQ调用Select(x => x.name_surname).ToArray()
将返回一个字符串数组string[]
,因为您选择了string
类型的特定字段,但您将其存储为Report[]
。你应该把它变成像
string[] ns = Phone.LookupNumber(p).Select(x => x.name_surname).ToArray();
根据你的评论:
是的,那是因为你在集合对象上调用了ToString()
,如下所示。
Label1.Text = ns.ToString();
如果你真的想在lebel中显示,那么可能会得到数组中的第一个元素并将其显示如下(或)使用不同的控件,如ListBox
Label1.Text = ns.FirstOrDefault();
答案 1 :(得分:0)
尝试改变:
public Report[] LookupNumber(Phone p)
为:
public static Report[] LookupNumber(Phone p)
答案 2 :(得分:0)
你是否应该使用&#34;其中&#34;而不是&#34;选择&#34;?
报告[] ns = Phone.LookupNumber(p).where(x =&gt; x.name_surname == ?)ToArray的();
从它的外观来看,如果你想&#34;选择&#34;一个字段,该字段的类型将返回(即字符串)。