Thursday, January 14, 2016

Bee Management

Well, it is another week and I find myself saying, "I am not moving as fast as I would like." There is always more learning to be done. The amount of learning for me to feel comfortable telling people I am a developer is a large amount! Luckily, my job title is only doing part time coding, but that doesn't mean I should go slow on my learning.

This chapter took me longer than I had hoped. Not only did it take a long time, I also feel I did myself a small disservice in this chapter. The book provides the code that they want you to write. That is great when you are stuck, but I feel I was using the "help" too much. I created a Bee Management system in two parts. The first part used code that I should already know, I do. The problem, I should have taken the opportunity to learn it better. I felt I didn't have enough time to do this though and looked at the "help" more than I ought to have. I need to slow down and spend more time doing the code myself. That is a New Years resolution 14 days late.

Now, house cleaning out of the was and the confession of my sins is done. I already blogged about the inheritance in chapter 6 and, as I thought, I redid the party planner and used inheritance instead of writing extra code. Before doing that I learned a little about the hierarchy used in the code. If you don't override you can run into some issues with code not doing what you want when using inheritance.


The bee management program was done in two steps. First just writing the program that assigns the jobs to the bees, no inheritance used. The next piece was to use inheritance by adding a class that all bees used for honey consumption. Hey! I got to override the honey consumption for the queen, she apparently is real hungry when the bees work.

Not only did I override a method, I also added parameters to a subclass. This requires a little different syntax then the virtual/override. The bee class (base class) has a weight parameter and the worker bee (subclass) also tells you what jobs they can do. Do something like:

public Worker(string[] jobsICanDo, int weight) : base(weight)
(the string is an array and I hard coded the jobs a bee can do in the Form1.cs file.)

The Bee class has the weight parameter and the worker class added the jobs parameter.

So much you can do with code!

Here is the bee management program. Happy bee management!

Now that 15 days of the year have gone, I hope you are still doing your resolutions. If not, I hope you have a great new start with your old habits!

Wednesday, January 6, 2016

Inheritance

First, I did take some time off of EVERYTHING (except being a dad and husband and all the stuff I normally do at home). Okay, let me rephrase that, I took some time off work and coding. I should have spent at least some of my time of during December to learning me some code but, alas, I did not. No going back now, so now I have two weeks of family time only, which is good! Next, I just started reading the next chapter on inheritance yesterday and am not finished, but I feel I need to provide an update.

Ah, inheritance. Sometimes you inherit really great things, like the genius gene or money, and sometimes not so great things, some sort of really bad gene. Coding, you can luckily change what gets inherited, kind of. More on this later.

Coding, a subclass inherits everything from the base class (return type, fields, parameters, everything). Do something like:

Class A {
public int field1;
}

Class B ; A {
public int field2;
}

That allows the coder to do a lot less code duplication. The code above, class A has one field called field1 and class B has two fields, one called field2 and one it inherited from class A called field1. One less line of code to write, yeah!

 I started out with an exercise from the book, I took the Party Planner tool from the last chapter and added the ability to cost out birthday parties.

The two cost structures were almost identical with a few minor differences. At work I had seen, and actually used, inheritance before. I knew that it was possible, although when I used it I was more copying what I learned from Google and didn't understand what I was doing. So, when the book had me typing everything out for my new class that was pretty much the same code as another class that already existed I thought, "Man, this seems like a lot." And it was. Too much.

The book must have the readers go through that to see if we will stop and think that there has to be a more simple way (the reader should know that there is because it alludes to this fact at the beginning of the chapter). I am just past this point in the book and I think in the next few pages it will have me create another project, probably just like the party planner before, but this time use a base class for my overall Party and have sub-classes: one for dinner parties and one for birthday parties (just my guess but knowing the structure of the book, it is probably a pretty good guess, we will see on the next post...ooh, I cliff hanger).


WAIT! What about my kind of statement above? I did get a little further after re-writing all my party code. I read that you can change something that gets inherited from the base class. You just need to make sure your method is marked with "virtual" like below:

In Class A:

public virtual void CoolMethod () {
   //do some CoolMethod here
}

This needs to be followed by an override in the subclass like, say class B that inherited from A:

public override void CoolMethod() {
  //do an even more CoolMethod here
}

No you inherited everything else that is in class A, such as field1, but the CoolMethod does some other awesome stuff that A doesn't do, and also has field2 (see above).

More to come on inheritance.

A few definitions:
Subclass: A class that inherits fields and methods from a base class

Override the method: A subclass changes the behavior of an inherited class

Class Hierarchy: Classes at the top are base classes with subclasses below it. Those subclasses can have their own subclasses that inherit from them

P.S. Maybe I should spend more time this week learning GitHub...I deleted the Chapter 6 code...again! Anyway, I took a shortcut on recreating the code..You can get the program here.

https://github.com/robertjorg/Ch6

Saturday, December 26, 2015

A Christmas GitHub

Well, I know that I am passed my week deadline. December seems to be a busy month, don't you think?

In the time since I last wrote, I was able to create a Github account using the name robertjorg and create a few repositories for the coding I have done. I first tried just one repository for all the code that I have done for the C# book. Maybe I should have kept just one repository for the entire book, but I decided I was going to break up the repositories by bigger projects. Each bigger project in the book will have it's own repository. Which is the better way? I still am not sure and am still learning about forks, repositories, and working together in GitHub. Maybe I will decide that I should have one repository for the entire book as a move along and discover more.

I had a great experience (and I am glad I wasn't developing something that I will need down the road)! I somehow deleted all of my code I had written for the book. I saw it there, then when I was moving from one repository to many, it was deleted. Not sure what I did, but I am glad I wasn't more attached to the code. I had saved the Day at the Races code on my Google Drive so that was easily recovered. I had one more chapter of code that I lost (to save time I went to the books website and downloaded the code). Lesson learned, when trying something new, make sure I have a backup so I don't lose the code. Then again, maybe GitHub removes the need to have to create backups as long as I am using it correctly? I was using the Desktop client, which seems to be pretty user friendly, and plan to continue using that (unless someone tells me there is a better way of doing things).

I found another GitHub repository that contains some code for A Day At the Races. I checked the solution for walbalooshi and it they did implement some of the same code that I did, but was pretty different. walbalooshi has three repositories with two appearing to be solutions for the C# book and only the projects (maybe they are two of the labs).  The Day at the Races as five forks. I am interested to learn if those that are forking that code are  forking to help make changes to the original code?

I hope to keep learning and using GitHub more. Maybe one day I will be a paying customer because I will need a private repository or two.


Thursday, December 17, 2015

A better understanding

Well, I now have a better understanding of the get set accessors. I think the blog is a great idea. I have a coworker, K.C., that has been reading all my posts and reached out a few hours after I posted on Tuesday. K.C. and I spoke about my questions on Wednesday.  My team leader, Taylor, read and knew I had questions as well.I made sure to reread pieces of the book and look at the code I had written for the chapter to try and gain a better understanding before speaking with anyone at work. I spoke a few minutes with Taylor and he helped with my understanding. K.C. and I spent close to half an hour going over my questions.

Now, what did I learn? First, just like I said last time, I need to go back and create a list of vocabulary used so I can stop confusing the terms. I will be doing that this week. Maybe writing down the words and their definitions will help me retain them better. K.C. was great and gave me some history that the book didn't present. A property is a C# creation and not known by other languages. Without the idea of property there will be a few more lines of code (it actually looked like A LOT more coding).

The get portion of the property is used anytime the property is read and returns the value of our private property. The set accessor is called when the public property needs to be set and in turn can set the private object in the class. Anytime the private object changes, there may be other objects that need to be changed, hence our private object.

Constructors, no return value. Constructors allow us to initialize the private fields in a class.Then to change our private property we have to go through our public property to change the private field. Using the property will change everything needed to change (the reason for our private property).

Automatic Property: public int MyProperty { get; set; } - C# automatically creates the private piece needed when using the generic structure.
Field: Variable declared directly in a class

I had some co-workers speak to me about how I was putting my code and programs on the web. I have just been using my Google drive. What about something like Git? I will be working on that and my definitions list. 

Tuesday, December 15, 2015

Privatey privates

Another week down. I understand why you want pieces to be private. I don't want everyone knowing all of my business and I am a pretty private guy. I like my privacy. There are certain things we all tell people and other pieces we keep to ourselves (or share with only a select few). I like the sentence at the beginning of the chapter that states "...good objects don't let other objects go poking around their fields." Enter encapsulation.

Encapsulation allows you to keep your privates private without impacting the integrity of your program. I can tell my class that, hey, this parameter or method is private and only this class can use it. It's like keeping a secret. The problem becomes, when do I make something private vs. public? I guess that is a subject that one really needs to think about when programming. Some pieces are ok to be viewed by the entire program while others we want to keep locked down, we just want to get it write to help with the readability and ability to understand our code. Nothing new to programmers that have been in the game, but I found this piece helpful.

To help with encapsulation you have properties, or the get and set accessors. I know, right? Honestly, I see that these work, but am fuzzy on the how they work. Ok, I reread a paragraph. It states "A property can be used to get or set a backing field, which is just a name for a field set by a property." I need to go back and keep a list of vocabulary words. I have to keep looking up what the different words mean. Anyway, I am fuzzy here, but I could just use the automatic properties and we will be all good right? I mean, type prop, tab, tab and it is done! Well, I still need to understand what this is doing to make the program work as expected in a clean way. Plus, I am sure just throwing gets and sets out there doesn't help with encapsulation and determining if I should have a public or private object.

More reading this week on this subject...watch for it

A screen shot from the program the book helped walk me through

Tuesday, December 8, 2015

A Day at the Races...Finally

Well the day has come. I finally finished the first lab in the C# book. The book reads that we should know everything you need to in order to complete the lab. I found there were a few things that either I glossed over or the book did not touch on the subject. That was moving pictures in the Windows Form. A quick Google search (yes I used Google in the lab) helped me understand that I needed to find the location of the picture and then I change the location by changing one of the points (X axis or Y axis). I then joked with my cousin (who is a much better coder than I am) that I didn't know how to move the dogs and he said to use dog.Move();. Great idea I said, except they returned dog.NotListen();.

Here is the opening screen of the wonderful Day at the Races app.
The dogs and the race track are 5 pictures and, as discussed above, the key was figuring out how to get the dogs move by changing the point on one of the axis. That movement is supposed to be random. The randomness comes between zero and four spaces forward (I decided to add 0 to try and make it more random). Well, it is random, but the races are always very close. You have to stay on the edge of your seat in anticipation.

And don't worry, you can see where the dogs have been! They leave a trail of...let's call it droppings. I tried a few different sets of code in the Run() object to fix this issue. I found that I was reliably able to get all the dogs to run at the same speed and finish at the same time, except it always had dog number one win. Hey, you could make a lot of money betting on number one! I found that if I used Application.DoEvents(); the droppings don't appear anymore, but the races get stuck in an infinite loop. Then if I add a break; the loop is broken, but all the dogs finish at the same time. I am thinking it maybe has to do with the order which I have the code written, but I tried rearranging and in some cases made it a little better, but generally made things worse.

This was the first project in the book that required multiple classes in order to run. I have a guy, greyhound, and bet class. It was fun trying to instantiate an instance of bet for the guy class (or is that backwards?). I have instantiated an instance or two at work, but I have not fully understood what or why I was doing it. I feel that having walked through having to write a  program that uses an instance of another class helped me understand these much better.

In the end I wasn't given much direction on how to write the code, just a skeleton frame. I added a few objects that weren't in the original skeleton I was given. I hope it was needed and that I am not just adding objects that aren't needed (I will say I used all the objects somewhere in the code).

I want to include the SLN so that if anyone wants to take a look and provide constructive feedback they can. This is the first time I have ever shared code, so I hope I included everything that is needed. If it matters I have the solution saved at C:\Source\Personal\CSharpBook\labADayAtTheRaces (maybe it matters for where it will look for the dog and racetrack pictures). Here is the code and sln files (code was written in Visual Studio 2015).

Well, here is the Day at the Races (don't worry, no actually money is exchanged so it is legal in every state). Have fun playing with Joe, Bob, and Al's money. Happy betting!

12/8/2015 Update - I was able to fix the dog droppings. The pictures were not not refreshing so each picture was left behind leaving a trail of the back of the dog legs. All it took was talking to my team lead about the issue and he said, "Try this.Refresh();" Easy fix. No more trails. (I have updated the link above).

Tuesday, December 1, 2015

My day at the races (or a few weeks...so far)

The C# book tells me I should know everything to make the day at the races app. Well, I may know it, maybe I am not spending enough time thinking about it. Or, maybe I am spending too much time thinking about it and I need to take a step back. Honestly, I did take some time off from thinking about the program. If you read my prior posts, I was in Orlando for a week at the Visual Studio Live! conference. It was very informative and was a great experience. Then it was Thanksgiving. Lots of food and family.

Now back to the app. I have the visual pieces done. The pictures of the dogs are waiting for the code to tell them what to do. I have the skeleton framework done for what I need to do. I also have one class done (I think, I haven't been able to test it yet as I need more of the code in to test that the betting section works).

Will I finish this week? We will see.