Monday, January 23, 2012

Chapter 3, Thoughts.

Chapter 3 was interesting to me in a number of ways.  While all but one topic of discussion was something that I had learned previously, I found it great that the book delved into basic exception handling and type casting, which I think are important to cover early on.  This also allows students to learn the way it "should" be done, rather than coming up with their own round-about solutions.  For instance:

// Convert the Ultimate Answer To Life, The Universe, and Everything from string literal to decimal.
            try
            {
                ultimateAnswerToLifeTheUniverseAndEverything = decimal.Parse(HitchhikerGuideToTheGalaxy.Text);
// Display the number '42'
                MessageBox.Show(ultimateAnswerToLifeTheUniverseAndEverything);
            }

Can be worked around by just showing the .Text field in the MessageBox, which of course means that you cannot really "work" with the answer as a number of type Int/Float/Decimal/Double, but rather just "show" it:

// Convert the Ultimate Answer To Life, The Universe, and Everything from string literal to decimal.
            try
            {
// Display the number '42'
                MessageBox.Show(HitchhikerGuideToTheGalaxy.Text);
            }

I am especially impressed by them introducing the

            try
            {
                // Try statements here.
            }
            catch (Exception errMsg)
            {
                // Catch exceptions, handle error.
            }

statements before introducing statements like IF/THEN/ELSE and SELECT CASE:

// IF/ELSE/THEN example:
    static int Example(int myVar)
    {
 if (myVar == 0) // myVar is equal to 0.
 {
     return -1;
 }
 else if (myVar <= 10) // myVar was less or equal to than 10.
 {
     return 0;
 }
 else  // myVar is not equal to zero, and not less than 10 (meaning it was greater than 10).
 {
     return 2;
 }
    }

// SELECT/CASE/SWITCH Example:
 switch (myVar )
 {
     case -1:  // myVar was equal to 0.
  MessageBox.Show(myVar);
  break;
     case 0:  // myVar was less or equal to than 10.
  MessageBox.Show(myVar);
  break;
     case 2:  // myVar was greater than 10
  MessageBox.Show(myVar);
  break;
 }
    }

Most tutorials/books that I've read usually teach If/ELSE/THEN, move into a SELECT/CASE (in C# it uses the "switch" statement, I guess, and THEN get into Try/Catch. I wonder if this book covers boxing/unboxing (similar to type casting, but instead of casting a specific type for a variable, it converts the value type to a reference type.  Mainly used when an object's parameters require it, but we don't care about the "type" of the parameter). I should probably just read ahead, but I haven't had the time!

4 comments:

  1. You always have the most interesting information! I will definitely use it as reference if needed! I hope this class isn't too much of a bore for you. Plus who really has the time for anything fun anymore, like "reading ahead". :)

    ReplyDelete
    Replies
    1. Thanks! Nah, the class won't bore me. Since I've been doing the system administration type jobs for so long, I really only know HOW to program...not what I actually need to type. Put another way - I know the theory and best practices, but have never actually done it myself...at least not with the newer languages of the past 8 years or so. While this class may be easy, I still find it fun - and it got me the extra 4 credits I needed in order to be classified as a Full Time student ;-)

      I did some reading ahead, but then got sucked into an alternate path...next thing I know, I was reading about PowerShell on Microsoft Technet (which is why I made a post about PowerShell today, VERY similar to C#, but it's a scripted language).

      Delete
  2. Thanks for the insight and links to further my knowledge. I will likely look into the mono-project as it seems interesting. How do you paste the snippets of code into the blog so that they can be scrolled like your powershell codes?

    ReplyDelete
    Replies
    1. I'll post a "how-to" after I get home from class tonight.

      Delete