0

Hy, I'm stuck a bit. I'm trying to reach List's fields in other class. Here is my code example:

Loader class:

public List<Contact> LoadLicenses()
    {
        var listOfClients = new List<Contact>();     

        using (var connection = new SqlConnection())
        {
            connection.ConnectionString =
                ConfigurationManager.ConnectionStrings["ContactManagerContext"].ConnectionString.ToString();
            connection.Open();
            string sql = "SELECT * FROM [dbo].[Contacts]";
            using (var command = new SqlCommand(sql, connection))
            {
                using (var reader = command.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        var licenses = new Contact();

                        licenses.ContactId = Convert.ToInt32(reader["ContactId"]);
                        licenses.Name = reader["Name"].ToString();
                        licenses.Address = Convert.ToInt32(reader["Address"]);
                        licenses.Email = reader["Email"].ToString();
                            listOfClients.Add(licenses);
                    }       
                }       
            }
        }
        return listOfClients;
    }

ClientDuration class:

public List<Contact> DurationLeft(/*DateTime _date*/)
    {
        Loader loader = new Loader();
        List<Contact> clientContacts = new List<Contact>();




         return clientContacts;
    }

In ClientDuration class I would like to do something like to reach fields (Email, Address,...) from List witch was declared in Loader class. I call LoadLicenses in DurationLeft method and then assign it to new List with same type as it was in Loader class. But then I can't reach fields. How to do it? Please help!

Spider man
  • 3,224
  • 4
  • 30
  • 42
Nikas Žalias
  • 1,594
  • 1
  • 23
  • 51
  • how did you tried accessing the fields? – Joseph Jul 01 '15 at 07:22
  • have you tried something like `clientContacts[0].Name`? – Szer Jul 01 '15 at 07:24
  • I tried like this: ClientContacts = loader.LoadLicenses(); ClientContacts. (nothing happens)... – Nikas Žalias Jul 01 '15 at 07:25
  • You wrote 'I call LoadLicenses in DurationLeft' but it's not in your example. Where do you call `loader.LoadLicenses();`? – nilsK Jul 01 '15 at 07:25
  • 1
    From other class you should [not access fields](http://stackoverflow.com/q/295104/1997232), but properties. You can expose list as [read only](https://msdn.microsoft.com/en-us/library/e78dcd75.aspx). – Sinatr Jul 01 '15 at 07:25

2 Answers2

0

try this

public List<Contact> DurationLeft(/*DateTime _date*/)
 {
        Loader loader = new Loader();
        List<Contact> clientContacts = new List<Contact>();
         clientContacts =loader.LoadLicenses();
         var firstemailfield=clientContacts[0].Email;


         return clientContacts;
  }
Jagadeesh Govindaraj
  • 6,977
  • 6
  • 32
  • 52
0

You have a collection of Contacts you must single out one contact before you access the properties/fields of it

I.e.

foreach(Contact contact in clientContacts)
 {
        Console.WriteLine(contact.name):
}

Or

clientContacts[0].name // access first contact in list
BhavO
  • 2,406
  • 1
  • 11
  • 13