C
C#ā€¢7mo ago
olleeee

āœ… Combobox writes out collection instead of the values

Hi im working in a WPF application for a university project thats based on Harry Potter. So i have built Hogwarts, and then put in the different houses, Gryffendor and so and so in different classes and built them after a house class to follow the same criteria. So the story is that you supposed to put in a password, and then fill in in a combobox which house the password is for. But when I do the combobox like bellow it just says collection, and not the differnt houses, why? Also added more code into here: https://paste.mod.gg/
BlazeBin
A tool for sharing your source code with the world!
27 Replies
Thinker
Thinkerā€¢7mo ago
You need to save the code and copy+paste the link rn the link is just to the base website, not your code
olleeee
olleeeeā€¢7mo ago
BlazeBin - uakbccbribmb
A tool for sharing your source code with the world!
olleeee
olleeeeā€¢7mo ago
public partial class MainWindow : Window
{
//create hogwarts
Hogwarts hogwarts = new Hogwarts();

string[] bloodStatus = { "halfblood", "muggelborn", "pure blood", "unknown" };

public MainWindow()
{
InitializeComponent();
AddMembers();
AddHousesToHogwarts();
AddToComboBox();

}


// the different houses
Hufflepuff hufflepuff = new Hufflepuff();
Ravenclaw ravenclaw = new Ravenclaw();
Slytherin slytherin = new Slytherin();
Griffendor griffendor = new Griffendor();


public void AddMembers()
{ //add members to the different houses.
//griffendor
griffendor.Members.Add(new Wizard("half blood", false, true, "Harry Potter"));
griffendor.Members.Add(new Wizard("mugelborn", false, true, "Harmoine Granger"));
//slytherin
slytherin.Members.Add(new Wizard("pure blood", true, false, "Draco Malfoy"));
//ravenclaw
ravenclaw.Members.Add(new Wizard("muggel", false, true, "Cho Chang"));
//hufflepuff
hufflepuff.Members.Add(new Wizard("pure blood", false, false, "Newt Scamander"));
}
public void AddHousesToHogwarts()
{
hogwarts.Griffendor.Add(griffendor);
hogwarts.Ravenclaw.Add(ravenclaw);
hogwarts.Hufflepuff.Add(hufflepuff);
hogwarts.Slytherin.Add(slytherin);

}

public void AddToComboBox()
{
cboHouses.Items.Add(hogwarts.Griffendor);
cboHouses.Items.Add(hogwarts.Hufflepuff);
cboHouses.Items.Add(hogwarts.Slytherin);
cboHouses.Items.Add(hogwarts.Ravenclaw);
}
public partial class MainWindow : Window
{
//create hogwarts
Hogwarts hogwarts = new Hogwarts();

string[] bloodStatus = { "halfblood", "muggelborn", "pure blood", "unknown" };

public MainWindow()
{
InitializeComponent();
AddMembers();
AddHousesToHogwarts();
AddToComboBox();

}


// the different houses
Hufflepuff hufflepuff = new Hufflepuff();
Ravenclaw ravenclaw = new Ravenclaw();
Slytherin slytherin = new Slytherin();
Griffendor griffendor = new Griffendor();


public void AddMembers()
{ //add members to the different houses.
//griffendor
griffendor.Members.Add(new Wizard("half blood", false, true, "Harry Potter"));
griffendor.Members.Add(new Wizard("mugelborn", false, true, "Harmoine Granger"));
//slytherin
slytherin.Members.Add(new Wizard("pure blood", true, false, "Draco Malfoy"));
//ravenclaw
ravenclaw.Members.Add(new Wizard("muggel", false, true, "Cho Chang"));
//hufflepuff
hufflepuff.Members.Add(new Wizard("pure blood", false, false, "Newt Scamander"));
}
public void AddHousesToHogwarts()
{
hogwarts.Griffendor.Add(griffendor);
hogwarts.Ravenclaw.Add(ravenclaw);
hogwarts.Hufflepuff.Add(hufflepuff);
hogwarts.Slytherin.Add(slytherin);

}

public void AddToComboBox()
{
cboHouses.Items.Add(hogwarts.Griffendor);
cboHouses.Items.Add(hogwarts.Hufflepuff);
cboHouses.Items.Add(hogwarts.Slytherin);
cboHouses.Items.Add(hogwarts.Ravenclaw);
}
SinFluxx
SinFluxxā€¢7mo ago
The combobox won't know by default what data you want it to display from the items you've added. Somewhere you need a line like: cboHouses.DisplayMemberPath = "NameOfPropertyToDisplay";
olleeee
olleeeeā€¢7mo ago
public override string ToString()
{
return this.GetType().Name;
}
public override string ToString()
{
return this.GetType().Name;
}
something like this?
Sir Rufo
Sir Rufoā€¢7mo ago
Well it seems that hogwarts.Griffendor is a collection and cboHouses.Items.Add(hogwarts.Griffendor) adds a collection to the combobox items. You put fire in the hole and wondering why there is fire in the hole ;o) I would say your class design is a little mess
SinFluxx
SinFluxxā€¢7mo ago
Ah sorry yeah I hadn't spotted that
olleeee
olleeeeā€¢7mo ago
no stress, but should that be in the hogwarts or somewhere else?
SinFluxx
SinFluxxā€¢7mo ago
what Sir Rufo is pointing out is that you're not adding individual objects to your combobox, you're adding 4 lists to it (though they all only contain 1 item each), and that really they probably shouldn't be in lists
olleeee
olleeeeā€¢7mo ago
okeyy
SinFluxx
SinFluxxā€¢7mo ago
i.e. here:
public class Hogwarts
{
// put the houses into Hogwarts
public List<Griffendor> Griffendor { get; set; } = new List<Griffendor>();
public List<Hufflepuff> Hufflepuff { get; set; } = new List<Hufflepuff>();
public List<Ravenclaw> Ravenclaw { get; set; } = new List<Ravenclaw>();
public List<Slytherin> Slytherin { get; set; } = new List<Slytherin>();
public class Hogwarts
{
// put the houses into Hogwarts
public List<Griffendor> Griffendor { get; set; } = new List<Griffendor>();
public List<Hufflepuff> Hufflepuff { get; set; } = new List<Hufflepuff>();
public List<Ravenclaw> Ravenclaw { get; set; } = new List<Ravenclaw>();
public List<Slytherin> Slytherin { get; set; } = new List<Slytherin>();
Why would you need a list for each house when there will only ever be one each of them
olleeee
olleeeeā€¢7mo ago
okej, so maybe i just do like a normal construktor and in parameter?
SinFluxx
SinFluxxā€¢7mo ago
you just need to make them not lists really
public class Hogwarts
{
public House Slytherin { get; set; }
public House Ravenclaw { get; set; }
...
public class Hogwarts
{
public House Slytherin { get; set; }
public House Ravenclaw { get; set; }
...
or have a list of Houses:
public class Hogwarts
{
public List<House> Houses { get; set; } = new List<House>();
public class Hogwarts
{
public List<House> Houses { get; set; } = new List<House>();
olleeee
olleeeeā€¢7mo ago
but then i cant use the add method to add them to hogwarts
SinFluxx
SinFluxxā€¢7mo ago
in option 1 you wouldn't need to, in option 2 you could Add them to Hogwarts.Houses Do you have to use the add method or something?
olleeee
olleeeeā€¢7mo ago
no not really, but then how do i put in all the values from lets say griffendor? ohh i see, maybe like this?
hogwarts.Slytherin = slytherin;
hogwarts.Slytherin = slytherin;
the problem now is that even though i added this:
public override string ToString()
{
return this.GetType().Name;
}
public override string ToString()
{
return this.GetType().Name;
}
the name of the solution still comes up
nohopestage
nohopestageā€¢7mo ago
Do you have to have a separate type for each house?
olleeee
olleeeeā€¢7mo ago
yes but sorted it out now, just needed it to be put in the class for the houses. thanks everybody!
SinFluxx
SinFluxxā€¢7mo ago
it would probably make more sense to just have the House class and have a Name property in it, rather than separate classes for each house
nohopestage
nohopestageā€¢7mo ago
Yeah, that's what I was thinking
olleeee
olleeeeā€¢7mo ago
i know but the assigment says to have different classes for each house šŸ˜¦
Sir Rufo
Sir Rufoā€¢7mo ago
Could you post that assignment? It sounds very strange even for an exercise
olleeee
olleeeeā€¢7mo ago
The assigment is not in English so don`t know even if I post it that it would be understandable but talked to my professor about it and they say that we need to practice OOP and hence why they want us to create many different classes and so!
SinFluxx
SinFluxxā€¢7mo ago
They definitely didn't mean a different instance of a class for each House?
Sir Rufo
Sir Rufoā€¢7mo ago
That was also on my mind šŸ˜‰ I guess I would do something like
c#
class House
{
// a bunch of properties and methods
}

class Griffendor : House {}
// etc.
c#
class House
{
// a bunch of properties and methods
}

class Griffendor : House {}
// etc.
šŸ˜ Now we have a class for each house And a big practice of OOP => Inheritance
olleeee
olleeeeā€¢7mo ago
oh you would put that in the same class as the house?
Sir Rufo
Sir Rufoā€¢7mo ago
You put everything in the parent class which is common for all child classes, yes