List overwriting items

Asked

Viewed 205 times

2

I have a Data Object List, which has two attributes: Name (String) and Transactylock (Int) It’s the Data List However, with each execution of the executarSchedule method, it overwrites the item without even using the Add in the list. Can anyone tell me where the error is?

public partial class MainWindow : Window
{
    private int contador = 0;
    private Dado dadoAtual = new Dado();
    private string[] linhas;
    private string linhaAtual;
    private int transacaoAtual;
    private char operacaoAtual;
    private List<Dado> dadosBloqueioCompartilhado= new List<Dado>();
    private List<Dado> dadosBloqueioExclusivo = new List<Dado>();
    private List<string> dadosTotal = new List<string>();

    public MainWindow()
    {
        InitializeComponent();
    }

    private void executarSchedule()
    {
        linhaAtual = linhas[contador];
        if (linhaAtual.Equals("00"))
        {
            txtHistorico.Text = "Fim do schedule" + "\r\n";
            return;
        }
        else
        {
            transacaoAtual = Convert.ToInt32(linhaAtual.Substring(0, 2));
            operacaoAtual = Convert.ToChar(linhaAtual.Substring(2, 1));
            if (operacaoAtual == 'C')
            {
                txtHistorico.Text += "COMMIT(" + transacaoAtual.ToString() + ")" + "\r\n";
                if(dadosBloqueioCompartilhado.Count > 0)
                    foreach(Dado d in dadosBloqueioCompartilhado)
                    {
                        if (d.getTransacaoBloqueio() == transacaoAtual)
                        {
                            dadosBloqueioCompartilhado.Remove(d);
                            txtHistorico.Text += "Unlock(" + dadoAtual.getNome() + "," + dadoAtual.getTransacaoBloqueio().ToString() + ")" + "\r\n";
                        }
                        if (dadosBloqueioCompartilhado.Count == 0)
                            break;
                    }
            }
            else
            {
                dadoAtual.setNome(linhaAtual.Substring(3, 1));
                dadoAtual.setTransacaoBloqueio(transacaoAtual);
                if (!dadosTotal.Contains(dadoAtual.getNome()))
                    dadosTotal.Add(dadoAtual.getNome());
                if(operacaoAtual == 'R')
                {
                    if(dadosBloqueioCompartilhado.Count() == 0)
                        dadosBloqueioCompartilhado.Add(dadoAtual);
                    else
                    {
                        foreach (Dado d in dadosBloqueioCompartilhado)
                        {
                            if (!(d.getNome() == dadoAtual.getNome() && d.getTransacaoBloqueio() == dadoAtual.getTransacaoBloqueio()))
                                dadosBloqueioCompartilhado.Add(dadoAtual);
                        }
                    }
                    txtHistorico.Text += "Lock-S(" + dadoAtual.getNome() + "," + dadoAtual.getTransacaoBloqueio().ToString() + ")" + "\r\n";
                    txtHistorico.Text += "Read(" + dadoAtual.getNome() + ")" + "\r\n";
                }
                else if(operacaoAtual == 'W')
                {

                }
            }             

        }
        contador++;
    }
  • 1

    What item are you talking about? This code is bad because it mixes screen logic with business logic. It has a if completely unnecessary and another used to correct wrong gambiarra. So you can imagine that a lot of things there can be wrong and can be in dots not shown. A fairly obvious mistake is to sweep a collection with foreach and remove items from this collection, this messes up the enumeration completely. And has Add(), which causes problem in enumeration as well. Several things in this code do not seem to make the slightest sense.

  • @bigown Is the list of dataBlock Shared. Every time I add an item it removes the previous one. It is overwriting instead of incrementing. On the foreach, how then to search for an item in the list and remove it otherwise?

No answers

Browser other questions tagged

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