Thursday, February 25, 2016

Anyone hungry?

So, I was almost done with chapter 8. It was just a few more collections that act similar to lists. They are queues and stacks. The book used language I was used to hearing in my prior job, LIFO and FIFO (last in first out and first in first out). LIFO refers to stacks and FIFO refers to queues. I thought about those and how I choose how to treat my lots when buying stocks, do I want to pay taxes on those I first bought when I sell or those that were the most recent purchase. Okay, an easier way to think about it queues: treated like a line, the people who get in line first are the first to get their food or be seated. Stacks are take the top, like Pringles. You have to take the top Pringle in the can to get to the ones at the bottom.

Alright, seems simple. Let's take a look.To create a queue you add some code like:

Queue myQueue = new Queue();

Then to add to the queue:

myQueue.Enqueue("Piece of data");

Or remove:

myQueue.Dequeue;

Remember FIFO. That "Piece of data" we put in will be the first out. And now to show stacks:

Stack myStack = new Stack();

A little different to put in a stack, we push the piece of data in:

myStack.Push("Stack data goes here");

Then to remove data from the stack we pop it out:

myStack.Pop;

Both stack and queue inherit from IEnumerable and, pretty easily, we can move our data from a queue to a stack to a list and back (probably any combination you can think of) by overloading the constructor. Example:

Queue myQueue = new Queue();
*enter data into the myQueue*
Stack myStack = new Stack(myQueue);
List myList = new List(myStack);

At the end of a chapter we created a flap jack tracker to see and feed lumberjacks (does anyone like soggy flapjacks?). A lot of concepts are starting to come together.



Next up is another lab. No help on this one, wish me luck!

Wednesday, February 24, 2016

cSharpDictionary.ContainsKey("GoFish") - Another section of chapter 8

I still am not done reading Chapter 8. Although I have been reading and coding most nights (except the nights my wife and daughter were both sick or doing my taxes which are always phone *please insert sarcasm*) it takes me a while to get through the material. 

Well, since my last post a week and a half ago, I have mostly been working on a project in chapter 8. I tried really hard not to use the help for this project and perhaps I have learned something during the week. I just finished the Go Fish project in chapter eight and I did use the code that is provided in the book. I was stuck, what was I to do? Sometimes I ask myself, is object oriented programming for me? I felt when doing VBA that I got a relatively good grasp on what I was doing and didn't have to refer to the web much after doing it for a while. Perhaps I will start feeling that way with C#. I will say there are pieces that I look at what needs to be done and a light bulb goes on. Those are the days that help me continue spending any small amount of free time I have trying to become a coder. Just code more each day I guess (I try to do CodeWars but feel sometimes they are above what I know, maybe those are the good ones to actually test my skills) and have joined some communities like codenewbie.org (full of people wanting to learn to code from all backgrounds). What helped you learn to code?

One great thing about this chapter is that it made me really look at overloaded objects. As usual, We have talked about overloading before at work and I have never really grasped what that meant. This chapter was discussing overloaded methods and I probably missed it early on in the book, but I was really feeling lost. I spent some time investigating to find out that I really didn't know much about overloaded objects. Come to find out, they can probably come in handy. I like the definition from MSDN:

Members can have the same name as long as their parameter lists differ. When two or more members in a type are the same kind of member (method, property, constructor, and so on) and have the same name and different parameter lists, the member is said to be overloaded.



Just pass the correct parameters and the code will use the correct overloaded object. The Go Fish project actually uses one so you can find what card the computer will choose and then call the method that the human player uses. pretty nifty.

Hey, you can also create a dictionary to store definitions of keys. Just do:
Dictionary kv = new Dictionary ;

You set your TValue object type and key (keys must be unique just like the dictionary you use!) and enter data. You can then count the data, see if objects are in the dictionary, remove items you don't like, and add items you do like. There seems to be a lot more you can do with dictionaries. 

GoFish! Game - I still have not won the computer, I can only tie the computer. AI must cheat.

Until next time.

"I like to fish. Fishing is always a way of relaxing." Tom Felton

Sunday, February 14, 2016

enum Ch8FirstHalf { Collections, Enums, Icomparable, IComparer }

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.



Sunday, February 7, 2016

if (catchyTitle.Text == "Interface Inheritance" ) { MessageBox.Show("Chapter 7 is done!") }

This chapter took a little longer than prior chapters. Maybe it is because there is more material and the coding projects try to use the majority of code I have learned in the prior chapters. So, since a few blogs ago I decided that I wasn't going to use the "help" as much as I had been prior. I would say I succeeded pretty well in this chapter. Building a house with places to hide I did go to solution after I got stuck and was unable to make progress. I hope it helped me learn what I was actually using better. I think it also helped that I didn't have many days between actually looking at code I started and code I was finishing. Instead of having to remind myself of what I did before, I was able to remember it pretty well.

While my code doesn't look the same as the solution, and probably has more lines then what is needed, I ended up having an issue where my form wasn't updating correctly. The issue showed itself after I implemented the interface for a hiding place. I forgot to call a method when moving from one room to another called ReDreawForm(). Hence, the form was not redrawing and giving incorrect information about where the hiding place was in the different rooms.

Well, the hide and seek project (I called it long project because when I started it the book said it was going to be a long one) contains both outside rooms and inside rooms. Both inherit from the Room class. Then there are two interfaces for rooms that have exterior doors and rooms with hiding places. I had to do some searching on what to display depending on the room type (IHasExteriorDoor or IHidingPlace). In comes upcasting and downcasting (aka Polymorphism). Example of finding a class that inherits from an interface:

if (currentLocation is IHasExteriorDoor)
            {
                goThroughTheDoor.Visible = true;
            }

That brings me to another point, Object Oriented Programming has four principles (why the book waited to chapter 7 is a good question):
  • Inheritance - one class or interface inherits from another
  • Encapsulation - creating an object that keeps track of its state internally using private fields, and uses public properties and methods to let other classes work with only the part of the internal data that they need to see
  • Abstraction - When you create a class model that starts with more general - or abstract - classes and then has more specific classes that inherit from it. Cannot instantiate these
  • Polymorphism - "Many forms" Take an instance of one class and use it in a statement or method that expects a different type, like a parent class or an interface that the class implements. Upcasting or downcasting allows us to get polymorthism.

We have used all of the principles so far, it now is a matter of being able to remember each principle and how each is used (so much to remember, but I think coding more often will help that). 

This chapter also talked about access modifiers. You know, like public or private but with a few more. 
  • Public - anything can access this (as long as access to the declaring class.) Least restrictive. Only use if you have a reason to make it public.
  • Private - only other members in the class can access it (other instances of the class can use these as well). You can only mare a class private if it is encased in another class.
  • Protected - public to subclasses, private to all else.
  • Internal - public only to other classes in the assembly. Protected Internal can only be accessed from within the assembly OR from a subclass.
  • Sealed - class which cannot be subclassed. Not an actual access modified, it only affects inheritance.

I think something to help remember the implementations, inheritance from a class allows you to modify the methods or use the methods how they are written in the base class. Interfaces have abstract methods and cannot be instantiated. The methods in the interface do not have any code in the contained methods, it only tells a class that inherited from the interface that all the methods MUST be used. The class can then implement any type of code needed for the given method, but it has a framework of what needs to be implemented from the interfaces.

I am only touching the surface I know. So much to learn!

Thursday, February 4, 2016

The Communities we belong to...

We are given time to innovate each week and sometimes I do it and sometimes I don't. I really need to get better at not only spending innovation time innovating, but also what I am innovating on. Well, we had a really interesting and fun experience at work yesterday. We were able to participate in Open Space (more information at http://openspaceworld.org/). We weren't given much information as to what the day would entail or what would be expected of us. At the beginning we discussed passion and that maybe Open Space would help Innovation Time to get some real quantifiable results (sometimes the innovation projects got lost and nothing comes of them). We were given 45 minutes blocks to bring up topics we felt passionate about. I brought up C# learning. I didn't expect many people to come to this session.

When the session started in the afternoon, I was surprised at how many people came to talk about C# learning. It makes sense, another team is going to start using C# more and wanted to see what I, a person on engineering using C# everyday, was doing to help in my learning. Our discussion led us to what I was using and how we could all learn what resources and communities others were using in order to stay on top of their coding.

I thought that I had a limited number of resources I used...but then I really looked at the sources I am using. Between blogs, people I follow on Twitter, and my network at work I actually do have quite a few resources. Of course there is the obvious one, I use the book resource that leads to most of my blogs. I feel it is a great resource, I get in-depth training on the basics of C# and I (hopefully) provide what I learn in a blog. 

At work I obviously have my co-workers that I can work with and ask questions of. One, K.C., appeared to take particular interest in me and I ask many questions of him. You could say he is acting as a mentor to me. Strange thing, I have never asked someone to mentor me that is younger than me. I am getting old, but then I guess that happens when you switch careers.

Then I follow other coders on Twitter and try to read their blogs (at least when I don't think that the subject will be a mile over my head). I like getting their perspectives and I hope that the bits of information I get from reading their blogs and posts will help in my learning. I follow (to name a few) Scott Hanselman, Troy Hunt, Jeff Atwood, John Papa, Visual Studio and those are just a few of those I feel can add to my learnings. Hey, maybe one day I will post a question in Twitter and receive a response from those I follow or that follow me.

I was lucky one day and found a retweet from one of those I follow about CodeNewbie (funny that I picked Newbie Programmer). CodeNewbie is a set of blogs, podcasts, Twitter discussions, and probably more that I haven't found out about yet. From listening to the podcast I have already found a couple great communities to use in my code adventure. Some of the stories are similar to mine, 30 somethings that switch careers and trying to learn to code. I hope to take part in the weekly discussions that are held on Wednesday nights and that I will be able to contribute my knowledge gained.

There are more like Free Code Camp (I just started following them and need to check it out more), Evasium Programmers (need to look more into this one too) and the usual Microsoft accounts to help in all things Microsoft. I know my world will keep expanding too.

So, I have a few communities I use to learn, and it continues to grow as I dive deeper into coding. In the Open Space meeting where we talked about what everyone uses, I am not aware of anything used at work for one person to pass along the resource and communities one person belongs to those that are trying to learn C#. Maybe I should start a Slack channel at work, or is there a better way to pass this knowledge on? I need to let others know what I am reading and who I am following. They may prove useful to someone else just starting to learn to code.

What communities have you found to belong to?