Coding for Kids #2. Code!

In my last post, I introduced the problem we’re trying to solve via an introductory computer program:  finding 100 point words where each letter of the alphabet corresponds to its place in the alphabet (A=1, B=2 … Z=26).  At this point, we’ve built the basic template for the app, but haven’t yet written any code.   It’s time to write some basic functions (or methods) that help solve this.  For coding veterans, remember – this initially is about solving the problem as a beginner and optimizing later.   When programming, it’s often easiest to work backwards – that is, the program flow is getting the user input, parsing it, then determining the value.   To write code to do this, it’s easiest to determine the value of a character first, working backwards.   So, let’s write a simple method that does this.   We’ll want a method that accepts a character, and returns an integer.  Within the code template established in the first post, add a method like this under the button click event handler: 1: private void btnCalculate_Click(object sender, EventArgs e) 2: { 3: 4: } 5:   6: /// <summary> 7: /// Returns the value of a given character, where the 8: /// value is determined by its location in the alphabet. 9: /// </summary> 10: /// <param name="c">The character to be evaluated.</param> 11: /// <returns>The numerical value of the character.</returns> 12: private int getCharacterValue(char c) 13: { 14: if (c == 'a') return 1; 15: if (c == 'b') return 2; 16: if (c == 'c') return 3; 17: // ... 18: if (c == 'z') return 26; 19:   20: return 0; 21: } .csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, "Courier New", courier, monospace; background-color: #ffffff; /*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em; } .csharpcode .lnum { color: #606060; } The green text are comments.  Like the button method above it (that doesn’t do anything), our method is private (means it can only be used in this class), returns an integer (int), is called getCharacterValue, and accepts a character (char) we’ll just call c.  Normally, you’d want your variables, like c, to be clear names … such as firstName or dateOfBirth.  But for a simple character, it’s common to just use a single letter like this.  Remember, c is not “C” as in the letter of the alphabet – c is a variable that represents any possibly character.   All the code within the method must be encapsulated with curly braces { }.   Notice that we have a bunch of if statements.  An if statement evaluates a condition in parentheses.  If the condition is true, it executes the code that follows.    We could optionally execute multiple lines of code by putting it in curly braces, or include an else section.   The return command stops executing code, and returns the value if the function is true.  So, suppose that we do something like: int charValue = getCharacterValue('b'); .csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, "Courier New", courier, monospace; background-color: #ffffff; /*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em; } .csharpcode .lnum { color: #606060; } The runtime (that is, what is executing our code) calls our getCharacterValue function passing in ‘b’.  We’re assigning the result of that call to an integer called charValue.   Logically, you know the result should be 2, and therefore, charValue should be 2.  If you were to follow the function as it executes, it would hit line 14 and see if the character is an ‘a’ … it’s not, so it keeps going.  It is a ‘b’, so it immediately stops executing and returns 2.   If the character passed in is ‘z’, you can see that it’s relatively inefficient.  We’re evaluating 26 if statements.   We’ll address that another time, but for now, it’ll do.   If it gets all the way to the end, we’ll return 0. My daughter asked me why we use 2 equal signs when comparing … such as c == ‘a’ … the short version is that one equal sign is assignment, whereas two is an evaluation.   Notice that in my one line sample code, we’re assigning charValue with a single equal sign.  That means we’re assigning the value of that function to our variable charValue.   Remember that computers need precise instructions, and so, in the if statements, we’re asking the runtime if these two things are the same – we’re asking it to evaluate.  We now have a method that determines the value of a single character … but what about an entire word?  Multiple characters in a computer program are called a string.   What we need to do is write another function that accepts a string, parses it for each character in the string, adding up the value of each character along the way.   To solve this, we need a variable to keep track of each character’s value.   We also need a way to look at each character in a string.  Fortunately, there’s an easy way to do that. 1: private int getWordValue(string theWord) 2: { 3: int wordValue = 0; 4:   5: foreach (char c in theWord) 6: { 7: wordValue += getCharacterValue(c); 8: } 9:   10: return wordValue; 11: } .csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, "Courier New", courier, monospace; background-color: #ffffff; /*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em; } .csharpcode .lnum { color: #606060; } When our function is called, we’ll use a temporary variable called wordValue that we initially assign to zero.  Next we have a foreach statement.  A foreach is a fancy kind of loop.   There are different kinds of loops, with subtle advantages and disadvantages.  A foreach makes it easy to iterate over objects in a collection – in this case, characters in a string. What the foreach is saying is that for every character (called c) in theWord, execute the code in curly braces.  In that code block, we assign wordValue to itself plus the value of getCharacterValue of the current character in theWord.  (The += means ‘itself plus…’.)   If the word is “test”, the foreach code block is executed 4 times, the first time c is ‘t’, then ‘e’, then ‘s’, then ‘t’.  When that is done, wordValue should be the sum of each character’s value, so we’ll just return that. Now all we need to do is make it so when the button is clicked, we grab the value of the YourWord textbox, and display it in the WordValue textbox.  private void btnCalculate_Click(object sender, EventArgs e) { txtWordValue.Text = getWordValue(txtYourWord.Text).ToString(); } .csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, "Courier New", courier, monospace; background-color: #ffffff; /*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em; } .csharpcode .lnum { color: #606060; } .csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, "Courier New", courier, monospace; background-color: #ffffff; /*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em; } .csharpcode .lnum { color: #606060; } We can do this in one line of code in our button event handler.  First, one thing to know is that classes encapsulate functionality.  A TextBox is a class, as is our button.  We don’t have to worry about the inner workings of the textbox – such as how to draw the box, handle user input, etc.   The textbox contains a number of properties that can be accessed that make it easy for us to get or set the value of something within the class.  In the case of the textbox, it has a Text property that lets us get or set the value of the text in the textbox.  Properties are accessed by using a “.” – so, in the line above, we’re assigning the value of the Text property in the txtWordValue, and also reading the value of the Text property in the txtYourWord textbox. We call getWordValue, passing in the txtYourWord.Text value, which would be whatever the user types in the box.  Now, what’s that ToString()?   You’ll see ToString() a lot in C#.  Remember, with C# and many languages, you need to be very precise.  When getWordValue returns, what is it?  It’s an integer.  A whole number.  Computers deal with numbers differently and more simply than they do strings.   The Text property of the textbox gives you a string, and also expects a string if being assigned to.  We have a number.  So, in essence, calling ToString() turns, for example, the number 42 into the string “42”.  Let’s hit F5 and run the program.  Using the word “test”, we’ll see the following.  We’ll also try “automated” – an apropos 100 point word! Let’s not get too excited.  Let’s try it again, this time using the word “Automated” (notice the capital “A”): Our first bug.  We’ve barely written code and it’s already inefficient, buggy, etc.  The problem is that “A” is not the same as “a” … there are a number of ways to solve this, but we’ll look at those in part 3.    In the meantime, here’s the code for this version: Download VS12 Project

Windows 8 DevCamp: Raleigh

Microsoft RD Jim Duffy and I are hosting a series of events, kicking off with a Windows 8 DevCamp in Raleigh on Sept 25th!  I’m the brains, Jim is the talent, so don’t miss it.  Sign up here!  (Just kidding about Jim being the talent ) http://takenotewin8.eventbrite.com/ The DevCamp is designed to not only introduce you to developing on the platform, but also take active steps through the design, development, and submission phases to get your app into the marketplace.  Here’s the rundown: October 26th is right around the corner! Time is ticking away and the time to act is now! Microsoft Developer Evangelist Brian Hitney and Jim Duffy want to help you get your app in the store in time for the October 26 Windows 8 launch. Come join us on Tuesday, September 25, at 9:00 AM in the Microsoft RTP offices to learn how simple it can be to construct a world class Windows 8 application. Don't think you can be ready to join the Windows 8 launch? Come anyway. You might be surprised. Don't have any idea what kind of app to build? Come anyway. They're are plenty of places to look for inspiration. Either way you can learn what it takes to create or tune an app for Windows 8 and publish it in the Windows App Store. (Registration opens at 8:30 AM.) Windows 8 Overview and the Windows Store Haven't seen Windows 8 or know what it takes to get an app in the Windows Store? We'll cover that here. Drink your coffee because this first session will dive quickly into Windows 8, the platform, the changes, and the Windows Store, allowing you to monetize applications in a number of ways from in app purchases to subscriptions to trials. Cookbook I: Design Templates and Style In this session, we'll talk about the design principles for Windows Store applications – controls, color, typography, and general guidelines to follow to deliver the best user experiences. We'll also dive into the development choices and tooling support available. We'll begin with the built in templates and show how we can quickly scaffold a data-driven application called the Contoso Cookbook. Cookbook II: Data, Contracts, and Settings You've seen the Cookbook. But, how do we leverage data? How do we expose our data to allow users to search and share from the app? We'll explore these options in this session, from storing data, retrieving data via a web service using an in app purchase, to implementing search and sharing contracts, we'll look at the code that makes this possible. We'll also show how to store data locally, as well as roam preferences that can follow a user automatically as they log in to different devices. Cookbook III: Application Bar, Tiles and Notifications We've got the Cookbook well under way; now it's time to add some polish. We'll look at using the app bar for common tasks, and spend time talking about leveraging "live tiles" to create an up-to-date, engaging tile for your application. We'll also look at using Notifications, and how applications can run either background agents for various tasks, or be notified from a remote service using the Windows Notification Service. Hands On: [Your App Here] Now it's your time. Fire up your laptop with Windows 8 Release Preview (or RTM) and VS Express 2012 loaded on it and get ready to code!

Coding for Kids #1. The Beginning.

My daughter, a 5th grader, has shown an occasional interest in programming but having so many frameworks and platforms available today is both a blessing and a curse.  I began to learn programming by hacking Maxit, Hunt the Wumpus, Adventure, and others.   There were no graphics, no internet, no multiplayer. I saw her doing working on something when it occurred to me this would be a fun afternoon project to solve via code.  She was looking for “dollar words” – words that added up to 100 points (or cents, I suppose) where each letter is worth their index in the alphabet (A=1, B=2, etc.). I really liked this problem because it’s something that can be solved iteratively, and bits can be optimized later for further instruction.  If you’re like me, you immediately start trying to figure out the most efficient way to map a given letter to a point value … simple if/then?  Regex?  Character code map?   Lots of options!  Immediately, in this type of routine, you can see the tradeoffs between simplicity, reuse, and speed. I decided to write this series to be approachable by non-programmers and newcomers, because it can be a lot of fun to solve with your kids!   If you’re already a programmer, some of this may seem rather basic, but then, so is the problem(s) we are solving and it’s a great way to get started. Step 1.  Assuming you have never coded before, you need to download a code editor.   These are typically called IDEs for Integrated Development Environment because it can do much more than just be a text editor.   While we could technically write code in notepad, an IDE is often preferred because it can help you be more productive by compiling code, allowing you to debug, catching errors, and can autocomplete items.  Visual Studio is a fantastic IDE, and there are a number of free versions for problems just like this called Visual Studio Express.    If you don’t have it, download it now.  For this project, we’ll create a simple desktop application, so you’ll need the VS2012 Express Desktop version. Step 2.  Before we even start coding, it’s helpful to be clear on the problem.   We want a program that gives us 100 point words based on the scoring method mentioned above.  Ideally, it would be great if it could give us ALL 100 point words in the English language.  With a computer, we need to be explicit, so we’d want them sorted alphabetically.   While we’re at it, wouldn’t it be nice to let the user tell the program to find words of a certain value?  Why not 200 point words? These are great ideas for future versions, but let’s start simple.  The user types in a word, and the program displays the value of that word. Step 3. Launch Visual Studio.  If it’s the first time you’re launch it, you’ll need to select a profile, such as “C# Development Settings.”  We’ll use C# to solve this problem.  C# is a programming language.   A fun exercise in the future would be to convert this to another language – it’s a good way to learn a new programming language. Once loaded, select File > New Project.  On the New Project window, you’ll see templates which act as starting points for many different kind of applications.  Your screen may look different from mine, but essentially, you’ll want to select, under the Visual C# tree, the Windows > Windows Forms Application template.   Give it any name you’d like – in this case, I chose 100PointWords (though, you’ll see later why this name is limiting): Let’s familiarize ourselves with the environment.  Click the picture below for a larger version: This is the IDE.  On the left side of the screen is the Toolbox.  If you don’t see the Toolbox, you can click the View menu and select Toolbox from the drop down.    The Form1 we see is the design surface.   The design surface allows us to drag and drop, resize, and otherwise visually manipulate our application.   On the right side of the screen is the Solution Explorer window.  Our solution can contain any number of individual projects – in this case, we have only one, our 100PointWords application.   Form1 is currently loaded. If you double click on the Form1 design surface, you’ll go from design view into code view.   This will open the Form1.cs file and display the code associated with our application: 1: namespace _100PointWords 2: { 3: public partial class Form1 : Form 4: { 5: public Form1() 6: { 7: InitializeComponent(); 8: } 9:   10: private void Form1_Load(object sender, EventArgs e) 11: { 12:   13: } 14: } 15: } .csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, "Courier New", courier, monospace; background-color: #ffffff; /*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em; } .csharpcode .lnum { color: #606060; } Not a whole lot of code yet.  A namespace is a container that encapsulates any number of classes typically at the project level.   A class is used as a container for a given object, like a form (in this case).   This is partial class.  Without getting into too many details, what this means is that the designer file and the code file will get combined at compile time into a single class.  This makes it easier for us to work on code here, and keep the design separate where we can drag and drop items visually. Step 4.  Now, let’s start dragging some controls from the toolbox onto our form.  Switch back to the design surface tab.   Take a look at the screenshot below: Now just drag and drop some labels, textboxes, and a button from the toolbox onto the design surface (just drag and drop!).   They can be moved with the mouse, or fine tuned with the arrow keys.  A label is just that – some text.  We’ll have one label say “Your Word:” and the other label say, “Word Value:”.   We’ll have two textboxes – the top one is where the user enters a word, and the bottom is where we’ll display the word value. On the lower right side of the screen, you’ll notice we have the properties window for the current textbox, called “txtYourWord” … if you don’t see the properties window, right click the top textbox and select Properties, as shown in the screenshot above. By default, the textbox will have a name like “textbox1” and the button will be “button1” … it’s helpful to change these to reflect their purpose, so in the properties window, change the top textbox to “txtYourWord” and the bottom to “txtWordValue”, and the button to “btnCalculate”.  For the button, you’ll change two values in the properties window:  the text, which should say, “Calculate!”, and the name, which is “btnCalculate.”  There are a LOT of properties in that properties window – don’t let it confuse you.  We can ignore everything except the name and text values.  While you’re there, update the labels to something like the screenshot above (“Your Word” and “Your Value”). For controls like textboxes, buttons, etc., it’s common to use an identifier like “txt” or “btn” in the name so that, in code, it’s easier to recognize we’re dealing with a textbox and a button.  (This is known as Hungarian notation in computer programming speak.) With the mouse over the button, double click the calculate button: And you should see, in the code file: What we’ve just done is wire up an event handler.    This is a fancy way of saying, “When the user clicks the Technorati Tags: beginner,code,c#,development button, run this piece of code.”  Which, as of right now, does nothing.   An event handler simply responds to some type of event – like a button click.  Events are happening all the time – when a key is pressed, the mouse is moved, etc.    Notice that this function, btnCalculate_Click, has a few declarations.  The first is that it’s private.  That means this function (or method) can only be called from within this class.  Void is the return value – in this case, we’re not returning anything, so the return type is void.   The items in parentheses are items passed into the method.  For our purposes, we can ignore these for now. Now, if you’re really curious, you can open up the designer code file (Form1.Designer.cs) and see that all double clicking the button did was add this line of code for you: this.btnCalculate.Click += new System.EventHandler(this.btnCalculate_Click); We won’t go any further into event handling here, but event handling is key to modern app development. Congratulations!  We have the program laid out and ready for code.   We’ve leveraged the design surface to build the look of our application, and now what we need to do is dive into some code to make it all happen.   Stay tuned for part 2!

Charlotte Windows 8 DevCamp 9/8/12

This coming Saturday, 9/8/12, we’re putting together a 1 day Windows 8 DevCamp!  Check out the details here:  http://developersguild.org What we decided to do is put the best content from ou Technorati Tags: windows 8,development,charlotte,microsoft r other Win8 DevCamps (typically 2 day), and create an essential, what-you-need-to-know day of content to help you get an app up and running quickly.  In addition, we decided to build the entire content around the Contoso Cookbook sample app, allowing us to focus on a single solution implementing the best practices. Here’s an overview of the event: We want your app in the store in time for the October 26 Windows 8 launch. Join us Saturday, September 8, at 9:00 AM in the Mt. Kilimanjaro/Mt. Everest rooms of the Charlotte Microsoft Campus to understand how simple it can be to construct a world class Windows 8 application. Don't think you can be ready to join the Windows 8 launch? Come anyway. You might be surprised. Either way you can learn what it takes to create or tune an app for Windows 8 and publish it in the Windows App Store. (Registration opens at 8:30 AM.) Windows 8 Overview and the Windows Store Haven't seen Windows 8 or know what it takes to get an app in the Windows Store? We'll cover that here. Drink your coffee because this first session will dive quickly into Windows 8, the platform, the changes, and the Windows Store, allowing you to monetize applications in a number of ways from in app purchases to subscriptions to trials. Cookbook I: Design Templates and Style In this session, we'll talk about the design principles for Windows Store applications – controls, color, typography, and general guidelines to follow to deliver the best user experiences. We'll also dive into the development choices and tooling support available. We'll begin with the built in templates and show how we can quickly scaffold a data-driven application called the Contoso Cookbook. Cookbook II: Data, Contacts, and Settings You've seen the Cookbook. But, how do we leverage data? How do we expose our data to allow users to search and share from the app? We'll explore these options in this session, from storing data, retrieving data via a web service using an in app purchase, to implementing search and sharing contracts, we'll look at the code that makes this possible. We'll also show how to store data locally, as well as roam preferences that can follow a user automatically as they log in to different devices. Cookbook III: Application Bar, Tiles and Notifications We've got the Cookbook well under way; now it's time to add some polish. We'll look at using the app bar for common tasks, and spend time talking about leveraging "live tiles" to create an up-to-date, engaging tile for your application. We'll also look at using Notifications, and how applications can run either background agents for various tasks, or be notified from a remote service using the Windows Notification Service. Hands On: [Your App Here] Now it's your time. We'll work in breakouts and 1:1 as necessary to get the tools and environment set up and provide guidance for building out your app. Have an app already underway? We can test, review and provide feedback against store certification requirements. As time permits, we'll cover additional features, and talk further about the certification process. And if your app isn't quite ready yet, that's fine! We're here to give you the kickstart to building your app, and we'll be here to make sure it's done by October 26th

Music Library Synchronization, Sonos Tips

I love Windows Home Server.   I’ve been using Windows Home Server for years, and just purchased a Windows Home Server 2011(WHS) box from Newegg (great deal on a HP Proliant micro server).  Many have asked me why I like WHS so much – it’s NAS, it’s a media server, it’s backup.   It’s a step up from a simple NAS device (although, admittedly, not as plug and play), offers more flexibility and is more cost effective than a Drobo.  A small backup agent can take snapshots of your PC, typically on a daily basis, so they can be restored to a given point in time.  I keep snapshots of my initial installation, for example.  Restoring to those backups is a simple process.  I’m also a big fan of Sonos, a whole-home music solution that works amazingly well.  What Sonos has done exceedingly well is blend quality hardware, quality software, and reasonable (but not cheap) price points.  I have an extensive music collection, and I point Sonos to a share on my WHS box to index and stream music.  However, I consider my laptop my “database of record” for my music.  It’s where I download stuff, and I take it with me because I’m often on the road.  The problem I run into is keeping my WHS library in sync with my laptop.   In my case, I want to mirror my library on the WHS exactly as it is on my local collection – and because I’m often reorganizing my collection, adding tags, etc., I need a simple way to do this.  Enter Robocopy.   Robocopy (Robust file copy) is now built into Windows, and it’s a simple command line tool with a number of options to make this a snap.   For example, if I want to mirror a folder on my laptop with my WHS, this command will do it: c:\>robocopy "D:\Music" "\\BEAST\Sonos\music" /mir /r:10 /MT:8 D:\Music is my local folder, my server is \\Beast.  The /mir command is for mirror – it’s the same as using /purge and /e:  /purge is to delete files at the target folder that no longer exist in the source, and /e is to copy all subdirectories, including empty ones.   The /r:10 will tell it to retry up to 10 times, in case of some network glitch, and the /MT:8 will have Robocopy use 8 threads to speed things along.   (If you’re familiar with Robocopy, I don’t recommend using /z (restartable) mode as it adds overhead, not needed given the size of files we’re dealing with.) Now, what if you don’t keep all your music local, and just want to copy it over?   You don’t want to use /mir since it will remove files you otherwise want to keep!   The rest of the command will work fine, but if you move/rename files locally that were previously copied, you’ll have to remember to do that manually on the server.  Once Robocopy does its thing, you’ll get a nice summary:             Total    Copied   Skipped  Mismatch    FAILED    Extras Dirs :      1384        29      1355          0         0         0 Files :     15078       381     14697         0         0         0 Bytes : 117.188 g   3.212 g 113.975 g         0         0         0 Times :   0:16:40   0:02:52                       0:00:00   0:00:48 Here, it copied about 30 new folders.  It took about 16 minutes to run, but that’s largely due to new content, having copied some 3.2gb of new files.   Assuming minor changes only, the process typically runs in about 30 seconds. If you want to get fancy, you could even have Robocopy monitor your folders for changes.  The next challenge is to have Sonos update its music index once new files are copied over.   Sonos can update its index on a daily basis (or manually via the control software), but I want it done automatically after new files are copied over.  This one is a bit trickier, but thanks to some gurus in the Sonos forums, it’s not impossible.   I’m including the .exe file here for you to use.  Obviously, trusting an exe from someone on the web is not something I’d do, but it’s a .NET assembly which means you can use a tool like JustDecompile to crack it open and look at the source yourself.   Having said that, I’m not responsible if this code causes your computer to blow up, your music collection to vanish, or kills any puppies. The source code looks like so, and it sends an SOAP packet to a specified Sonos unit to trigger an index rebuild: using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.IO;using System.Net;namespace SonosIndexUpdater{ class Program { static void Main(string[] args) { string ip; if (args != null && args.Length > 0) { ip = args[0].Trim(); } else { Console.WriteLine("Missing IP Address. Please add IP address for any Sonos unit."); return; } string header1 = @"SOAPACTION: ""urn:schemas-upnp-org:service :ContentDirectory:1#RefreshShareIndex"""; string postData = @"<s:Envelope xmlns:s=""http://schemas.xmlsoap.org/soap/envelope/"" s:encodingStyle=""http://schemas.xmlsoap.org/soap/encoding/""> <s:Body> <u:RefreshShareIndex xmlns:u=""urn:schemas-upnp-org:service:ContentDirectory:1""> <AlbumArtistDisplayOption></AlbumArtistDisplayOption></u:RefreshShareIndex> </s:Body> </s:Envelope>"; string url = string.Format("http://{0}:1400/MediaServer/ContentDirectory/Control", ip); byte[] byteArray = Encoding.UTF8.GetBytes(postData); try { System.Net.WebRequest req = System.Net.WebRequest.Create(url); req.Headers.Add(header1); req.ContentType = "text/xml"; req.Method = "POST"; req.ContentType = "application/x-www-form-urlencoded"; req.ContentLength = byteArray.Length; req.Timeout = 5000; Stream dataStream = req.GetRequestStream(); dataStream.Write(byteArray, 0, byteArray.Length); dataStream.Close(); using (WebResponse response = req.GetResponse()) { Console.WriteLine("Response from Sonos: {0}", ((HttpWebResponse)response).StatusDescription); using (dataStream = response.GetResponseStream()) { using (StreamReader reader = new StreamReader(dataStream)) { string responseFromServer = reader.ReadToEnd(); Console.WriteLine("Data: {0}", responseFromServer); } } } } catch (Exception ex) { Console.WriteLine("Exception occured: {0}", ex.Message); } } }}   To use it, you’d just pass in the IP address of any Sonos unit:   c:\>SonosIndexUpdater 192.168.1.100 If you stumbled on this and aren’t a developer but want to try it out, you can build this for free using Visual Studio Express.    Here are some files:   EXE file only: DownloadVS2010 Solution: Download

Azure Camps Coming Soon!

Jim, Peter, and I are gearing up for another road trip to spread the goodness that is Windows Azure! The Windows Azure DevCamp series launched recently with a two-day event in Silicon Valley, and we’re jumping on the bandwagon for the East Region. We have five stops planned in December, and we’re doing things a bit differently this go-round. Most of the events will begin at 2 p.m. and end at 9 p.m. – with dinner in between of course. The first part will be a traditional presentation format and then we’re bringing back RockPaperAzure for some “hands-on” time during the second half of the event. We’re hoping you can join us the whole time, but if classes or your work schedule get in the way, definitely stop by for the evening hackathon (or vice versa). By the way it wouldn’t be RockPaperAzure without some loot to give away, so stay “Kinected” to our blogs for further details on what’s at stake! Here’s the event schedule, be sure to register quickly as some venues are very constrained on space. You’ll want to have your very own account to participate, so no time like the present to sign up for the Trial Offer, which will give you plenty of FREE usage of Windows Azure services for the event as well as beyond.   Registration Link Date Time NCSU, Raleigh NC Mon, Dec. 5th, 2011 2 – 9 p.m. Microsoft, Farmington CT Wed., Dec. 7th, 2011 2 – 9 p.m. Microsoft, New York City Thur., Dec. 8th, 2011 9 a.m. – 5 p.m. Microsoft, Malvern PA Mon., Dec. 12th, 2011 2 – 9 p.m. Microsoft, Chevy Chase MD Wed., Dec. 14th, 2011 2 – 9 p.m.

Rock, Paper, Azure is back…

Rock, Paper, Azure (RPA) is back!   For those of you who have played before, be sure to get back in the game!  If you haven’t heard of RPA, check out my past posts on the subject.   In short, RPA is a game that we built in Windows Azure.  You build a bot that plays a modified version of rock, paper, scissors on your behalf, and you try to outsmart the competition.  Over the summer, we ran a Grand Tournament where the first place prize was $5,000!   This time, we’ve decided to change things a bit and do both a competition and a sweepstakes.   The game, of course, is a competition because you’re trying to win.  But we heard from many who didn’t want to get in the game because the competition was a bit fierce.   Competition:  from Nov 25 through Dec 16, each Friday, we’ll give the top 5 bots a $50 Best Buy gift card.  If you’re the top bot each Friday, you’ll get a $50 gift card each Friday.  Sweepstakes:  for all bots in the game on Dec 16th, we’ll run the final round and then select a winner at random to win a trip to Cancun.   We’re also giving away an Acer Aspire S3 laptop, a Windows Phone, and an Xbox Kinect bundle.  Perfect timing for the holidays! Check it out at http://www.rockpaperazure.com!

New Worldmaps Reporting Site

Thanks to the folks at Infragistics, there’s a new reporting site for Worldmaps!   This Silverlight application not only looks great, but provides many new ways to drill down into the data: One of the cool things you can do is easily compare to sites – the app has tabs that allows you to add multiple sites (unlike the still-existing old dashboard which showed only your neighbors in the leaderboard): What’s even cooler is playing around with the Gapminder stats: A Gapminder chart is ideal for animating data over time in a way that a simple 2-D chart doesn’t.   The bottom line is: this app is so much fun just to play around with.   Many thanks to Jason Beres, Mihail Mateev, Riddhima Shelat, and others (I’m sure I’m leaving people out!) for making this possible!   Check out Mihail’s post here on the project!

Windows Phone 7 App Sales

I wanted to make my app experience as open as possible, so now that reporting for Windows Phone 7 app sales is out, I’m going to share my details with the world for those interested in developing for Windows Phone 7.  First: device sales, as of this writing, have not been released by Microsoft.  I don’t know them, and won’t speculate on what that means.   As Joe B pointed out in his interview with Walt recently, it will be a process over time.    But, now that reporting for trials/purchases has been implemented in the developer dashboard, let’s take a look at how SqueakBox is doing.  SqueakBox is generally in the upper third of the entertainment section, getting good feedback, and providing a trial.  Providing a trial is a key element that developers really should take advantage of.   So, let’s log in at take a look-see: First up, when you log in to the reports you’ll see the following daily download stats: Overall I’m happy with the trend.   We can also look at cumulative downloads, which is helpful to understand the trend which shows a fairly linear distribution: Next is purchase/trial information.  Obviously this is important for conversion rates and, naturally, how much money I’m going to get. This is the second page of detailed sales data – the first page has more international usage, but decided to show only 1 screen above because it shows consumption in the US.  As you can see, I’m doing best in my home turf and that’s not surprising.  It’s an indie app, not internationalized, and while there are some sales abroad, it’s a small percentage. Now – what is the conversion?   Here, we need to make an assumption the user downloaded the trial first.  This may or may not be the case.  A user could just click purchase (hey, it’s only 99 cents), particularly if they used the app on another phone.  In that case, it would show up as a purchase – not a trial.  Assuming folks downloaded a trial first, based on this page alone, we had 289 trial downloads, and 115 sales.  That is a very good conversion rate if you ask me, since I download TONS of stuff with trials and many I haven’t even tried yet.  I have to assume many users are doing the same.  The big question I’m pondering is this: do I implement an ad in the trial mode?  Currently, the app limits features/sounds hoping users will unlock them by purchasing.  Optionally, I can unlock some of this, and implement the ads to bring in perpetual income, albeit much smaller.   I’ve been cautious of the ad approach for the reasons I outlined in this blog post.

Distributed Cache in Azure: Part III

It has been awhile since my last post on this … but here is the completed project – just in time for distributed cache in the Azure AppFabric!  : )  In all seriousness, even with the AppFabric Cache, this is still a nice viable solution for smaller scale applications. To recap, the premise here is that we have multiple website instances all running independently, but we want to be able to sync cache between them.   Each instance will maintain its own copy of an object in the cache, but in the event that one instance sees a reason to update the cache (for example, a user modifies their account or some other non-predictable action occurs), the other instances can pick up on this change.  It’s possible to pass objects across the WCF service however because each instance knows how to get the objects, it’s a bit cleaner to broadcast ‘flush’ commands.  So that’s what we’ll do.   While it’s completely possible to run this outside of Azure, one of the nice benefits is that the Azure fabric controller maintains the RoleEnvironment class so all of your instances can (if Internal Endpoints are enabled) be aware of each other, even if you spin up new instances or reduce instance counts. You can download this sample project here:  DistributedCache.zip     When you run the project, you’ll see a simple screen like so: Specifically, pay attention to the Date column.  Here we have 3 webrole instances running.  Notice that the Date matches – this is the last updated date/time of some fictitious settings we’re storing in Azure Storage.    If we add a new setting or just click Save New Settings, the webrole updates the settings in Storage, and then broadcasts a ‘flush’ command to the other instances.  After clicking, notice the date of all three changes to reflect the update: In a typical cache situation, you’d have no way to easily update the other instances to handle ad-hoc updates.  Even if you don’t need a distributed cache solution, the code base here may be helpful for getting started with WCF services for inter-role communication.  Enjoy!

My Apps

Dark Skies Astrophotography Journal Vol 1 Explore The Moon
Mars Explorer Moons of Jupiter Messier Object Explorer
Brew Finder Earthquake Explorer Venus Explorer  

My Worldmap

Month List