C
C#ā€¢7mo ago
olleeee

āœ… 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?
public void SortingHat(Wizard wizard, List<House> hogwarts)
{
foreach (House house in hogwarts)
{
house.Members.Add(wizard);
break;
}

}
public void SortingHat(Wizard wizard, List<House> hogwarts)
{
foreach (House house in hogwarts)
{
house.Members.Add(wizard);
break;
}

}
23 Replies
olleeee
olleeeeā€¢7mo ago
private void btnSortingHat_Click(object sender, RoutedEventArgs e)
{
Wizard newWizard = new ();

Wizard student = CreateNewWizard(newWizard);

foreach (House house in hogwarts.Houses)
{
house.SortingHat(newWizard, hogwarts.Houses);
break;
}
}
private void btnSortingHat_Click(object sender, RoutedEventArgs e)
{
Wizard newWizard = new ();

Wizard student = CreateNewWizard(newWizard);

foreach (House house in hogwarts.Houses)
{
house.SortingHat(newWizard, hogwarts.Houses);
break;
}
}
dan in a can
dan in a canā€¢7mo ago
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 breaks out https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/statements/jump-statements#the-break-statement
olleeee
olleeeeā€¢7mo ago
but if i dont have the break it just loops through all the houses. and never stops
hime
himeā€¢7mo ago
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.
olleeee
olleeeeā€¢7mo ago
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.
dan in a can
dan in a canā€¢7mo ago
ok, I misunderstood what you wanted then - yeah a loop is not what you want
olleeee
olleeeeā€¢7mo ago
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!
dan in a can
dan in a canā€¢7mo ago
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
SinFluxx
SinFluxxā€¢7mo ago
You definitely want to add to house 1, then 2, then 3, then 4, then 1... etc and not a "random" house each time?
olleeee
olleeeeā€¢7mo ago
yes, just one two and then three and four
dan in a can
dan in a canā€¢7mo ago
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
olleeee
olleeeeā€¢7mo ago
int currentHouseIndex = 0;
if (currentHouseIndex >= 4)
{
currentHouseIndex = 0;
}

Wizard newWizard = new ();
Wizard student = CreateNewWizard(newWizard);

foreach (House house in hogwarts.Houses[currentHouseIndex])
{
currentHouseIndex++;
house.SortingHat(newWizard, hogwarts.Houses);
MessageBox.Show($"{newWizard.Name} is now member {house.Members.Count} in {house}\n" +
$"{house.HouseGhost} is going to take well care of you.");
break;
int currentHouseIndex = 0;
if (currentHouseIndex >= 4)
{
currentHouseIndex = 0;
}

Wizard newWizard = new ();
Wizard student = CreateNewWizard(newWizard);

foreach (House house in hogwarts.Houses[currentHouseIndex])
{
currentHouseIndex++;
house.SortingHat(newWizard, hogwarts.Houses);
MessageBox.Show($"{newWizard.Name} is now member {house.Members.Count} in {house}\n" +
$"{house.HouseGhost} is going to take well care of you.");
break;
like this? or should i then change it to a for loop?
hime
himeā€¢7mo ago
You're almost there, but again, you don't need the loop as you're only doing it once
olleeee
olleeeeā€¢7mo ago
ahh i dont understand.. how can i do it without loops???
dan in a can
dan in a canā€¢7mo ago
try it without a loop and see what happens
olleeee
olleeeeā€¢7mo ago
public void currentHouseIndex()
{
int currentHouseIndex = 0;
if (currentHouseIndex > 3)
{
currentHouseIndex = 0;
}
}


private void btnSortingHat_Click(object sender, RoutedEventArgs e)
{

Wizard newWizard = new ();
Wizard student = CreateNewWizard(newWizard);


currentHouseIndex++;
House currentHouse = hogwarts.Houses[currentHouseIndex];

currentHouse.SortingHat(newWizard, hogwarts.Houses);
MessageBox.Show($"{newWizard.Name} is now member {currentHouse.Members.Count} in {currentHouse}\n" +
$"{currentHouse.HouseGhost} is going to take well care of you.");



}
public void currentHouseIndex()
{
int currentHouseIndex = 0;
if (currentHouseIndex > 3)
{
currentHouseIndex = 0;
}
}


private void btnSortingHat_Click(object sender, RoutedEventArgs e)
{

Wizard newWizard = new ();
Wizard student = CreateNewWizard(newWizard);


currentHouseIndex++;
House currentHouse = hogwarts.Houses[currentHouseIndex];

currentHouse.SortingHat(newWizard, hogwarts.Houses);
MessageBox.Show($"{newWizard.Name} is now member {currentHouse.Members.Count} in {currentHouse}\n" +
$"{currentHouse.HouseGhost} is going to take well care of you.");



}
okey i understand now but now when the program gets to the last house the program crashes
dan in a can
dan in a canā€¢7mo ago
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 that
olleeee
olleeeeā€¢7mo ago
so what should i Do?
int currentHouseIndex = 0;




private void btnSortingHat_Click(object sender, RoutedEventArgs e)
{

if (currentHouseIndex > 3)
{
currentHouseIndex = 0;
}


Wizard newWizard = new ();
Wizard student = CreateNewWizard(newWizard);

currentHouseIndex++;
House currentHouse = hogwarts.Houses[currentHouseIndex];
int currentHouseIndex = 0;




private void btnSortingHat_Click(object sender, RoutedEventArgs e)
{

if (currentHouseIndex > 3)
{
currentHouseIndex = 0;
}


Wizard newWizard = new ();
Wizard student = CreateNewWizard(newWizard);

currentHouseIndex++;
House currentHouse = hogwarts.Houses[currentHouseIndex];
still not working after the last houses.. šŸ˜¦ šŸ„² also i never get to put members into the first house from the list.
SinFluxx
SinFluxxā€¢7mo ago
You'll want to increment currentHouseIndex at the end
hime
himeā€¢7mo ago
It's the perfect time to use a debugger to watch what the values are
olleeee
olleeeeā€¢7mo ago
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:
private void btnSortingHat_Click(object sender, RoutedEventArgs e)
{
Wizard newWizard = new ();
Wizard student = CreateNewWizard(newWizard);


House currentHouse = hogwarts.Houses[currentHouseIndex];

currentHouse.SortingHat(newWizard, currentHouse);
MessageBox.Show($"{newWizard.Name} is now member {currentHouse.Members.Count} in {currentHouse}\n" +
$"{currentHouse.HouseGhost} is going to take well care of you.");
currentHouseIndex++;
if (currentHouseIndex > 3)
{
currentHouseIndex = 0;
}


}
private void btnSortingHat_Click(object sender, RoutedEventArgs e)
{
Wizard newWizard = new ();
Wizard student = CreateNewWizard(newWizard);


House currentHouse = hogwarts.Houses[currentHouseIndex];

currentHouse.SortingHat(newWizard, currentHouse);
MessageBox.Show($"{newWizard.Name} is now member {currentHouse.Members.Count} in {currentHouse}\n" +
$"{currentHouse.HouseGhost} is going to take well care of you.");
currentHouseIndex++;
if (currentHouseIndex > 3)
{
currentHouseIndex = 0;
}


}
public void SortingHat(Wizard wizard, House currentHouse)
{
currentHouse.Members.Add(wizard);

}
public void SortingHat(Wizard wizard, House currentHouse)
{
currentHouse.Members.Add(wizard);

}
dan in a can
dan in a canā€¢7mo ago
looks good, glad you got it working
olleeee
olleeeeā€¢7mo ago
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.
public void FillLists()
{

lstGryffindor.ItemsSource = hogwarts.Griffendor.Members;
lstHufflepuff.ItemsSource = hogwarts.Hufflepuff.Members;
lstRavenclaw.ItemsSource = hogwarts.Ravenclaw.Members;
lstSlytherin.ItemsSource = hogwarts.Slytherin.Members;

}
public void FillLists()
{

lstGryffindor.ItemsSource = hogwarts.Griffendor.Members;
lstHufflepuff.ItemsSource = hogwarts.Hufflepuff.Members;
lstRavenclaw.ItemsSource = hogwarts.Ravenclaw.Members;
lstSlytherin.ItemsSource = hogwarts.Slytherin.Members;

}
and put the method call inside the sortinghat button
Want results from more Discord servers?
Add your server
More Posts