C
C#•5mo ago
asd

Get first item in list

I am having a very strange bug with my List<> The following code works as intended - resulting tallyTotal of first person in possiblePeople :
c#
{
Person selectedPerson;
List<Person> possiblePeople = new List<Person>();

foreach (Person person in people)
{
if(person.tallyTotal == Person.tallyBaseline)
{
possiblePeople.Add(person);
}
}
possiblePeople.Sort((x, y) => string.Compare(x.lastName, y.lastName));

consoleOutput.Text = possiblePeople[0].tallyTotal.ToString();
}
c#
{
Person selectedPerson;
List<Person> possiblePeople = new List<Person>();

foreach (Person person in people)
{
if(person.tallyTotal == Person.tallyBaseline)
{
possiblePeople.Add(person);
}
}
possiblePeople.Sort((x, y) => string.Compare(x.lastName, y.lastName));

consoleOutput.Text = possiblePeople[0].tallyTotal.ToString();
}
just changing the last line breaks it:
c#
possiblePeople[0].tallyTotal += 1;
c#
possiblePeople[0].tallyTotal += 1;
it provides the attached error (image)
No description
28 Replies
TheBoxyBear
TheBoxyBear•5mo ago
Does it rub the adding of items? Check with a breakpoint and checking the contents of the list once stoped
Pobiega
Pobiega•5mo ago
yeah verify the number of items in possiblePeople before using it your code does not take the posibility of no person in people having that tally I would personally use var firstPerson = possiblePeople.FirstOrDefault(); and check that for null before trying to modify the value
asd
asd•5mo ago
let me try that real quick I don't know how it would be possible for possiblePeople to be empty, but I'll give it a go
Pobiega
Pobiega•5mo ago
well you should verify with the debugger, as BoxyBear suggested
asd
asd•5mo ago
I'm using visual studio, not visual studio code, as this is a WPF project
Pobiega
Pobiega•5mo ago
sure, I dont see how that changes anything I just said lol VS has a very capable debugger built in
asd
asd•5mo ago
oh, I'm not too familiar with VS... I'll have to play around with it for a bit I'll just try this first
Pobiega
Pobiega•5mo ago
$debug
MODiX
MODiX•5mo ago
Tutorial: Debug C# code and inspect data - Visual Studio (Windows)
Learn features of the Visual Studio debugger and how to start the debugger, step through code, and inspect data in a C# application.
asd
asd•5mo ago
Ok i tested it there definitely IS a person at index 0 i will send a series of screenshots
Pobiega
Pobiega•5mo ago
and tallyTotal is just a public int property?
asd
asd•5mo ago
No description
asd
asd•5mo ago
No description
asd
asd•5mo ago
yup
Pobiega
Pobiega•5mo ago
if you run the program with the debugger attached but with no breakpoints, it should break when the exception is thrown (possibly thats what we are already looking at in the last screenshot)
asd
asd•5mo ago
c#
public class Person
{
public int id;
public string firstName = "Doc";

public int tallyTotal = 0;


public Person(string FirstName, string LastName)
{
firstName = FirstName;
lastName = LastName;

id = personCount;
personCount++;
}
}
c#
public class Person
{
public int id;
public string firstName = "Doc";

public int tallyTotal = 0;


public Person(string FirstName, string LastName)
{
firstName = FirstName;
lastName = LastName;

id = personCount;
personCount++;
}
}
Person object (heavily simplified)
Pobiega
Pobiega•5mo ago
you can then inspect the possiblePeople variable there those are fields, not properties fyi not the cause of your problem, but should be fixed
asd
asd•5mo ago
oh yeah my bad welp it is empty
Pobiega
Pobiega•5mo ago
🙂
asd
asd•5mo ago
for whatever reason between these two lines of code it becomes empty
asd
asd•5mo ago
No description
Pobiega
Pobiega•5mo ago
your code runs several times then
asd
asd•5mo ago
let me double check that I'm doing something very stupid here
Pobiega
Pobiega•5mo ago
and it works once or more, then fails at a later time
asd
asd•5mo ago
I can't imagine how that's possible the code runs when a WPF button is pressed just once the function is called this project is VERY bare bones right now
Pobiega
Pobiega•5mo ago
hm, well unless you have shared mutable state for this class, thats the only explanation I can come up with can you share the full source somewhere? github?
asd
asd•5mo ago
ah you're right actually the code is nested in a foreach loop, and it looks like it does work the first few times, but then breaks at the end. I didnt notice/forgot about the loop because it happens instantly... woops well tysm
Pobiega
Pobiega•5mo ago
🙂