Error in passing C#parameters

Asked

Viewed 32 times

0

I’m making the following mistake:

Error   1   Cannot implicitly convert type 'System.DateTime' to 'string'

In the following code:

t.Codigo = GetValorDateTime(tabela, i, "DataDeContrato", DateTime.MinValue);

    public DateTime GetValorDateTime(DataTable pDados, int pLinha, String pNomeColuna, DateTime pValorPadrao)
    {
        DateTime retorno = pValorPadrao;
        if (pDados.Rows.Count > pLinha)
        {
            if (pDados.Columns.Contains(pNomeColuna))
            {
                object valor = pDados.Rows[pLinha].ItemArray[pDados.Columns[pNomeColuna].Ordinal];
                if (valor != null)
                    retorno = Convert.ToDateTime(valor);
            }
        }
        return retorno;
    }

1 answer

0


Your t.Codigo must be of the string type, and the return of the method is of the DateTime.

you can try to do so:

t.Codigo = GetValorDateTime(tabela, i, "DataDeContrato", DateTime.MinValue).ToString();

But I believe that is not what you want, a Datetime does not make sense as Code of something. Maybe you’re missing the property you want to define, possibly:

t.DataDeContrato = GetValorDateTime(tabela, i, "DataDeContrato", DateTime.MinValue);
  • 1

    Oops, that’s right. I forgot to change the name after CTRL+C CTRL+V hshshs

Browser other questions tagged

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