Is there a maximum amount of use of "using"?

Asked

Viewed 55 times

2

My initial doubt was whether it was right to use using inside using, I was able to answer that question:

It is correct to use a block using within another block using?

But I kept asking myself, is there maximum amount?

I mean, I wrote the following code:

 private void clickMovimentoEstoque()
    {
        GridView viewMat = null;

        TextBox codigo = null;
        TextBox qntBaixa = null;
        TextBox lote = null;

        if (xtraTabControl1.SelectedTabPage == xtraTabPage2)
        {
            //componentes
            viewMat = gridView2;

            codigo = textBox9;
            qntBaixa = textBox7;
            lote = textBox4;
        }
        else if (xtraTabControl1.SelectedTabPage == xtraTabPage5)
        {
            //mp
            viewMat = gridView10;

            codigo = textBox14;
            qntBaixa = textBox13;
            lote = textBox12;
        }

        using (viewMat)
        {
            using (codigo)
            {
                using (qntBaixa)
                {
                    using (lote)
                    {
                        qntBaixa.ForeColor = Color.Black;

                        //para garantir que a quantidade informada nunca vai ser maior que a quantidade pendente para baixa.
                        decimal qnt = 0, qntInformada = 0;
                        decimal.TryParse(viewMat.GetRowCellValue(viewMat.FocusedRowHandle, "QNT").ToString(), out qnt);
                        decimal.TryParse(qntBaixa.Text, out qntInformada);

                        int tipoMaterial = 0;
                        int.TryParse(viewMat.GetRowCellValue(viewMat.FocusedRowHandle, "tipoMaterial").ToString(), out tipoMaterial);

                        string un = viewMat.GetRowCellValue(viewMat.FocusedRowHandle, "UN").ToString();

                        //talvez iremos precisar comparar a unidade, pois no modelo atual, se for materia prima (tipomaterial = 0) pode dar baixa em quantidade maior que a planejada
                        //mas temos como mp mpbr, entre outros que tem sua unidade de medida como pç, nao podendo dar baixa maior que o solicidado            

                        if (!string.IsNullOrEmpty(codigo.Text) && !string.IsNullOrEmpty(qntBaixa.Text) && !string.IsNullOrEmpty(lote.Text) && qntInformada <= qnt && tipoMaterial != 0 ||
                            !string.IsNullOrEmpty(codigo.Text) && !string.IsNullOrEmpty(qntBaixa.Text) && !string.IsNullOrEmpty(lote.Text) && tipoMaterial == 0)
                        {
                            confirmaMovimento();
                        }
                        else if (!string.IsNullOrEmpty(codigo.Text) && !string.IsNullOrEmpty(qntBaixa.Text) && string.IsNullOrEmpty(lote.Text) && qntInformada <= qnt)
                        {
                            MessageBox.Show("Selecione um lote, para continuar!", "Movimentos", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        }
                        else if (string.IsNullOrEmpty(codigo.Text) && string.IsNullOrEmpty(qntBaixa.Text) && string.IsNullOrEmpty(lote.Text) && qntInformada <= qnt)
                        {
                            MessageBox.Show("Selecione um item, para continuar!", "Movimentos", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        }
                        else if (qntInformada > qnt)
                        {
                            MessageBox.Show("Quantidade informada é maior que a quantidade pendente para baixa!", "Movimento", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        }
                    }
                }
            }
        }                
    }

1 answer

5


There is nothing specified, you can use as much as you want, you will bump into other limitations, which are already very high and nobody hits them. And if knocking did the most wrong thing in the history of programming :)

This code, for example, unless it is absurdly overshadowed, needs zero usings. All objects that were used, in addition to being in incorrect form are not disposable, therefore they will not work.

  • Friend, I understood thanks for the answer, could you give me a brief idea of how to use these objects correctly? would have some link? or how to search on Google?

  • 2

    @Thomaserichpimentel, There’s an answer here at Sopt which speaks the reason for using block. In short, it is necessary when a class uses unmanaged resources. The use of unmanaged resources makes Garbage Collector, for example, not know when that instance of the object 'died' unless called the method Dispose from it - without that it will remain in memory forever. What the using block does is: at the end of the execution of that block call the method Dispose of the object implicitly (you don’t see it, but it does!).

  • 1

    @Thomaserichpimentel Most of what you need to know is where they speak of Dispose(): https://answall.com/search?tab=votes&q=dispose. A good start would be here: https://answall.com/q/173210/101. Obviously we have some tags helping hbem: https://answall.com/questions/tagged/dispose?sort=votes&pageSize=50 and https://answall.com/questions/tagged/using?sort=votes&pageSize=50

Browser other questions tagged

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