ā Lists is the same index all the time.
Hi, i have built different lists for different houses in Hogwarts and now Im trying to add members to the different houses through a sorting hat method. But every time the same house just get members added, why?
23 Replies
break
will exit a foreach
loop early, so every time your loops run, they just do the first iteration and then stop. It should work as you expect if you take the break
s out
https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/statements/jump-statements#the-break-statementbut if i dont have the break it just loops through all the houses.
and never stops
Loops are when you want to do things repeatedly, it looks like you only want to add to a single house, so a loop isn't the right way to do it.
I want to add to a signel house every time, but the house to change to the next one for each time i press the button
so in short i want to change houses everytime a new student is created and put them in different houses after the list.
ok, I misunderstood what you wanted then - yeah a loop is not what you want
https://paste.mod.gg/miepvnmylgex/2 bit more of my code if that helps
BlazeBin - miepvnmylgex
A tool for sharing your source code with the world!
you will need to keep track of which house you added to last with an index and then whenever the button is clicked, increment the index and add them to the next house
You definitely want to add to house 1, then 2, then 3, then 4, then 1... etc and not a "random" house each time?
yes, just one two and then three and four
start with
int currentHouseIndex = 0
, then you can add them to hogwarts.Houses[currentHouseIndex]
and increment currentHouseIndex
on each click, putting it back to 0 when it's reached 4
like this?
or should i then change it to a for loop?
You're almost there, but again, you don't need the loop as you're only doing it once
ahh i dont understand..
how can i do it without loops???
try it without a loop and see what happens
okey i understand now
but now when the program gets to the last house the program crashes
close, I think your logic for the index is a little off though
you're incrementing your index but never calling the method
currentHouseIndex()
, also in that method you're setting a local variable to 0 (different from your class-level currentHouseIndex
) so the condition will always be false in thatso what should i Do?
still not working after the last houses.. š¦
š„²
also i never get to put members into the first house from the list.
You'll want to increment
currentHouseIndex
at the endIt's the perfect time to use a debugger to watch what the values are
yeah okey, so now i can go through everything, but it seems like something is wrong. Griffendor is the only one that get more members, the other ones says that it always the first member of the house. Byt the wierd part about griffendor is that after adding 1 member once to every house and then add another one griffendor says 4 members first time and 9 members second time
does that mean that all the members saves into griffendor?
it must be because the index doesnt follow because there is a new loop in the method sortinghat
got it now this is how i did it:
looks good, glad you got it working
i just have one more question, after i put the wizards in different houses i want to display the wizard in that house in a listbox and only display their name and if you double click find the other attributes
this is what i have done so far.
and put the method call inside the sortinghat button