Time table with 8 variables

Asked

Viewed 63 times

1

I have a difficult question, I have 8 teams in a table, I have simulated the results but I need to update the table using the points, so I know the amount of points of each team I wrote the following code:

        public void AtualizarTabela()
        {
        int TimePontos1 = Time1Vitoria * 3 + Time1Empate * 1;
        int TimePontos2 = Time2Vitoria * 3 + Time2Empate * 1;
        int TimePontos3 = Time3Vitoria * 3 + Time3Empate * 1;
        int TimePontos4 = Time4Vitoria * 3 + Time4Empate * 1;
        int TimePontos5 = Time5Vitoria * 3 + Time5Empate * 1;
        int TimePontos6 = Time6Vitoria * 3 + Time6Empate * 1;
        int TimePontos7 = Time7Vitoria * 3 + Time7Empate * 1;
        int TimePontos8 = Time8Vitoria * 3 + Time8Empate * 1;
        }

How can I get these Timespont variables to be in descending order and then update the table? Since I need to return after each point variable in a label.

  • It is not easier for you to use an array for this?

  • I’ve tried using the array but how do I get it out of the list in order and put it in the boxes?

1 answer

1


Here’s what I’d do:

public struct Estatistica
{
    public int Pontos { 
        get { return Vitorias * 3 + Empates; } 
    }

    public short Vitorias;
    public short Empates;
}

public void AtualizarTabela()
{
    Dictionary<string, Estatistica> times = new Dictionary<string, Estatistica>();
    time["Time 1"] = new Estatistica() { Vitorias = Time1Vitoria, Empates = Time1Empate };
    time["Time 2"] = new Estatistica() { Vitorias = Time2Vitoria, Empates = Time2Empate };
    time["Time 3"] = new Estatistica() { Vitorias = Time3Vitoria, Empates = Time3Empate };
    time["Time 4"] = new Estatistica() { Vitorias = Time4Vitoria, Empates = Time4Empate };
    time["Time 5"] = new Estatistica() { Vitorias = Time5Vitoria, Empates = Time5Empate };
    time["Time 6"] = new Estatistica() { Vitorias = Time6Vitoria, Empates = Time6Empate };
    time["Time 7"] = new Estatistica() { Vitorias = Time7Vitoria, Empates = Time7Empate };
    time["Time 8"] = new Estatistica() { Vitorias = Time8Vitoria, Empates = Time8Empate };
}

Descending:

    using System.Linq;

    foreach (var time in times.OrderByDescending(t => t.Value.Pontos)) 
    {
        // Escreva o que você precisa aqui. O nome do time está em `time.Key`.
        // O resto das informações está em `time.Value`.
        Console.WriteLine(time.Key);
    }

See an example here.


If it is Web Forms, you can do the following:

System.Web.UI.WebControls.Label[] labels = new System.Web.UI.WebControls.Label[] {
    lblTime1LugarPontos,
    lblTime2LugarPontos,
    lblTime3LugarPontos,
    lblTime4LugarPontos,
    lblTime5LugarPontos,
    lblTime6LugarPontos,
    lblTime7LugarPontos,
    lblTime8LugarPontos
};

Windows Forms:

System.Windows.Forms.Label[] labels = new System.Windows.Forms.Label[] {
    lblTime1LugarPontos,
    lblTime2LugarPontos,
    lblTime3LugarPontos,
    lblTime4LugarPontos,
    lblTime5LugarPontos,
    lblTime6LugarPontos,
    lblTime7LugarPontos,
    lblTime8LugarPontos
};

The bow, then, stays like this:

    foreach (var infoTime in times.OrderByDescending(t => t.Value.Pontos).Select((time, i) => new { Time = time, i = i })) 
    {
        // labels[infoTime.i].Text = infoTime.Time.Key;
        labels[infoTime.i].Text = infoTime.Time.Value.Pontos;
    }
  • Just a question, as I do to take the value of the team that stood at 1° and put it on a label.text now?

  • Look at my edition, if there’s no doubt.

  • Look got a little confused, I need to return in descending order on the following variables: lblTime1LugarPontos, lblTime2LugarPontos, lblTime3LugarPontos, lblTime4LugarPontos, lblTime5LugarPontos, lblTime6LugarPontos, lblTime6LugarPontos, lblTime7LugarPontos, lblTime8LugarPontos.

  • Ah, yes. Look again.

  • Gave the following error: Cannot initialize type 'System.Windows.Forms.Label' with a Collection initializer because it does not implement 'System.Collections.Ienumerable'

  • It was bad, young man. It’s sleep. It’s right System.Web.UI.WebControls.Label[] labels = new System.Web.UI.WebControls.Label[] { ... };. I’ve already edited again.

  • Only one last doubt, where I apply the lasso?

  • That’s a good question. Do you have a calculating button on that? Or an event that triggers that calculation? If so, the loop goes in it.

  • I have a table made with Labels and panels, (img: http://prntscr.com/f8q6x4) the play button activates a timer that updates the round and sets the win/lose/draw values of the teams, I need to make the values of points organized for me use as a basis to update the name of times - icons - Abels

  • It can be on the Play button, then. Note that I have already left the structure ready for you to have all the logic for the round.

  • When I played the button, the times. Order... says it does not exist in context.

  • Added using System.Linq;, as I told you in reply?

  • Yes it is already in use

  • You’re gonna need to refactor a little. times, that is to say, Dictionary<string, Estatistica> times = new Dictionary<string, Estatistica>();, is in AtualizarTabela(). times, therefore, it has to be declared globally in Form.

  • Now it says that Abels do not exist in context and "Time" cannot be accepted as the first argument of type "Anonymoustype#1"

  • Better open another question. I can’t read your screen.

  • The following is an image. http://prntscr.com/f8qbua

  • Young, if times is global, labels it is too. And it is infoTime.time.Value.Pontos. The time is tiny because the .Select() set lowercase. If you want to use Time, capital letters, change for .Select((time, i) => new { Time = time, i = i })).

Show 13 more comments

Browser other questions tagged

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