head-first-csharp / fourth-edition

Code and graphics for the projects in the 4th edition of Head First C#
Other
889 stars 285 forks source link

Ch 8 Two Decks Downloadable exercise problem #37

Open Delgathar opened 2 years ago

Delgathar commented 2 years ago

Hello,

I've been scratching my head over this and can't seem to figure it out. Basically, something is not happening right with my data bindings. When I debug in local I get leftDeck error cs0103 - Does not exist in this context. But somehow rightDeck seems to work fine. At least it calls the initializer and gets populated with cards. It doesn't get cleared for some reason, but that is the next bug to figure out.

MainWindow - Copy.xaml.txt MainWindow.xaml - Copy.cs.txt

Deck - Copy.cs.txt image

As shown in the picture. Resources does populate with key 0- rightDeck and key 1-leftDeck. leftDeck was first in the xaml so I'm not sure why it is the second key and if it might be related to the problem. I tried forcing the leftDeck to populate by adding the statement to main Deck leftDeck = Resources["leftDeck"] as Deck;

this did populate the field correctly in my watch list and show it full of 52 cards, but the listBox did not populate.

Thank you for any help.

andrewstellman commented 2 years ago

Try unloading and reloading the solution in Visual Studio. Does that help?

I'd be happy to have a look at the full source. Could you publish the whole project to GitHub so I can load it in Visual Studio and have a look?

Delgathar commented 2 years ago

Hi Andrew,

Thank you for looking at this. I appreciate it. Here is my link. https://github.com/Delgathar/HFCS-Ch8-TwoDecksWPF It should be public. Unloading the solution and re-loading didn't work. Thank you again.

Delgathar commented 2 years ago

Just help with the databinding bug please. Any other bugs I'll hopefully be able to figure out myself. My problem with the databinding bug is other than checking my typing, I don't know what else to look for since that part was straight out of the book.

andrewstellman commented 2 years ago

Ok! I see the issue. On line 49 of Deck.cs remove cards. so the line looks like this:

Add(new Card((Values)value, (Suits)suit));

Then you can uncomment MainWindow.xaml.cs line 16 and you'll see your left deck populated.

You can also comment out or delete Deck.cs line 10.

Can you figure out why removing six characters from one line in Deck.cs made your project work? I'm happy to explain (or give a hint if you want), but it's actually a really good learning experience to figure it out yourself and I don't want to spoil that if you want to try to figure it out yourself. Just let me know!

Delgathar commented 2 years ago

Thank you.

I'm now extra glad I did the extra assignment. First, because it tossed this new ObservableCollection my way. Second: It gave a second chance to redeem myself with data binding since I couldn't get it to work with my bee program. Other new things like using alt-key combos and other minor reinforcements were all good as well.

Since I was starting to feel comfortable with many of the statements, it seemed all right to accept the IDE suggestions to simplify some expressions now that I knew what they simplified, but what confused me was how the program knew that Add(), Clear(), and RemoveAt() were supposed to still work when I removed "cards." Intellisence is pretty smart so I figured maybe it was because I only had one list for it to deal with and 5.0 was a little more forgiving. I would have removed"cards." on line 49 too, but the IDE didn't suggest it as with the others so I left it the way it was. Coming right out of interfaces, I also had interfaces on the brain and was thinking of ObeservableCollection as an interface, and kept wondering why there was no code to implement.

The hint that I could remove line 10 where I declared List cards sorta spells out the problem. I suppose extending ObservableCollection alone turned the class itself into a collection of Cards so adding the List field just created ambiguity, it wasn't necessary anymore and actually was hurting because "cards." was pointing to a different collection of card.

Thank you again. That was definitely a learning experience. I suppose a bit of frustration is a good teacher.

Btw, both times (bee exercise and two decks) we added to the xaml, it was by typing it directly. Is that the only way to add that specifically? I clicked on the window and went to Resources but when I clicked the new button, it just erased the statements. It didn't seem to open a box or any other interface to add these things. Is this because while the IDE will generate much of the code, there are some things that still must be done manually?

andrewstellman commented 2 years ago

Nice! Sounds like you've really figured out what's going on here. And there's definitely a valuable lesson about taking suggestions from the IDE. It's really smart—but it can't read your mind, and doesn't always know exactly what you're trying to do.

I'll to put some thought into finding ways to prompt future learners to think through this. I bet you're not the only one wondering why there's no code to implement with ObservableCollection – I can add an annotation (or maybe even a Brain Power element?) pointing out that you're extending a base class and not implementing an interface. Thanks so much for sharing your thinking on this, it's really valuable to me!

To answer your question, unfortunately Visual Studio's XAML designer doesn't really work with resources the way you'd expect. You can read a little more about it here: https://docs.microsoft.com/en-us/visualstudio/xaml-tools/how-to-create-and-apply-a-resource – I wish it worked intuitively the way you expect it to, if it did I would have used it in the book. Luckily, adding a <Window.Resources> section doesn't require a lot of extra work, so we felt comfortable enough using it in the book.

Delgathar commented 2 years ago

Thank you again. With that now working, I had one more bug. My shuffle method wasn't working right. The list was basically being divided in half each time I ran it. Changing the for loop to a while loop fixed that. Now moving on to the next assignment.

One suggestion - concerning Interfaces. I found it helped to think of Interfaces kinda like hashtags. Not only do they require certain methods, but it acted as a good meta label. #photos, #placesNotToGo, #Trees. I was thinking they might be good even as empty interfaces. An empty interface might be a place holder for code later, but in the meantime, it allows some things to work together.