-4

I'm a little confused about this fairly typical error. My code is as below. I am trying to add items to a list.

The compiler is saying I need an object reference for non-static field, but I can't make the class static because I am not returning a value...?

 public class ApplicantData
        {
            public string Salutation { set; get; }
            public string FirstName { set; get; }
            public string LastName { set; get; }
        }

        public class ApplicantList : List<ApplicantData>
        {
            public void Add(string salutation, string firstName, string lastName)
            {
                var data = new ApplicantData
                {
                    Salutation = salutation,
                    FirstName = firstName,
                    LastName = lastName

                };
                this.Add(data);
            }
        }

The above is called via:

List ApplicantsDetailsData = ApplicantList.Add(salutation, firstname, lastname);

I'm sure the answer must be obvious... (!)

Dean
  • 360
  • 1
  • 10
  • 27
  • 1
    You're calling ApplicantList.Add() statically. You need an instance of ApplicantList, not the class reference (otherwise you don't really have a list to add items to). – BoltClock Aug 22 '16 at 15:09
  • You need an instance of ApplicantList. You are calling the Add method as it were a static method – Steve Aug 22 '16 at 15:10
  • 1
    Notice that you are also returning `void`, where you want to assign a `List` – technikfischer Aug 22 '16 at 15:11
  • Also, your `Add` method is calling itself via `this.Add(...)` perhaps you meant to call the base class (`base.Add(...)`)? – Chris Dunaway Aug 22 '16 at 18:27

4 Answers4

2

You need an instance of a list to add things to it. Right now you only have the concept of a list. You have zero lists. For example:

var list = new ApplicantList();
list.Add("foo", "bar", "blap");

However, usually it is a bad / confusing move to subclass List<T>, IMO.

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • I am afraid to edit question, here is [why inheriting from List<> is bad](http://stackoverflow.com/q/21692193/1997232). – Sinatr Aug 22 '16 at 15:15
1

You are trying to use ApplicantList.Add like a static method.

You first need to create an object of type ApplicantList, and call Add on that object. You cannot call it directly on the class because it's not static.

Bhushan Shah
  • 1,028
  • 8
  • 20
0

You need to create an instance of your ApplicationList

ApplicantList applicantsDetailsData = new ApplicantList();
applicantsDetailsData.Add(salutation, firstname, lastname);
Jamiec
  • 133,658
  • 13
  • 134
  • 193
0

ApplicationList is a type (inheriting from List) and not an instance of that type. You can only call instance members (functions without the static keyword) on an instance of the class.

ApplicationList ApplicantsDetailsData = new ApplicationList();
applicationList.Add(salutation, firstname, lastname);
John Hoven
  • 4,085
  • 2
  • 28
  • 32