CS0747 Error with C#declared

Asked

Viewed 59 times

1

Imagem do erro CS0747

I have created a list that receives my command object powered by the fields of my Forms, my class:

public class Comanda //Essa classe carrega tudo necessário para o pedido.
{
    public int idComanda { get; set; } //número do pedido, usaremos como chave de busca
    public int nMesa { get; set; }
    public Cliente cliente { get; set; }
    public Produto produto { get; set; }
}

The problem may be here, when I create my attributes cliente and produto, they’re kind of Cliente and Produto, classes below:

public class Cliente:Pessoa
{
    public int idCliente { get; set; }
}

Cliente inherits the base class Pessoa:

public abstract class Pessoa //classe base
{
    public string nome { get; set; }
    public string endereco { get; set; }
    public string cpf { get; set; }
    public int telefone { get; set; }
}

public class Produto
{
    public int idProduto { get; set; }
    public string categoriaProduto { get; set; }
    public string nomeProduto { get; set; }
    public string descProduto { get; set; }
    public double valorUnitario { get; set; }
}

Knowing this, I instate both customer and product objects in my form where the list is, but the error persists, I tried to initialize both objects by the class constructor Comanda also, but nothing done.

Follow the code of the complete form:

public partial class frmMenu : Form
{
    Cliente cliente = new Cliente();
    Produto produto = new Produto();

    public frmMenu()
    {
        InitializeComponent();
    }

    private void btnAddProduto_Click(object sender, EventArgs e)
    {
        
        List<Comanda> cmd = new List<Comanda>(); //lista que armazena meus valores de comanda

        cmd.Add(new Comanda
        {
            idComanda = Convert.ToInt32(txtIdComanda.Text), 
            nMesa = Convert.ToInt16(txtMesa.Text),
            produto.nomeProduto = cbxProduto.Text,
            produto.valorUnitario = Convert.ToDouble(txtValorProduto.Text),
        }) ; 

        dgvComanda.DataSource = cmd;
    }


    private void btnLimparComanda_Click(object sender, EventArgs e)
    {   
    }
}

Why of CS0747 error?

  • From what you’re saying, it seems to be the case mark an answer as accepted. Here we do not write "solved" in the question. If you have an answer that really helped you, mark it as accepted. If you came to the solution on your own, put in the solution as an answer. So content is more organized and easier to find in the future by other people with similar problems.

1 answer

1

You’d have to do something like this:

if (!int.TryParse(txtIdComanda.Text out var id) {
    //faz alguma coisa para indicar que deu erro e não permite continuar
}
if (!short.TryParse(txtMesa.Text out var mesa) {
    //faz alguma coisa para indicar que deu erro e não permite continuar
}
if (!decimal.TryParse(txtValorProduto.Text out var valor) {
    //faz alguma coisa para indicar que deu erro e não permite continuar
}
cmd.Add(new Comanda {
    idComanda = id), 
    nMesa = mesa,
    produto = new Produto() {
        nomeProduto = cbxProduto.Text,
        valorUnitario = valor
    }
});

I put in the Github for future reference.

I’m creating an object like Produto and then yes By initiating its members, you cannot do this without creating the object. This object is placed in the field produto.

I took the opportunity to correct some code errors such as not testing if the conversion worked before proceeding to the execution and using the correct type for monetary value (of course you need to change in class too). Do not fix several other errors in the classes, such as allow creating an object freely without any criteria or have getter/Setter for everything even where there is no need. Some things I cannot speak without knowing more details. And I considered no mistake, but the nomenclature standard is not the one used in C#.

See more in What is the main difference between int. Parse() and Convert.Toint32()? and Differences between Parse vs Tryparse.

  • Thank you very much, you gave me a north, as for the other mistakes I will still work, on access levels and error handling, it’s all too raw yet. But I tried to instantiate the second object "Product" within the Add method and it insists on the error.

  • I forgot a part there. But I’ll give you gold advice. Try to make things simpler, you’re having difficulty with basic syntax and doing complex things, as you master the simplest part there part to the most complex, when you learn full of gaps you can’t program that is not only copy and decorate things, need to understand everything that is happening.

  • Thank you for the advice Maniero.

  • If the answer solved the problem you can accept the answer by marking the green on the left just below the votes you still can’t give. You can see more in the [tour] how it works.

Browser other questions tagged

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