How to fill a list C#

Asked

Viewed 1,053 times

0

I need an appeal similar to the one made below:

     email ListaEmail = new email();
     … 
     while …
     {
       email Email = new email();
       Email.email1 = adoDR["email"].ToString();
       Email.tipo = adoDR["tipo"].ToString();
       ListaEmail.Add(Email); 
     }

However, the list does not accept the Add method().

So I tried to declare the array with the number of records:

email[] ListaEmail = new email[nRegistros];

and fill in the fields for each record. However, each element of Listemail is created with null content, not with the structure and error occurs at runtime when attempting to assign content

ListaEmail [Cont].email1 = adoDR["email"].ToString();

The code is below:

public static email[] Func_RelatorEmail(Guid IdRelator)
{
  try
  {
   string sql = "SELECT Relator_Email.IdRelator, Relator_Email.EmailId, Email.email, Email.tipo, Email.tipoSpecified "
                      + "FROM Relator_Email INNER JOIN Email ON Relator_Email.EmailId = Email.EmailId "
                      + "WHERE(Relator_Email.IdRelator='" + IdRelator + "')";

    string connString = ClsParametros.Conexao;
    SqlConnection adoConn = new SqlConnection(connString);
    adoConn.Open();
    SqlCommand adoCmd = new SqlCommand(sql, adoConn);
    SqlDataReader adoDR = adoCmd.ExecuteReader();

    // contar o numero de registros
    int nRegistros = 0;
    while (adoDR.Read())
    { ++nRegistros; }

    // criar o objeto com o numero de registros 
    email[] ListaEmail = new email[nRegistros];

    if (nRegistros > 0)
    {
      adoDR.Close();
      adoDR = adoCmd.ExecuteReader();

      int Cont = 0;
      while (adoDR.Read())
      {
         ListaEmail[Cont].email1 = adoDR["email"].ToString();
         ListaEmail[Cont].tipo = adoDR["tipo"].ToString();
         Cont++;
      }
   }
   adoDR.Close();
   adoDR.Dispose();
   adoCmd.Dispose();
   adoConn.Close();
   adoConn.Dispose();

    return ListaEmail;
  }
  catch (Exception ex)
  {
    throw new Exception(ex.Message);
  }
  finally
  {
  }
}

Certainly not the most efficient way to solve the problem. However, as the control will not have very intense use and I have very little time to solve the problem, I tried this way. Have any suggestions to solve this problem?

Thank you!

Paul

  • 2

    Your list is not a list, so it does not accept the method Add. email ListaEmail = new email(); is wrong, should be: IList<email> ListaEmail = new List<email>();

2 answers

3


I need an appeal similar to the one made below

email ListaEmail = new email();     
while …
{
    email Email = new email();
    Email.email1 = adoDR["email"].ToString();
    Email.tipo = adoDR["tipo"].ToString();
    ListaEmail.Add(Email); 
}

You need to declare a list, not a email.

Just change the statement email ListaEmail = new email(); for List<email> ListaEmail = new List<email>();


List<email> ListaEmail = new List<email>();     
while(condicao)
{
    email Email = new email();
    Email.email1 = adoDR["email"].ToString();
    Email.tipo = adoDR["tipo"].ToString();
    ListaEmail.Add(Email); 
}
  • I adjusted the control to use List, however, this function must feed the data of a Web Service, whose structure has the definition that has a set email[]. Then error appears Cannot implicitly type 'System.collection.Generic.List<email>' to email[] The way would be to convert List<email> to email[]? If so, how is the conversion performed. This is a simplified example, but I will need to do many other structures in the project. Thanks! Paulo

  • @Pauloyatsuzuka on the list, do ListEmail.ToArray() (LINQ) to convert the list into an array.

  • @Pauloyatsuzuka Exactly what Omni quoted. Include the namespace System.Linq to power this method.

  • I followed the orientation declaring as List<> and it worked. I only had to put at the end Return Listemail.Toarray(); Thank you very much!

0

To solve this problem you must use the class List<T>. The class List<T> is generic, where T is the kind you want your list to be. In your case, List<Email>. Take a look at this booklet by Caelum, I think it will help you a lot! :-)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.