Storing data into list with class

C#.NetList

C# Problem Overview


I have the following class:

public class EmailData
{
    public string FirstName{ set; get; }
    public string LastName { set; get; }
    public string Location{ set; get; }
}

I then did the following but was not working properly:

List<EmailData> lstemail = new List<EmailData>(); 
lstemail.Add("JOhn","Smith","Los Angeles");

I get a message that says no overload for method takes 3 arguments.

C# Solutions


Solution 1 - C#

You need to add an instance of the class:

lstemail.Add(new EmailData { FirstName = "John", LastName = "Smith", Location = "Los Angeles"});

I would recommend adding a constructor to your class, however:

public class EmailData
{
    public EmailData(string firstName, string lastName, string location)
    {
        this.FirstName = firstName;
        this.LastName = lastName;
        this.Location = location;
    }
    public string FirstName{ set; get; }
    public string LastName { set; get; }
    public string Location{ set; get; }
}

This would allow you to write the addition to your list using the constructor:

lstemail.Add(new EmailData("John", "Smith", "Los Angeles"));

Solution 2 - C#

If you want to instantiate and add in the same line, you'd have to do something like this:

lstemail.Add(new EmailData { FirstName = "JOhn", LastName = "Smith", Location = "Los Angeles" });

or just instantiate the object prior, and add it directly in:

EmailData data = new EmailData();
data.FirstName = "JOhn";
data.LastName = "Smith";
data.Location = "Los Angeles"

lstemail.Add(data);

Solution 3 - C#

You need to new up an instance of EmailData and then add that:

var data = new EmailData { FirstName = "John", LastName = "Smith", Location = "LA" };

List<EmailData> listemail = new List<EmailData>();
listemail.Add(data);

If you want to able to do:

listemail.Add("JOhn","Smith","Los Angeles");

you can create your own custom list, by specializing System.Collections.Generic.List and implementing your own Add method, more or less like this:

public class EmailList : List<EmailData>
{
    public void Add(string firstName, string lastName, string location)
    {
        var data = new EmailData 
                   { 
                       FirstName = firstName, 
                       LastName = lastName,
                       Location = location
                   };
        this.Add(data);
    }
}

Solution 4 - C#

One way(in one line) to do it is like this:

listemail.Add(new EmailData {FirstName = "John", LastName = "Smith", Location = "Los Angeles"});

Solution 5 - C#

You need to create an instance of the class to add:

lstemail.Add(new EmailData
                 {
                     FirstName = "JOhn",
                     LastName = "Smith",
                     Location = "Los Angeles"
                 });

See How to: Initialize Objects by Using an Object Initializer (C# Programming Guide)


Alternatively you could declare a constructor for you EmailData object and use that to create the instance.

Solution 6 - C#

You're not adding a new instance of the class to the list. Try this:

lstemail.Add(new EmailData { FirstName="John", LastName="Smith", Location="Los Angeles" });`

List is a generic class. When you specify a List<EmailData>, the Add method is expecting an object that's of type EmailData. The example above, expressed in more verbose syntax, would be:

EmailData data = new EmailData();
data.FirstName="John";
data.LastName="Smith;
data.Location = "Los Angeles";
lstemail.Add(data);

Solution 7 - C#

You are attempting to call

List<EmailData>.Add(string,string,string)
. Try this:

lstemail.add(new EmailData{ FirstName="John", LastName="Smith", Location="Los Angeles"});

Solution 8 - C#

How do you expect List<EmailData>.Add to know how to turn three strings into an instance of EmailData? You're expecting too much of the Framework. There is no overload of List<T>.Add that takes in three string parameters. In fact, the only overload of List<T>.Add takes in a T. Therefore, you have to create an instance of EmailData and pass that to List<T>.Add. That is what the above code does.

Try:

lstemail.Add(new EmailData {
    FirstName = "JOhn", 
    LastName = "Smith",
    Location = "Los Angeles"
});

This uses the C# object initialization syntax. Alternatively, you can add a constructor to your class

public EmailData(string firstName, string lastName, string location) {
    this.FirstName = firstName;
    this.LastName = lastName;
    this.Location = location;
}

Then:

lstemail.Add(new EmailData("JOhn", "Smith", "Los Angeles"));

Solution 9 - C#

This line is your problem:

lstemail.Add("JOhn","Smith","Los Angeles");

There is no direct cast from 3 strings to your custom class. The compiler has no way of figuring out what you're trying to do with this line. You need to Add() an instance of the class to lstemail:

lstemail.Add(new EmailData { FirstName = "JOhn", LastName = "Smith", Location = "Los Angeles" });

Solution 10 - C#

Here's the extension method version:

public static class ListOfEmailDataExtension
{
    public static void Add(this List<EmailData> list, 
        string firstName, string lastName, string location)
    {
        if (null == list)
            throw new NullReferenceException();

        var emailData = new EmailData
                            {
                                FirstName = firstName, 
                                LastName = lastName, 
                                Location = location
                            };
        list.Add(emailData);
    }
}

Usage:

List<EmailData> myList = new List<EmailData>();
myList.Add("Ron", "Klein", "Israel");

Solution 11 - C#

And if you want to create the list with some elements to start with:

var emailList = new List<EmailData>
{
   new EmailData { FirstName = "John", LastName = "Doe", Location = "Moscow" },
   new EmailData {.......}
};

Solution 12 - C#

  public IEnumerable<CustInfo> SaveCustdata(CustInfo cust)
        {
            try
            {
                var customerinfo = new CustInfo
                {
                    Name = cust.Name,
                    AccountNo = cust.AccountNo,
                    Address = cust.Address
                };
                List<CustInfo> custlist = new List<CustInfo>();
                custlist.Add(customerinfo);
                return custlist;
            }
            catch (Exception)
            {
                return null;
            }
        }

Solution 13 - C#

EmailData clsEmailData = new EmailData();
List<EmailData> lstemail = new List<EmailData>(); 

clsEmailData.FirstName="JOhn";
clsEmailData.LastName ="Smith";
clsEmailData.Location ="Los Angeles"

lstemail.add(clsEmailData);

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionNate PetView Question on Stackoverflow
Solution 1 - C#Reed CopseyView Answer on Stackoverflow
Solution 2 - C#slandauView Answer on Stackoverflow
Solution 3 - C#Christian HorsdalView Answer on Stackoverflow
Solution 4 - C#TheBoyanView Answer on Stackoverflow
Solution 5 - C#George DuckettView Answer on Stackoverflow
Solution 6 - C#Daniel MannView Answer on Stackoverflow
Solution 7 - C#Tetsujin no OniView Answer on Stackoverflow
Solution 8 - C#jasonView Answer on Stackoverflow
Solution 9 - C#DavidView Answer on Stackoverflow
Solution 10 - C#Ron KleinView Answer on Stackoverflow
Solution 11 - C#Sunny MilenovView Answer on Stackoverflow
Solution 12 - C#user3364059View Answer on Stackoverflow
Solution 13 - C#Mayank PandeyView Answer on Stackoverflow