C
C#•10mo ago
milan

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•10mo ago
Does it rub the adding of items? Check with a breakpoint and checking the contents of the list once stoped
Pobiega
Pobiega•10mo 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
milan
milanOP•10mo 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•10mo ago
well you should verify with the debugger, as BoxyBear suggested
milan
milanOP•10mo ago
I'm using visual studio, not visual studio code, as this is a WPF project
Pobiega
Pobiega•10mo ago
sure, I dont see how that changes anything I just said lol VS has a very capable debugger built in
milan
milanOP•10mo 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•10mo ago
$debug
MODiX
MODiX•10mo 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.
milan
milanOP•10mo ago
Ok i tested it there definitely IS a person at index 0 i will send a series of screenshots
Pobiega
Pobiega•10mo ago
and tallyTotal is just a public int property?
milan
milanOP•10mo ago
No description
milan
milanOP•10mo ago
No description
milan
milanOP•10mo ago
yup
Pobiega
Pobiega•10mo 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)
milan
milanOP•10mo 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•10mo 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
milan
milanOP•10mo ago
oh yeah my bad welp it is empty
Pobiega
Pobiega•10mo ago
🙂
milan
milanOP•10mo ago
for whatever reason between these two lines of code it becomes empty
milan
milanOP•10mo ago
No description
Pobiega
Pobiega•10mo ago
your code runs several times then
milan
milanOP•10mo ago
let me double check that I'm doing something very stupid here
Pobiega
Pobiega•10mo ago
and it works once or more, then fails at a later time
milan
milanOP•10mo 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•10mo 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?
milan
milanOP•10mo 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•10mo ago
🙂
Want results from more Discord servers?
Add your server