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.