Saturday, October 24, 2009

Loading applications code sample

Code snippet for loading applications (This was adapted from a tutorial over at CodeProject so not even going to claim that I was the first to do this :) It's just a function I've used a few times at home). I'll be expanding it to take in a FileName as a string that will be the file I want to load, but I'm a keep it simple kind of girl. Do the basics first, get that bedded down, then add in more complexity. This is for windows apps/ console apps.

FileName is the location of the executable I want to load
i.e. FileName = @"C:\Program Files\Mobipocket.com\Mobipocket Reader\reader.exe";


 private void loadApplication(string FileName)
        {
            Process p = null;
            try
            {
                p = new Process();
                p.StartInfo.FileName = FileName;
                p.Start();
                p.WaitForExit();
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception Occurred :{0},{1}",
                         ex.Message, ex.StackTrace.ToString());
            }

Outputting XML to a file

A quick way to debug XML contents (I'm using this as  apart of a webservice call - where the response is long)

//Create an XML document
XmlDocument doc = new XmlDocument();

//Do stuff with it (add elements, properties, nodes, update text whatever)
....


// Save the document to file so you can look at it later
doc.Save("C:\\temp.xml"); 

// You could also do it as - particularly if you have a long path to where you are saving your files

doc.Save(@"C:\temp.xml");



 
//Otherwise, dump into the output window
doc.Save(Console.Out);

Sunday, October 4, 2009

String formatting in ASP.NET

I'm doing some pretty basic string formatting stuff while playing with an ASP.NET MVC application. The C# syntax for string formats in ASP.NET is pretty straight forward. At the moment I'm more interested in getting the controllers working - I can play with making the view pretty later :)

String formatting allows you to render common text formats such as:
  • Dates
  • Currency
  • alignment
  • numbers such as reals, decimals, scientific formats, floats, hex
Some basic examples that I've played with are:

 Purchase Price:  <%= Html.Encode(String.Format("{0:c}", Model.PurchasePrice))%>

would render to the HTML page as Purchase Price: $49.95


Release Date: <%= Html.Encode(String.Format("{0:d}", Model.ReleaseDate)) %>

would render to the HTML page as  Release Date: 7/11/1999

And because it's rendering in string format, if there is no string passed, then nothing is returned (it doesn't fall over). I haven't yet played with what happens if it can't format into a particular value as the data was validated before saving. But like a dutiful coder, that's what my unit tests will be for (when I wite them for this page now that the basic code is there.)


http://idunno.org/archive/2004/07/14/122.aspx has a fantastic overview of string formatting tags

Saturday, October 3, 2009

Various Links/ blog posts/ articles

Some interesting posts:

Programming for complete beginners
Talks about Microsoft's site for beginning programming

Cross Browser & Multibrowser testing using Microsoft Expression Web Support
A way of testing multiple browsers on the same OS

Performing common tasks with Linq in ASP.NET
- finding enabled text boxes
- finding long items in a drop down box


MVC & Entity Framework
Considering they're just about to release version 2 of the ASP.NET MVC framework, I'm wondering how much will change ...

Making it easy to refactor
- Providing good test names
- well named variables
- appropriate test coverage
- avoiding fragile tests
- avoiding [Setup] and [tear down]