Catching and Storing Exceptions in SSIS Script Tasks

I use SSIS (SQL Server Integration Services) very often to write data extraction and migration packages. More recently I have been using SSIS to write an interface to update a third party system through calling web services. The SSIS package runs as a scheduled task and queries a database table (call this the interface table) which holds records that have been updated in the source system, that also need updating in the third party system. By using the supplied web services, I am ensuring that all business logic is maintained when updating the third party system, I am also ensuring that no support agreements are invalidated as I am not performing direct updates to the database.

When the package runs, I need to maintain an audit trail of which records were successfully updated and which were not. There are 3 ways in which a record will not be updated, these are:

  1. The record from the source system did not exist in the third party system (This should be a rare occurrance as the source system is updated overnight from the third party system).
  2. Whilst programmatically the call to the web service was successful, the record could not be updated as the web service returned an unsuccessful response.
  3. An exception was thrown in the SSIS package.

Within the SSIS package, I am using a Script Task to perform all the calls to the web services and process the response. The first two items in the list above are handled fairly easily by examining the response coming back from the web service call and updating the status of the record in the interface table.

But what happens if there is an exception thrown in the Script Task in the SSIS package? Well, I would find out eventually that there was an issue, when the customer tells me there is and I have to go and start digging around to find out why the package failed. If I looked in the interface table and there was a status next to the record, I could inform them why the update did not occur. If exceptions are not handled properly, there will be no status information in the interface table saying that an exception has been thrown and I then have to start looking at the job history in SQL Server to find out when and why this happened.

So, how do we handle this in our Script Task? Catching exceptions is easy enough in code and originally I wrote the following code:

try
{
    .. code that could throw an exception ..
    Dts.TaskResult = (int)ScriptResults.Success;
}
catch (Exception e)
{
    Dts.Events.FireError(-1, "Task Name", e.message, String.Empty, 0);
    Dts.TaskResult = (int)ScriptResults.Failure;  
}

Within the Error Event Handler in the package, I added a simple Execute SQL Task, that used the contents of the System::ErrorDescription variable to update the status message.  Forcing the Script Task to throw an exception (by changing the URL of the web service end-point), the exception information was written back to the interface table, unfortunately all I ended up with as a status was “error in script task” – not exactly useful!! I know there was an error, but I want to know what it was!!

After some further digging and a lot of research, I found a solution to the problem. To overcome the problem, you need to create a package variable that is going to hold the exception message and you need to set this in your catch block. Normally when we work with variables in a Script Task we have to populate the ReadOnlyVariables and/or the ReadWriteVariables lists in the task configuration. For this solution to work, we do not add our package variable to the ReadWriteVariables list.

The code I have ended up with that works is as follows.

try
{
    .. program code that could throw and exception ..
    Dts.TaskResult = (int)ScriptResults.Success;
}
catch (Exception e)
{
    Variables exceptionVariable = null;
    Dts.VariableDispenser.LockOneForWrite("User::ScriptException", 
                                          ref exceptionVariable);
    exceptionVariable["User::ScriptException"].Value = e.Message;
    exceptionVariable.Unlock();

    Dts.Events.FireError(-1, "Task Name", e.Message, String.Empty, 0);
    Dts.TaskResult = (int)ScriptResults.Failure;
}

Looking at the code above, we have a package variable called ScriptException (remember do not add this to the ReadWriteVariables list!), I then modified my Execute SQL Task for the Error Event Handler to work with the User::ScriptException package variable instead of the System::ErrorDescription variable. When forcing the code to throw an exception, looking at the interface table, the full exception message is now held.

Whilst I don’t want my code to throw exceptions and I always strive for my code to be exception neutral, should the code throw an exception, I can now at least start by looking at the interface table and gain a good understanding of where things went wrong.

 

More Scratch String Custom Blocks

On September 22nd I blogged about a set of custom string blocks I implemented for Scratch. These have been very well received and I am glad people are finding them useful. I did say that I was going to add some more custom blocks to manipulate strings, and I am pleased to say that I have now added four more blocks to the Scratch project. I have now implemented the following blocks:

LTRIM

LTRIM allows you to trim any leading spaces from a string (Left Trim). Using an example, consider the string ”     Scratch” (quotes used to show the spaces). You can pass this string into the LTRIM block and it will remove all the leading spaces, giving you the string “scratch”. You use the block as follows:

LTrim BlockThe trimmed string is stored in the variable result.

RTRIM

RTRIM allows you to trim any trailing spaces from a string (Right Trim). Again using an example where the string is in quotes, if you call the block with the string “Scratch    “ you will receive a string back with the trailing spaces removed. The block is used as follows:

RTrim BlockThe trimmed string is stored in the variable result.

TRIM

TRIM is a combination of LTRIM and RTRIM and will remove any leading and trailing spaces. If you look at the block in Scratch, it shows a good example of being able to reuse exisiting blocks. All TRIM does is call the LTRIM and RTRIM blocks. As an example, if you call the block with the string ”    Scratch    “ all of the leading and trailing spaces are removed. You use the block as follows:

Trim BlockAs with the previous two blocks, the trimmed string is returned in the variable result.

SPLIT

SPLIT is an extremely useful block and in the programming languages I use on a daily basis, it is implemented and I use it often. SPLIT allows you to split a string at a certain character. A simple example of this is splitting a sentence into individual strings. If we passed in the string “I like programming in Scratch” and tell it to split on spaces, the SPLIT block will split this into indiviudal strings as follows:

I
like
programming
in
Scratch

I very often have to write programs that work with comma seperated values (CSV). In my programs, I would read a line of data from a file that could look like “phil curnow,11/04/1972,41” – This just shows a simple line with my name, date of birth and age. Now, using my language of choice, which is C#, if I wanted to display each of these values on screen, I could write some program code that looks like this:

string str = “phil curnow,11/04/1972,41”;

foreach (string s in str.Split(‘,’))
    Console.WriteLine(s);

This would print each of the three items on a seperate line on the screen. All I am saying in the program is that I want to split the string wherever there is a comma.

So how can we implement this in Scratch? Well, this is where lists come in to play. If we pass a string into our custom block and tell it to split on a space, each of the ‘sub-strings’ can be added to a list, and this is exactly how I have implemented the block.

To call our custom block, we need to supply two parameters, the string we want to split and the character we want to split on. So, looking at the example below, we will pass in the string I like programming in Scratch and the second parameter, although looks blank, is a space.

Split BlockAll our sub-strings are added to a list called SplitList and once the block has been executed, SplitList looks as follows:

SplitListUsing my example of the CSV list, we could pass in the string phil curnow,11/04/1972,41 into the block as follows:

Split Block CSVAnd our SplitList list would then contain:

CSV Split ListAs you can see SPLIT is a very powerful and above all, useful block to have.

Do you want to see anymore string blocks?

We now have several very useful custom blocks to manipulate strings in our Scratch projects. Are there any other blocks you would like to see for manipulating strings? If you can think of any other blocks, leave a comment on here or drop me a tweet and time permitting, I will see if I can implement it for you.

 

How the Scratch Custom Upper and Lower case blocks work

Last week I wrote a post about the custom string handling blocks I have written for Scratch. These have been very well received and I have had quite a few positive tweets back about them. I’m glad people are finding them useful!

Two of the blocks I have written allow you to convert a string to either upper or lower case. Now Scratch 2 when dealing with strings or characters is case insensitive, by this I mean if you perform a comparison of A = a Scratch will evaluate these to be the same. To clarify this, lets look at the block of code below.

Character ComparisonIf you run this block, you will see that the variable result is set to 1. This is what I mean when I say case insensitive, we may consider that A is not the same as a. So what does this have to do with converting our string to upper or lower case? Well, I’ll cover this a little later and you will see how this case insensitivity can help us!

ASCII Codes

If I was asked to write a very basic case changing method (same as a block in Scratch) in say C# (which is my preferred language), I may well consider looking at ASCII codes for characters, this is ignoring the fact that there is already two methods available in the .NET Framework to convert between upper and lower case!!. What is ASCII? well, I’m not going into the depths of explaining this here, but if you look at the Wikipedia entry for it at http://en.wikipedia.org/wiki/ASCII this will give you a full overview of it. ASCII codes can be our friend here for converting between case, if you consider the ASCII code for A is 65 (decimal) and the ASCII code for a is 97 (decimal), you can see there is a difference in the code values of 32. This works for B/b and C/c, etc.

So a method to convert upper case to lower case could loop over all the characters in a string and check the ASCII value for it. If the value is between 65 and  90, (A to Z), we can consider it to be an upper case letter, if we then add 32 to the ASCII value, this will give us the lower case version. We want to range check the letter value to ensure we are working with an upper case letter, otherwise the resulting string would look a little strange.

The small C# method below is something that will convert upper to lower case. Now at this point any C# programmer looking at the method would probably pick it to pieces and to be honest I can too. If I was going to write a version for production code it would be vastly different to this. This method has been written to explain my points above and nothing more.

string ToLowerCase(string str)
{
    string result = String.Empty;

    for (int loop = 0; loop < str.Length; loop++)
    {
        if ((int)str[loop] >= 65 && (int)str[loop] <= 90)
            result += Convert.ToChar((int)str[loop] + 32);
        else
            result += str[loop];
    }

    return result;
}

Passing the string abcd&ABCD to this method would return the string abcd&abcd. Great! we have a working conversion routine. So lets go and implement this in Scratch.

Our Scratch Version

Right, so now we know we can work with the ASCII codes for letters, we just convert what we have above to the corresponding Scratch code, right? …. Well, no we don’t sadly. Scratch does not give us the ability to work with the ASCII codes for letters, so this method will not work. Great! so how can we do this then? Well, remember I explained at the beginning of this post that Scratch is case insensitive and it can be our friend, well this is where it becomes our best friend!

When I was implementing my string blocks, I really did think that case conversion had to be included, so with a little thought figured there must be a way of doing this. The trick with solving any problem like this is to have a look at what we have to work with in Scratch. Whilst the C# method is very nice, we have to throw most of it away and think again. My eureka moment came when I thought about lists. My thinking here when converting to lower case was as follows:

1. Have a list that contains all the lower case letters a through to z
2. Loop through every character in our string and see if it exists in our list
3. If it exists, use the character found in the list (which will be the lower case character)
4. If it is not found in the list, use the original character.

So, putting this together, I generated a list called LowerCase which looks like this:

Lowercase ListThis method will work even if we pass over lowercase letters, we will still get our character from the list, which will be lower case. So, my Scratch ToLowerCase block looks like this:

ToLowerCase BlockOur block starts by initialising a couple of variables. result will hold our converted string and charCount is used by the repeat block to loop through every character in our string. I have then implemented another custom block called GetLowerCaseLetter. Splitting out code like this into another block makes the code more readable and also gives us yet another block that allows us to do single character conversion. Our GetLowerCaseLetter block looks in our list and finds the lower case letter we need. After calling this block, our lower case letter is held in the variable letterResult so we simply add this to our result variable. We then add 1 to the character count and go around the loop again. This keeps going until we have worked with every character in our string.

The GetLowerCaseLetter Block

This is where most of the work is done. Lets have a look at this block.

GetLowerCaseLetter BlockAgain at the beginning we initialist a couple of variables. loop is used to loop through our LowerCase list. letterResult is used to return our converted character. We are then going to loop through the list a maximum of 26 times (remember a to z!!). When we are in the loop, we check if the letter we have passed to the block, e.g. A is in our list. So using a simple if block we are essentially saying if A = a then set letterResult to the item found in our list, which will be a. As we don’t want to go around the loop again, we stop this script. At this point letterResult holds a which is the lower case version of what we passed in. If we had passed in a it would have found this and letterResult would hold a.

If our block has got as far as the end of the loop, we have another if block that says if letterResult is empty, we must have passed in a character other than a letter of the alphabet, so just simply return it. This ensures anything like %&*() is still kept in the converted string.

So putting this to the test, if we use the block like this:

Call ToLowerCaseThe string we will get back is abcd&abcd, which is the lower case version of what we passed in!

Converting to Upper Case

Converting to upper case is exactly the same, we just use a list that contains upper case A to Z. If you look at the ToUpperCase block in the Scratch project, you will see it is exactly the same as its lower case version. The GetUpperCaseLetter block again is exactly the same as its lower case version.

So, again as you can see, whilst our Scratch version of converting to upper and lower case is very different to what we may do in other programming languages, if you sit back and think about the problem and consider what you have to work with in Scratch, there is generally always a way of implementing what you want to do.

Scratch Custom String Blocks

Having the ability to manipulate strings is one of the most powerful facilities you can have in a programming language. In my day to day coding I manipulate strings an awful lot. You may want to be able to get specific characters in a string, convert the whole string to uppercase characters or lowercase characters. Many years ago, one of the first programming languages I learnt was BASIC (http://en.wikipedia.org/wiki/BASIC). BASIC has several functions for allowing you to manipulate strings, for example:

LEFT$(string, n)

LEFT$ allows you to return the LEFT most n characters from your string. For example if you use the command PRINT LEFT$(“Scratch”,3) the letters Scr would be printed to the screen. The n parameter tells the function how many characters starting at the beginning of the string you want.

RIGHT$(string, n)

Similar to LEFT$, RIGHT$ allows you to return the RIGHT most n characters from your string. So something like PRINT RIGHT$(“Scratch”, 3) would print tch to the screen. The n parameter tells the function how many characters starting at the end of the string and working backwards you want.

MID$(string, start, n)

MID$ is quite a powerful function. It allows you to get a certain amount of characters, but starting at any point in the string. So using a command like PRINT MID$(“Scratch Programming”, 9, 7) would print Program to the screen. The start parameter tells the function where you want to start in the string and the n parameter tells the function how many characters you want to return from the starting point.

UPPER$ and LOWER$

Some other functions available are UPPER$ and LOWER$. These return either an uppercase or lowercase version of your string. So something like PRINT UPPER$(“scratch”) would print SCRATCH and PRINT LOWER$(“SCRATCH”) would print scratch. Again, very useful!

How can we do this in Scratch?

Out of the box, Scratch does not have these kind of blocks built in. As we know, Scratch 2 allows us to write our own custom blocks, so we could write our own versions of these. What I have done is build versions of the functions explained above as custom blocks that can be used in your Scratch programs.

I have made these available at http://scratch.mit.edu/projects/12402145/. If you look inside the project, all the custom blocks are defined on the stage, with an example of how you would use each block next to the block definition. I will given an explanation below on how you would use each custom block.

Please Note: The resulting string from each of these blocks is stored in a variable called ‘result’.

Scratch LEFT$

This has been implemented as a custom block called Left. You would use the block like this:

Scratch Left BlockThe first parameter is the string or the variable holding the string you want to work with. The second parameter is the number of characters from the left you want. The result is stored in a variable called result. Looking at the example above, after calling this block, the result variable would hold Scr.

Scratch RIGHT$

This has been implemented as a custom block called Right. You would use the block like this:

Scratch Right BlockThe first parameter is the string or variable holding the string you want to work with. The second parameter is the number of characters from the right that you want. The result is stored in a variable called result. Looking at the example above, after calling this block, the result variable would hold tch.

Scratch MID$

This has been implemented as a custom block called Mid. You would use the block like this:

Scratch Mid BlockWe have three parameters that we have to supply to this block. The first parameter is the string or variable holding the string you want to work with. The second parameter is where you want to start in the string. The final parameter is the number of characters you want to get from the string. The result is stored in a variable called result. So, looking at the example call above, we can see that we will be working with the string Scratch Programming and we want to start returning characters from the 9th character in the string and that we want to return 7 characters. Starting at the 9th character (P) we will get 7 characters, which will give us the string Program.

Scratch UPPER$ and LOWER$

I have also implemented blocks that allow strings to be converted to upper of lower case. These blocks are called ToUpperCase and ToLowerCase. As with all the other blocks, the result is stored in a variabled called result. You would use the blocks like this:

Scratch ToUpperCase BlockScratch ToLowerCase BlockCalling the ToUpperCase block on the string shown would convert it to SCRATCH and calling the ToLowerCase block on the string shown would convert it to scratch. If you supplied a string such as ScRaTcH to either of these blocks, it will still work as expected.

I am going to write a further blog post on how the ToUpperCase and ToLowerCase blocks work, because Scratch is a little different to most languages when comparing upper and lower case characters. The code for the other blocks is fairly easy to follow and figure out whats going on.

Anymore Blocks?

I will be implementing more string manipulation blocks over a period of time. The blocks I want to implement next are:

  – SPLIT(string, char)
  – LTRIM(string)
  – RTRIM(string)
  – TRIM(string)

SPLIT enables you to split one string into several strings, splitting the string at a specific character. For example if you supplied a string I Program In Scratch and told the block to split on the space character, you would end up with 4 strings containing:

  I
  Program
  In
  Scratch

Each of these strings would be held in a list.

LTRIM and RTRIM removes all spaces to either the left or the right of a string. So if you had a string ”    Scratch” (quotes used to show the spaces) and passed the string to the LTRIM block, it would remove all the leading spaces. Similarly, if you had the string “Scratch   ” and passed the string to the RTRIM block, it would remove all the trailing spaces. TRIM performs a union of LTRIM and RTRIM and will remove all leading and trailing spaces.

As these blocks are added, I will add a post to this blog and will also add a comment in the Scratch forums and on the ScratchEd web site.

Use and Share!

I do hope that you find these blocks useful and feel free to use and modify them in any way you would like. Share them around with your fellow Scratchers.

The Power of Twitter – Crazy Weather in Falmouth

Hail Storm in September I’ve been a Twitter user for quite a while now and have to say I do think its a great invention. I mainly use it for fun, but do follow people related to the IT world, and as I travel frequently, follow an awful lot of the travel update feeds. Yesterday it was a great little tool for showing just how strange things can get!!

We had a very bizarre weather event here in Falmouth yesterday at about 5pm. The picture to the left shows what it looked like here from my house. Now considering its September and we have had one of the hottest summers for 7 years, you don’t expect to look out of your window and see the whole place white with cars struggling to get up the road! We’d had a severe weather warning put in place for heavy rain, but for 45 minutes we had one of the heaviest hail storms I have ever seen.

Watching the roads get worse and cars struggling to get up the hill was getting a little concerning, considering our road can get busy (well busy for an estate road anyway!!). What did it for me was watching a 4×4 skid up the road from one side of the road to the other and almost hit a car that had stopped as it couldn’t get any further up the hill. I said to my wife, I think I will tweet BBC Travel South West (@BBCTravelSW) about this. BBC Travel South West is fantastic and I always look at the updates from them on Twitter before I do a long journey.

The Power of Twitter!

I tweeted them with the picture above telling them where it was and how bad it was. Within a few minutes, they had re-tweeted, and they have one or two followers! I also follow a local weather feed as well, Kernow & Channel Islands Weather (@kernowweather – http://cciweather.co.uk). As they like to know about strange weather events around Cornwall I tweeted them the picture. I didn’t think anything more about it until my phone started pinging off twitter notifications. There was certainly a fair amount of re-tweeting going on and I was glad people were enjoying the picture.

Deciding to have an early night I was reading my book (yes the phone was on the bedside table) and Twitter started again. This time I was being tweeted by a Journalist from the BBC and from a newspaper in Devon asking if they could use the picture on their sites, feel free I said!!

Kernow and Channel Islands Weather then tweeted back saying thanks for the picture and they have a full weather report on their site with my little snap of the hail. My wife then comes down this morning and says, “Is that your picture on the Western Morning News website?” (http://www.westernmorningnews.co.uk/) — Really?? Quick trip over to their website and yes, there it is with a report on the weather.

Whats the big deal you posted a picture on Twitter!

Yep, so what, its only a picture! My point here is that whilst Twitter has had a bit of a bashing one way or another in the news, its down to what you post for the world of Twitter to see. Whilst I only originally posted the picture to BBC Travel South West to let them know what was happening in Falmouth at the time, to give warning to people travelling or wanting to travel in the Falmouth direction, judging by some of the tweets I’ve had back people have enjoyed seeing the picture and a couple of the news papers seem to like it too. This kind of freak weather event doesn’t happen very often and its a great way to record and share it with others.

Happy tweeting!

 

First Simple App for Windows Phone 8

Having had my Windows Phone for a couple of months now, and having set up a Windows 8 virtual machine to do some app development, I finally decided to sit down and bang out a simple app, to see how easy it is.

To be honest if your skill set follows the form of C#, WPF/Silverlight you won’t have many problems at all. You will need to have Visual Studio 2012 and the Windows Phone 8 SDK installed, but thats a fairly easy (but somewhat lengthy!) process to complete.

As I do a fair amount of driving and tutor Advanced Driving, for my first app I decided to put together a Stopping Distance calculator. I’ve done these for Android and BlackBerry, so thought I would do it for Windows Phone as I have all the calculations to hand.

The idea of the calculator is a simple one. Supply your speed in Miles per Hour or Kilometres per Hour and it will calculate the Braking Distance, Thinking Distance and the overall Stopping Distance (Thinking+Braking). I’ve added a couple of screen shots below of the app running in the emulator. It certainly seems to run fine, and the whole process of developing it from start to finish was about 2 hours. Not bad for a first app.

Screen 1 – Inputs

The inputs into the app are very simple, your speed and from the list, select your unit of measure (MPH or KPH). This is a list picker and will expand when you want to make your selection and then contract when you have made the selection. Simply then tap the Calculate button and you are taken to the results page showing the calculation results.

App Screen 1Screen 2 – Results

The Thinking Distance, Braking Distance and overall Stopping Distance is then displayed with the results in metres and feet. You can then tap another button to take you back to the input screen to allow you to calculate using another speed/unit of measure.

app screen 2

A far from complicated app, but someone going through a driving test, or Advanced driving test does need to know stopping distances. A simple little app that can aid revision of stopping distances.

I think I’ll tidy up the screens, make them a little more visually appealing and then upload to the Windows Phone App Store.

Visual Studio 2012 Error: No exports were found that match the constraint in…

I use a Windows 8 Virtual Machine for any Windows Phone 8 app development I do. I hadn’t fired up my VM for quite a while and there were a whole host of Windows updates to be installed.

After installing the updates and running Visual Studio 2012, I tried to load one of my Windows Phone App solutions. When trying to view any of the pages in Visual Studio, I received the error:

No exports were found that match the constraint:
ContractName
Microsoft.VisualStudio.Project.IProjectServiceAccessor
RequiredTypeIdentity
Microsoft.VisualStudio.Project.IProjectServiceAccessor

Very annoying considering that before the updates were installed everything worked fine. To overcome the issue, what you can do is delete the folder ComponentModelCache, which can be found in this path:

C:\Users\<<user>>\AppData\Local\Microsoft\VisualStudio\11.0\ — where <<user>> is the users folder.

This has worked fine for me and looking at Google, has worked for many other people.

Running Windows Phone 8 Emulator Outside of Visual Studio 2012

Sometimes it is useful to be able to run the Windows Phone 8 emulator without having to create a solution within Visual Studio 2012. Most of the time I am testing apps that I am writing, so launch through Visual Studio, but I have had the need to not do this. After a bit of Googling, the answer became quite clear on how to do this. Never one to keep useful snippets of information to myself, I’ve given the steps below on how to do it.

1. As an administrator, open a command prompt window
2. Navigate to the folder C:\Program Files (x86)\Microsoft XDE\8.0 (CD Program Files (x86)\Microsoft XDE\8.0)
3. Execute the following command: xde -vhd “c:\program files (x86)\Microsoft SDKs\Windows Phone\v8.0\Emulation\Images\Flash.vhd”

The emulator should then execute as expected. If you have installed the SDK to a different location, you will need to modify the folders/drive specified above, but if you have a default installation, the above should work fine.

Recursion in Scratch 2

With the release of Scratch 2 came the ability to create your own blocks, which in programming terms is the ability to create your own procedures or methods. One thing you can’t do with custom blocks is to return values from any computation performed in your block. You need to assign the result of any computation to a variable.

In this post we are going to look at recursion. The first question you may ask is “what is recursion?” — Simply put, recursion is the ability for a function/method (I tend to use the term method, but in the context of this post, they mean the same thing) to call itself. Now, with the advent of custom blocks in Scratch, recursion becomes possible. Before custom blocks, a form of recursion was possible in Scratch using broadcast blocks. There is a complete article on the Scratch Wiki about recursion, and I would recommend you read it. it can be found at http://wiki.scratch.mit.edu/wiki/Recursion.

So, first, lets have a simple recursion example. Consider you want to be able to count backwards from a specific number down to 1, easy, just use a loop, and to be honest that would be the best way to do this, but this simple example does give a good, simple introduction to recursion. First, lets have a look at how we would do this in a loop, defining a custom block.

Count Loop

We can then call this by simply using the CountLoop block and providing a parameter, say 10, and this will count down from 10 to 0, nothing particularly difficult here. Now consider the version below.

Recursive Count
The first thing to notice is that there is quite a lot less ‘code’ in this version. The second thing to notice is the last line in the IF block, our block is calling itself, and this is where the recursion happens. So, consider what happens when we first call the block, this is what happens:

1. We call the block with the parameter 10
2. The IF block checks if the value passed in is greater than 0. If it is, we display the value of n.
3. After displaying the value, we call the block again, using the value of the parameter that was originally passed in (10) minus 1. So the block is called with the value 9, and the whole process starts again.

This will continue to happen until the value passed in is 0 and then it will end.

Base Case

The IF block in our recursive block is very important. In programming terms, this is called the base case. The base case determines when the recursion will end. If there was no base case, the block would call itself forever, the same as an infinite loop (think forever block!).

Recursive Case

The opposite to the base case, is the recursive case. The recursive case is what happens when the base case has not been satisfied, in our example, its what happens within the IF block.

A More Realistic Example

Whilst the example above was perfectly valid to introduce the concept of recursion, you would usually use the loop construct to achieve the desired result. Lets look at something more valid. Very often, recursion examples use calculating the Fibonacci Sequence as an example. I’m not going to use this, I am going to use the calculating the factorial of a number, this lends itself well to explaining issues with recursion and also lends itself well to explaining an issue with recursion in Scratch 2.

Calculating the factorial of a number is easy, its the product of all the numbers (positive numbers) less than or equal to the number you provide. So the factorial of 5 is equal to 5x4x3x2x1, which equals 120, so the factorial of 5 is 120. So first how would we use a loop in Scratch to calculate the factorial of a number. Well we could do the following:

Factorial Loop
A fairly simple piece of code to calculate the factorial of a number. You will see if you call this block with the parameter 5, the variable result will contain 120, which is what we want. So, how do we do this recursively? Before I explain how to do this in Scratch 2, I want to move on to a little bit of programming theory, stick with me here, hopefully this won’t be too bad!!

If I was asked to implement a recursive factorial method in a programming language of my choice, I would use C# (designed and developed by Microsoft and part of the .NET Framework). I’ve used many languages over the years, but all my coding now (both home projects and work) is mainly in C#. This code will be easy to understand, so don’t worry. So, how could I implement this in C#, well I could do the following (for any programmers looking at the method below, you probably would replace the int with a long):

public int factorial(int n)
{

    if(n <= 1)
        return 1;
    else
        return n * factorial(n – 1);
}

We can then use the method as follows int result = factorial(5); and you should get the answer 120.

Right, so how does this work ‘under the covers’ when the method is called. Firstly, unlike Scratch, we can return values from our method, and this is a blessing, but in recursion, it can also be a nightmare. When a program is running and a method is called, it needs to know where to return back to when the method call has completed. An area of memory called the stack is used to hold this information. With recursion, each time the method calls itself, information is saved on the stack, so when the call finishes, it knows where to go back to. The stack is not infinite in size, so if you make a large amount of calls, the stack can become full up and you will get an error. When using C# (or any other language for the .NET Framework), you will receive StackOverflowException and you then have to go and fix this. I can remember having to fix someone elses recursive method where the stack was getting full up and it was a nightmare, the method was huge and there was no real documentation on what was going on. The eaasiest thing to do was to refactor the code to use a loop as there was no real need for it to be recursive. To show an example of what happens when the recursive method above runs, under the covers something like the following is happening when you calculate the factorial of 5:

1. factorial(5)
2. 5 * factorial(4)
3. 5 * 4 * factorial(3)
4. 5 * 4 * 3 * factorial(2)
5. 5 * 4 * 3 * 2 * factorial(1)
6.                                   returns 1
7                    returns 2 * 1 = 2
8.                 returns 3 * 2 = 6
9.             returns 4 * 6 = 24
10.    returns 5 * 24 = 120

As you can see, there is a lot going on here to calculate. The reason this is happening is because of this line of code in the method: return n * factorial(n – 1); The factorial method is being recursively called, but we also need to multiply the result of the recursive call by the previous value of n. You can see how this could cause stack problems should you wish to calculate the factorial of a large number. So, whats the recursive alternative? Well, there is an alternative, its called tail recursion and this is the type of recursion that you would ideally need to implement in Scratch 2 to recursively calculate the factorial of a number, as we have no version of the return statement in Scratch. Tail recursion may sound complicated, but to be honest, its an awful lot easier to understand whats going on than the example above.

Tail Recursion

A method is said to be tail recursive, when the last action performed by the method is to simply call itself. Looking at our example above, the recursive call is not the last action, the multiplication of the call to the method is the last action, so to be tail recursive, the last action needs to be factorial(n-1). How can we do this? Well, what we need to do is actually perform the multiplication in our method and return this back each time we call, so we need to keep a running total. Going back to my roots in assembly language programming, I’ll call this an accumulator. So we would have another parameter in our factorial method, so the declaration of the method would be int factorial(int n, int accumulator). Our method would then look like this:

int factorial(int n, int accumulator)
{
     if(n<2)
         return accumulator;
     else
         factorial(n-1, n*accumulator);
}

We would then call our method like this factorial(5, 1); You see that when we call the factorial method each time, we are sending over the result of the previous calculation. The benefit of this in languages such as C# is that the stack does not get filled up, essentally ‘under the covers’ the following happens:

1. factorial(5, 1)
2. factorial(4, 5)
3. factorial(3, 20)
4. factorial(2, 60)
5. factorial(1, 120);
6. returns 120

And because our base case says if the value of n passed in is less than 2, we return the value of accumulator, which will be 120, which is the factorial of 5. I’m sure you will agree, this looks an awful lot easier to understand than the first version!!

How do we do this in Scratch?

Some of you may be saying, well this still implements a return, how can we do this in Scratch. Well, with the single return, to do this in Scratch we would just assign the value of accumulator to a variable, as this is done in the base case, so there is nothing else needed to be done in the recursive method. So here goes, here is the recursive block that implements the tail recursive version of our method.

Recursive Factorial
As you can see, the only difference between our Scratch version and the C# version is that when the base case condition is met, we assign the result to a variable and not return it.

Conclusion

If you have got this far reading, thanks for sticking with it!! Recursion is interesting and one worth learning. As you can see, there are always ways of getting around issues with Scratch, and this also demonstrates the power of some of the new features of Scratch 2.

I have put together all the Scratch code used in the post into a Scratch solution that you can go and have a look at it. Should you find it useful feel free to use the code in your programs or teaching. The link to the solution is http://scratch.mit.edu/projects/11610916/

Windows Phone 8 Emulator running in VMWare Player

A couple of evenings ago, I decided to build a Windows 8 64 bit virtual machine with Visual Studio 2012 and the Windows Phone 8 SDK, so I could have a look at writing apps for Windows Phone. The main tool I use for creating and running VM’s is VMWare Player, its free and does exactly what I need. Now, its certainly possible to build a Windows 8 VM through VMWare Player as long as you have Version 5. I downloaded the latest version which is 5.0.2 build-1031769.

I went ahead and built my Windows 8 VM. Installed Visual Studio 2012 which went fine, and then installed Windows Phone 8 SDK. This is where I did have problems, the installation did fail with the usual unfriendly error message we have come to know with some software. After a search on Google, I did find some instructions on how to do this for VMWare Workstation, so thought I would give it a try for VMWare Player, and with a little modification, I’m pleased to say I now have a VM through Player working happily allowing me to test Windows Phone 8 apps through the emulator. I have detailed below exactly what I have done to build the virtual, so if you have had the same problem, following these instructions should sort out your problems.

Make Sure your Host system is good enough

The first thing you need to do is make sure your host system is up to the job. You need to have Hyper V running on your Windows 8 virtual, so your host systems needs to support SLAT – Intel Extended Page Tables. The easiest way to find this out is to use the Coreinfo.exe application. You can download this from the link http://technet.microsoft.com/en-gb/sysinternals/cc835722.aspx. Run a command prompt as administrator and from the directory that has Coreinfo.exe in it, run the command Coreinfo -v. If all goes well, you should see something like the following:

coreinfo

Looking at the last line, if this says Supports Intel extended page tables (SLAT) all is looking good. If SLAT is not supported, don’t continue with this, as I would say it isn’t going to work. Note: The asterisks are important here, if you see a hyphen instead of an asterisk it means the feature is not supported.

Creating the Virtual Machine

Ensure you have VMWare Player V5, as this will allow you to create Windows 8 VM’s. From within VMWare Player, select the option to create a new Virtual Machine, and make sure your dialog looks like the following:

Create VM

Click Next > and for the Guest Operating System select Windows 8 x64. Click Next > and give your VM a machine name. For the disk capacity, keep whats been offered (60 Gb for the disk size). Click Next > again and then click the Customize Hardware button.

For memory select, select 4 Gb if you can, this is what I have used and is what has been recommended in various posts I have read. Hopefully you will be able to allocate this much space. If you can’t select an amount that you can allow, but bare in mind that you should ideally have 4 Gb as a minimum. An example of this is shown below.

VM Memory

The from the list configuration options, select Processors. Select the number of cores you want to allocate, I have selected 2 and then ensure that the option Virtualize Intel VT-x/EPT or AMD-V.RVI is selected. The screen shot below shows my settings.

Processor SettingsClick Finish to save your new VM. Don’t start your VM yet!!!

Editing the VMX File

Before we carry on and start our VM, we need to edit the vmx file that has just been created. Navigate to where you have saved your VM files and look for the vmx file. Mine was called Windows8VM.vmx but yours will be called whatever you have called your VM. Open the vmx file in an editor like Notepad, and add the following line:

hypervisor.cpuid.v0 = “FALSE”

The v0 is a v and a zero.

My vmx file looks as follows. The added line is circled.

vmx file

Save your vmx file. We are now ready to start the VM.

Before you start the VM

Before you start the VM, select it in VMWare Player and edit the virtual machine settings. Select the CD/DVD (IDE) settings and for the Connection, either point this to the ISO for Windows 8 or a physical drive. As I’m installing from an ISO, my setting looked as follows:

CDDVD Settings

Click on OK and then start your VM. The Windows 8 installation should then start. When your Windows 8 installation has finished, you will need to install Visual Studio 2012 and then download and install the Windows Phone 8 SDK. You can get the SDK from this link http://dev.windowsphone.com/en-us/downloadsdk.

Conclusion

The above worked for me fine. It’s a mixture of installing for VMWare workstation and a bit of bespoking for VMWare Player through trial and error, hopefully this will work for you too and get you on the road to using the Windows Phone 8 emulator for testing your apps.