C# Label into Integer

Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

Poppey

Thread Starter
Joined
Aug 18, 2015
Messages
20
Hello,

I am trying to use a generated total to decide 1st, 2nd and 3rd places.

However, I am unable to convert the label value to an integer so I can apply me If statement.

1620291891711.png

This is what I have been trying so far.

Any help would be very appreciated.

Thank you.
 

Xsage

Dave
Joined
Jan 8, 2016
Messages
319
If Team1Total is your Label control, you cannot compare it to an Integer directly.

You either need to store one of conversions into a variable like you have with your commented out line then try to compare Team1TotalNum to 20 or do the conversion/parsing inline like this:

C#:
//WinForms:
if(Int32.Parse(Team1Total.Text) == 20)
{

}

//WPF:
if(Int32.Parse(Team1Total.Content.ToString()) == 20)
{

}
 
Last edited:

Poppey

Thread Starter
Joined
Aug 18, 2015
Messages
20
This is great! Thank you, Xsage.

I now have this:
1620298670775.png

Sorry, another question - Is there a better way to rank values?

My original idea were If statements but I do not think that is going to work :/
 

Xsage

Dave
Joined
Jan 8, 2016
Messages
319
Yes, I would but the values into an ICollection like a list, they have tons of really helpful methods for handling stuff like this.
There are also many many ways you could do this and no one way is right, but this works and is eaiser to read:

C#:
List<int> TeamScores = new List<int>();
TeamScores.Add(Int32.Parse(Team1Total.Text));
TeamScores.Add(Int32.Parse(Team2Total.Text));
TeamScores.Add(Int32.Parse(Team3Total.Text));
TeamScores.Add(Int32.Parse(Team4Total.Text));

switch (TeamScores.IndexOf(TeamScores.Max()))
{
    case 0:
        FirstPlaceLabel.Text = "Team 1";
        break;
    case 1:
        FirstPlaceLabel.Text = "Team 2";
        break;
    case 2:
        FirstPlaceLabel.Text = "Team 3";
        break;
    case 3:
        FirstPlaceLabel.Text = "Team 4";
        break;
}
EDIT
I just realised you want to rank all of the values and not just get first place, in that case you can do something like this:

C#:
Dictionary<string, int> TeamScores = new Dictionary<string, int>();

TeamScores.Add("Team 1", Int32.Parse(Team1Total.Text));
TeamScores.Add("Team 2", Int32.Parse(Team2Total.Text));
TeamScores.Add("Team 3", Int32.Parse(Team3Total.Text));
TeamScores.Add("Team 4", Int32.Parse(Team4Total.Text));

Dictionary<string, int>  OrderedTeamScores = TeamScores.OrderBy(x => x.Value).Reverse().ToDictionary(x => x.Key, x => x.Value);

FirstPlaceLabel.Text = OrderedTeamScores.ElementAt(0).Key;
SecondPlaceLabel.Text = OrderedTeamScores.ElementAt(1).Key;
ThirdPlaceLabel.Text = OrderedTeamScores.ElementAt(2).Key;
FourthPlaceLabel.Text = OrderedTeamScores.ElementAt(3).Key;
 
Last edited:

Poppey

Thread Starter
Joined
Aug 18, 2015
Messages
20
Thank you for this :)

Although I am a little lost now.

I have this:
1620315535828.png
I have not used OrderBy.


Update:

Just need - using System.Linq;


Thanks :)
 

Xsage

Dave
Joined
Jan 8, 2016
Messages
319
Sure, this stuff uses linq which in simple terms can be explained as an SQL like query language within code.
To get acces to it you need to have using System.Linq; in your imports above the namespace in the file.

A Dictionary is a ICollection like a List but allows you to specify two parts, a key and a value. So you can think of it as a tiny memory based database.

C#:
//create a new instance of a dictionary that takes a Key of type string and a Value of type int:
Dictionary<string, int> TeamScores = new Dictionary<string, int>();

//Add each of the scores for each team to the dictionary TeamScores.Add(Key: <string>, Value: <int>)
TeamScores.Add("Team 1", Int32.Parse(Team1Total.Text));
TeamScores.Add("Team 2", Int32.Parse(Team2Total.Text));
TeamScores.Add("Team 3", Int32.Parse(Team3Total.Text));
TeamScores.Add("Team 4", Int32.Parse(Team4Total.Text));

//Create a new instance of a dictionary the same as above, but instead of creating it empty, we use linq to sort the list for us
//and store the results of the sorted list into this new instance.
//1) OrderBy method sorts the list in ascending order. "x =>" basically declares x as an element within the list.
//     This is similar to saying foreach(var x in myArray). So saying x.Value orders them by the Value part of the Key Value pair
//     of the dictionary.
//2) Because for convinience we want it in decending order, so highest values is first, we want to Reverse() the collection.
//     I think you can also use OrderByDescending(x => x.Value) instead to achieve the same in one step so try that too.
//3) Finally we need to export the results to a dictionary again and we specify the x.Key as the <string> Key and x.Value as the <int> Value
//     to match our variable
Dictionary<string, int>  OrderedTeamScores = TeamScores.OrderBy(x => x.Value).Reverse().ToDictionary(x => x.Key, x => x.Value);

//the list should now be in decending order with the highest value element in the first index position. All indexes for lists and array start
//at 0. So like with array you would go Array[0] to get the first element in the array, you use dictionary.ElementAt(0) to ge the first element
//in the dictionary. Each subsiquent index will be the next highest value.
//Finally we specify we want to return the Key property of the element which we specified when adding the TeamScores to the first dictionary.
FirstPlaceLabel.Text = OrderedTeamScores.ElementAt(0).Key;
SecondPlaceLabel.Text = OrderedTeamScores.ElementAt(1).Key;
ThirdPlaceLabel.Text = OrderedTeamScores.ElementAt(2).Key;
FourthPlaceLabel.Text = OrderedTeamScores.ElementAt(3).Key;
Linq if very useful but can be quite complicated to get your head around until you've used it a few times. Even people who have used it a lot can get confused when doing complicated queries so I recommend you read up on it when you get a chance. It can be written out full like an SQL command or in shorthand like I have done in this example.
 
Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 807,865 other people just like you!

Latest posts

Members online

Top