1

I am beginner in ASP.NET and C# programming. I have two clasees Phone and Report. I want to call function LookupNumber(Phone p) in Default.aspx.cs file and to get each returned value of array list, displaying returned values in labels controls. But I get this error:

Error 1 An object reference is required for the non-static field, method, or property 'PhoneLookup.Models.Phone.LookupNumber(Phone.Models.Phone)' C:\Users\Erik\Desktop\Phone\Phone\Default.aspx.cs 24 26 Phone

Error 2 An object reference is required for the non-static field, method, or property 'PhoneLookup.Models.Phone.LookupNumber(Phone.Models.Phone)' C:\Users\Erik\Desktop\Phone\Phone\Default.aspx.cs 24 26 Phone

Error 3 An object reference is required for the non-static field, method, or property 'PhoneLookup.Models.Phone.LookupNumber(Phone.Models.Phone)' C:\Users\Erik\Desktop\Phone\Phone\Default.aspx.cs 24 26 Phone

Here is my source code:

// 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();
}

Thanks to all!

2
  • 2
    Looks like you need to Phone instance instead of it's type. Like var p = new Phone(); and call p.LookupNumber(...) etc. Or change this LookupNumber method to static like public static Report[] LookupNumber. Commented Dec 21, 2015 at 15:13
  • @SonerGönül I change , but I get this error: Error 1 Cannot implicitly convert type 'string[]' to 'Phone.Models.Report[]' Commented Dec 21, 2015 at 15:23

3 Answers 3

1

look at your code line below

Report[] ns = Phone.LookupNumber(p).Select(x => x.name_surname).ToArray();

First LookupNumber(p) is an instance method and not a static method and so you should call it like p.LookupNumber(p) (or) declare it as static method by prefixing static keyword.

Second, the LINQ call Select(x => x.name_surname).ToArray() will return a string array string[] cause you are selecting a specific field which is of string type but you are storing it as Report[]. You should rather make it like

string[] ns = Phone.LookupNumber(p).Select(x => x.name_surname).ToArray();

Per your comment:

Yes and that's because you are calling ToString() on a collection obejct as can be depicted below.

Label1.Text = ns.ToString();

If you really want to show in lebel then probably get the first element in the array and show it as below (or) use a different control like ListBox

Label1.Text = ns.FirstOrDefault();
Sign up to request clarification or add additional context in comments.

1 Comment

string[] ns = Phone.LookupNumber(p).Select(x => x.name_surname).ToArray(); returns to me : System.String[]
0

Try to change:

    public Report[] LookupNumber(Phone p)

to:

    public static Report[] LookupNumber(Phone p)

5 Comments

I change , but I get this error: Error 1 Cannot implicitly convert type 'string[]' to 'Phone.Models.Report[]'
The conversion problem is caused by your LINQ statement. You are doing a select which is returning a string. So, the LINQ statement will return an array of strings.
Additionally, as note. It would be better to call the LookupNumber function once to get the report on the phone number, and then query that retrieved report.
How can I get values of arraylist without Linq statement trouble ?
Change the types of ns, ad, and sp to "string[]". You will also need to change the logic for setting the labels' text. You are trying to convert the information for multiple strings to a single string. This requires formatting or redesigning your page.
0

Should you being using "where" instead of "select"?

Report[] ns = Phone.LookupNumber(p).where(x => x.name_surname == ?).ToArray();

By the looks of it, if you are trying to "select" a field, the type of that field will be return (i.e. string).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.