How to fill in a C #

0

I need a feature similar to the one 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.

Then I tried declaring the array with the number of records:

email[] ListaEmail = new email[nRegistros];

and fill in the fields of each record. However, each element of the E-mail List is created with null content, not with the structure, and run-time error occurs when trying to assign the 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
  {
  }
}

Sure enough, it's not the most efficient way to solve the problem. However, as the control will not have very intensive use and I have very little time to solve the problem, I tried that way. Do you have any suggestions for resolving this issue?

Thank you!

Paulo

    
asked by anonymous 23.01.2017 / 11:34

2 answers

3
  

I need a feature similar to the one 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 email ListaEmail = new email(); statement to 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); 
}
    
23.01.2017 / 12:28
0

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

    
23.01.2017 / 11:44