2

I'm writing some code where I have some information about customers stored in an array called members (id, initials). I then ask the user for their id and initials and match the inputs to the stored information from array members. If they match I move on. However I get an error in my coding: "an object reference is required to access non-static field method or property". The error comes from the if statements. Any suggestions on how to correct this issue?

Some background info: I have two classes, one called Customer and one called Menu. The Menu is the main class while Customer is the class I reference from.

This is from my Menu class:

       int L = 0; 
       string I = "";
       Customer[] members = new Customer[2];
        members[0] = new Customer(3242, "JS");
        members[1] = new Customer(7654, "BJ");

        Console.Write("\nWhat is your Loyalty ID #: ");
        L =Convert.ToInt32(Console.ReadLine());
        Console.Write("\nWhat is your first and last name initials: ");
        I = Console.ReadLine();

                if (L==Customer.GetId())
                    {
                    if (I == Customer.GetInitials())
                       {
                       Console.WriteLine("It matches");
                       }
                    }
                 else
                    {
                    Console.WriteLine("NO match");
                    }
                Console.ReadKey();
        }
    }
}

This from my Customer class

    private int id;
    private string initials;
    public Customer ()
    {
    }

    public Customer(int id, string initials)
    {
        SetId(id);
        SetInitials(initials);
    }
    public int GetId()
    {
        return id;
    }
    public void SetId(int newId)
    {
        id = newId;
    }
    public string GetInitials()
    {
        return initials;
    }
    public void SetInitials(string newInitials)
    {
        initials = newInitials;
    }
2
  • 1
    Google 'iterating through an array in c#' and see if you can solve your issue. If not come back and show us hwat you have got. Commented Dec 1, 2014 at 0:45
  • You probably mean if (L == members[0].GetId()). Customer is not an object, it is a class. Commented Dec 1, 2014 at 0:46

1 Answer 1

1

The error means exactly what it says. You can't access the GetId() function of Customer by calling Customer.GetId() because GetId() only works on an instance of Customer, not directly through the Customer class.

Customer.GetId(); //this doesn't work
Customer myCustomer=new Customer(); myCustomer.GetId(); //this works

To check the user's input against your array of inputs, you need to iterate through the array (or alternatively, use Linq).

I'm going to use a generic list, because there's not really a good reason to use arrays in most cases.

List<Customer> customers=new List<Customer>();
Customers.Add();//call this to add to the customers list.

foreach(var c in customers)
{
    if(c.GetId() == inputId)
    {
        //match!
    }
     else
    {
        //not a match
    }
}

You can also improve your Customer class by using properties, or auto properties (which doesn't need a backing field). Here's an auto property example:

public string Id {get; set;} // notice there's no backing field?

Using the above auto property syntax would allow you do to this:

var customer = new Customer();
string id = customer.Id; // notice there's no parentheses?

Properties and auto properties allow for a cleaner syntax than having to write Java-style separate getters/setters.

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you. A little bit too advance for me (beginner), but I appreciate the public string Id {get; set;} part.
I don't think it's too advanced - this is the basics of the C# language. Which part do you not understand? I'll improve it with a better example if you let me know which part.
MSDN has excellent examples. For each just iterates over all the items in a collection. It's like a for loop, except you don't have to create a variable to keep track of what position you are at. It's so handy that I almost never use plain for loops these days. Var uses type inference so you don't have to write the type of a variable. This is handy because it's shorter, and it's less things to change when refactoring (improving) code.
Okay, I think I understand. Var seems very useful. I will go more in depth with for each.

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.