First, let me start by staying that I may have finally figured GitHub out. I haven't deleted any projects for a while (yeah!) and I actually started using it on two computers. It was helpful this weekend as the computer I have been using my work computer to do all the coding. Well, just the other day I installed VS 2015 Community on my home computer, just in case, with GitHub. As I was coding tonight, I couldn't find my charger for my work computer so it came in handy that I had everything installed so I just cloned my chapter 8 repository and I was back in business. Worked like a charm!
Now, chapter 8, well at least the first part of it. Collections - pretty nice, they allow you to store, sort, and manage all the data that your programs need. I bet these will come in real handy when coding. Let's check them out.
First up was Enummerate Values, a.k.a. Emum. These are a data type that only allows certain values for a piece of data. I have seen those before! Actually, only a few times at work, but I have seen them and now I understand what they are used for and why. We have talked about enums in our stand-ups or in water cooler conversations and now I can actually add to the conversations! "Hey, just take that enum and assign a number value to it and BAM!"
Example to restrict the suits a card can have:
public enum Suit
{
Heart,
Spade,
Diamond,
Clubs,
}
Sweet! I can make sure the correct values are the only ones used. But! Enums have their limitations. What if I have an unknown amount of items to put in, or you have objects you need to put in a list instead of just a type? *Hint* Use a list
Lists take objects and you can assign anything to it as long as it is polymorphic. That leaves it wide open. So you have your enum that tells you what suits you can use and say that you have one that tells you the values that can be used for your cards. Now you can create a list that contains a card with both a suit and a value (you need a Card class as well):
List cards = new List();
for (int i = 0; i <= 4; i++)
{
cards.Add(new Card((Suit)random.Next(4), (Value)random.Next(1, 14)));
Console.WriteLine(cards[i].Name);
}
Your lists will automatically resize itself when objects are added or removed. Arrays (talked about in another blog) can't do that so you will need to know how many objects to include in an array or leave the correct number blank spots to add to the array. (I understand .Net has a bunch of collections to solve these issues, but I don't know about them yet and I am told List is the most common type).
You also have IComparable and IComparer to help sort your lists. Takes a little bit of coding, but essentially you create an integer output and tell it if it should be higher or lower by returning a -1, 1, or 0 (the same value). Create a method called Compare in IComparer and give it two object parameters then do like below:
class CardComparer_byValue : IComparer
{
public int Compare(Card x, Card y)
{
if (x.Value > y.Value) return 1;
else if (x.Value < y.Value) return -1;
else
{
if (x.Suit > y.Suit) return 1;
else if (x.Suit < y.Suit) return -1;
else return 0;
}
}
(I know that isn't as efficient as it could be, but I didn't use the books help on this activity AT. ALL!)
Also, remember that if you are comparing Enums it will use the index number assigned. The enum above for the suit would sort by Heart, Spade, Diamond, and then Clubs.
As mentioned above, I haven't used the help yet. I have used my prior projects to check my code or find an example to modify for my new project use. I am feeling pretty good.
https://github.com/robertjorg/Ch8/tree/master/FiveRandomCards
https://github.com/robertjorg/Ch8/tree/master/ListOfDucks/ListOfDucks - I think we will be using this one later as the book had me add some methods that we haven't used yet.
Now, chapter 8, well at least the first part of it. Collections - pretty nice, they allow you to store, sort, and manage all the data that your programs need. I bet these will come in real handy when coding. Let's check them out.
First up was Enummerate Values, a.k.a. Emum. These are a data type that only allows certain values for a piece of data. I have seen those before! Actually, only a few times at work, but I have seen them and now I understand what they are used for and why. We have talked about enums in our stand-ups or in water cooler conversations and now I can actually add to the conversations! "Hey, just take that enum and assign a number value to it and BAM!"
Example to restrict the suits a card can have:
public enum Suit
{
Heart,
Spade,
Diamond,
Clubs,
}
Sweet! I can make sure the correct values are the only ones used. But! Enums have their limitations. What if I have an unknown amount of items to put in, or you have objects you need to put in a list instead of just a type? *Hint* Use a list
Lists take objects and you can assign anything to it as long as it is polymorphic. That leaves it wide open. So you have your enum that tells you what suits you can use and say that you have one that tells you the values that can be used for your cards. Now you can create a list that contains a card with both a suit and a value (you need a Card class as well):
List
for (int i = 0; i <= 4; i++)
{
cards.Add(new Card((Suit)random.Next(4), (Value)random.Next(1, 14)));
Console.WriteLine(cards[i].Name);
}
Your lists will automatically resize itself when objects are added or removed. Arrays (talked about in another blog) can't do that so you will need to know how many objects to include in an array or leave the correct number blank spots to add to the array. (I understand .Net has a bunch of collections to solve these issues, but I don't know about them yet and I am told List
You also have IComparable
class CardComparer_byValue : IComparer
{
public int Compare(Card x, Card y)
{
if (x.Value > y.Value) return 1;
else if (x.Value < y.Value) return -1;
else
{
if (x.Suit > y.Suit) return 1;
else if (x.Suit < y.Suit) return -1;
else return 0;
}
}
(I know that isn't as efficient as it could be, but I didn't use the books help on this activity AT. ALL!)
Also, remember that if you are comparing Enums it will use the index number assigned. The enum above for the suit would sort by Heart, Spade, Diamond, and then Clubs.
As mentioned above, I haven't used the help yet. I have used my prior projects to check my code or find an example to modify for my new project use. I am feeling pretty good.
https://github.com/robertjorg/Ch8/tree/master/FiveRandomCards
https://github.com/robertjorg/Ch8/tree/master/ListOfDucks/ListOfDucks - I think we will be using this one later as the book had me add some methods that we haven't used yet.
No comments:
Post a Comment