[Solved] .NET Cast to Array Model Object [closed]


Your error says everything you need to know here. There’s no default cast for string to your Models.Emails type.

Emails is essentially an ID with a collection of e-mail addresses, it appears. I would recommend using the .Net System.Net.Mail namespace instead of what you’re doing here with strings, but in a lightweight application, strings are probably fine.

You will need to supply a constructor for your object, or change your Emails object properties. I would strongly recommend the latter, or even doing both:

public class Emails
{
    public int ID { get; set; }
    public string[] Addresses{ get;set; } 
    public Emails() {};
    public Emails(IEnumerable<string> list) 
    {
       this.Addresses = list.ToList().ToArray();
    }

and then in your code, each time you need a new EMails object, just construct one and add it to your list. Declare emails as a List instead of a List and bypass the need to cast:

List<Emails> emails = new List<Emails>();

and in your loops:
emails.Add(new Emails { emails = new string [] { item } });

then you can access your emails by index.
My question is, though, why are you creating a listing object but only ever putting one entry in its email collection?

3

solved .NET Cast to Array Model Object [closed]