Archive for July 2011

How to get a job in software development in as little as 6 months

Hello. I keep hearing about the number of unemployed.  Many have been unemployed for more than 6 months and are likely to be unemployed for another 6 months.

Well, technology is not going away so here is a solution for some of you.

Many of you think that you need a degree in Computer Science to become a Software Developer.  This is just not true.  Many of the Software Developers that I know do not have a degree in Computer Science.  However, most love technology and so they learn it on their own.

If you love technology and you can learn on your own, you could become a Software Developer in 6 months.

While this article is for Software Development, the pattern I describe here work with IT as well, and many other careers in many fields.

Disclaimer: I can’t promise that everyone who does this method will succeed in 6 months, but at least it is a pattern that you can try. And if you end up getting a job outside of technology the skills you learned here will still be beneficial for the rest of your life as computers are likely to still be here well past your retirement.

Month 1

Step 1 – Get access to a computer and the internet

Yes you need a computer to become a software developer.  You even need one to study. You need the internet to have access to the nearly unlimited amount of free training material about development.

Step 2 – Choose one of the top programming languages

The top languages for writing code today and probably will continue to increase in popularity in the future are these:

  • Java
  • PHP
  • C# or ASP.NET

Other languages that are popular are C++, Objective C, Perl, Python, etc…

Web development is fun too. HTML, CSS, JavaScript, etc. One language that is likely to become more popular now and in the near future are HTML5. You have probably heard buzz about it.

However, these are no the only languages you could learn.

Maybe you would like to work with databases. You could learn to about SQL.  There are many databases to choose from, the top being these:

  • MySQL
  • MS SQL
  • Oracle
  • PostgreSQL

Maybe you are into business technology. You could become an expert at a specific piece of software, such as how to create business intelligence reports with QlikView. There are hundred of products that require technology experts to use them and specializing in any one of them could lead to a stable career.

Step 3 – Find the Certification Preparation material

Here are some of the Certifications for the common languages.

Here is a list of certification sites for database software.

Many software applications have certification as well. You can find these certifications with a simple search on the internet.

Once you have the certification material, there should be some information on what to study and what to learn to complete the certification. It is your goal to pass a certification in 6 months.

Step 4 – Download any free or trial software

Almost all software has either open source, or a shareware, express, or trial versions of their software that you can use for free.  Sometimes, like in the case of trial software, there is a time limit.  But that doesn’t prevent you from learning as much as you can before the trial expires.

To become a developer, one of the most basic tools is an Integrated Development Environment (IDE).  Here is a list of the free or open source IDEs you can get.

You can find free or open source version of other software for other languages with a simple search on the internet.

With this free software you are set free to practice as you learn.

Step 5 – Access the free documentation and training on the internet

There are a lot of free tutorials, videos, blog posts and more on the internet.  You can become an expert in something without ever buying a book or paying from a training class.

The following site has a quick tutorial on almost all programming languages: http://www.w3schools.com/

You can use this information to study and prepare for your certification.

Make a list of all the tutorials you have found and that you plan to study. Put this in a spreadsheet in a nice way, maybe a separate line for each chapter or section, so you can see your progress.  If you want, email the spread sheet and I will post it for the next person.

Month 2 – 3

Study and practice. You now ave all the software you need and all the training material you need.  Go through it.  This takes time.

Do the sample development projects, don’t just look at them, code them up, compile them, and run them.

Month 4

Step 1 – Pick a development project and work on it.

There are so many open source projects in the world and you can pick any of them. Or you can start your own. Or maybe you have an idea for some software that you can sell and you can start your own business. Whatever, just pick something and start working on it.

Here is a good way to figure out what project to choose.

  1. Ask yourself, what interests me outside of development? (Maybe you are a mechanic and you tinker with cars, or maybe you like to collect stamps, or maybe you like horses)
  2. Is there an open source software project in this area? If not, is there an opportunity to create one? Or better yet, is there a software opportunity that could inspire a small business in this area.

Another option, if you have any connections with any company at all, offer to do something to help their company for free.  Maybe your hair stylist needs you to develop something to help her with her clients.  Even though you are doing it for free, you are writing code that will go into production in a business environment.  This experience is invaluable.

Step 2 – Keep learning

As you start working on this project, you will start using what you have already learned. However, you aren’t done learning. And you will have to solve some tough problems. Keep learning.

Month 5

Step 1 – Schedule the certification exam you have chosen for the end of the month

Keep learning. Keep working on your development project. However, take some time to focus on the certification exam. Find practice tests, sample question.

Most people fail the exam on the first try.  This is why you are taking it once in Month 5.

Step 2 – Start the job hunt

Start looking for companies that are hiring Jr. Developers or entry-level developers in your position.

Add to your resume all that you have done, your project, you training, your certification if you passed it.

Month 6

Keep up the job hunt.

If you didn’t pass the certification in Month 5, repeat month 5 steps.

Take the first job you can get, no matter the pay. You are on your way to becoming an experience developer.

Month 7 and later

Keep it up.  Just because you didn’t quite make it 6 months doesn’t mean the work you have done isn’t valuable and isn’t going to pay off.

Still not passing the certification exam. Don’t worry, these test are hard and I know people who understand the technology of a certification that still fail multiple times.  Keep trying.

The long term career

Developers with 1-3 years experience can make 38k to 59k. With 4-8 years experience they can make 60 to 80k. And if you have worked hard to become a senior developer, your salary can skyrocket towards 6 figures and beyond.

 

Software Business Ideas

There are many ideas for starting a business and I have a nice list myself. If you are interested in an idea that you think could become a successful selling piece of software, I am will to help you an consult with you.  Just hit me up on the contact page.

Not into development?

Trying applying this pattern other careers. I think you find it works for many other career paths as well.  This could also apply to the medical field, housing market, and many other fields.

 

 

String extension methods

Here is a list of string extension methods. If you know of any more cool extension methods, please comment.

using System;
using System.Runtime.InteropServices;
using System.Text.RegularExpressions;
using System.Security;

namespace LANDesk.Install.Common.Extenders
{
    /// <summary>
    /// This class adds methods to String that are useful
    /// </summary>
    public static class StringExtender
    {
        #region DLL Imports
        [DllImport("ldmsinst.dll", CharSet = CharSet.Ansi, EntryPoint = "Decrypt", CallingConvention = CallingConvention.Cdecl)]
        public static extern void Decrypt(int pwdLen, byte[] pwd);

        [DllImport("ldmsinst.dll", CharSet = CharSet.Ansi, EntryPoint = "Encrypt", CallingConvention = CallingConvention.Cdecl)]
        public static extern void Encrypt(int length, byte[] data);
        #endregion

        /// <summary>
        /// This adds a function to a string object that will convert the string
        /// to a SecureString.
        /// </summary>
        /// <param name="password">The string to convert to a secure string.</param>
        /// <returns>SecureString</returns>
        public static SecureString ConvertToSecureString(this string password)
        {
            SecureString ss = new SecureString();
            if (password == null)
                throw new ArgumentNullException("password");

            foreach (Char c in password)
            {
                ss.AppendChar(c);
            }
            return ss;
        }

        /// <summary>
        /// Checks if the string contains any of a list of characters
        /// entered as a string.
        /// </summary>
        /// <param name="password">Any string.</param>
        /// <param name="listofcharacters">Any string representing a list of characters.</param>
        /// <returns>bool</returns>
        public static bool ContainsAnyOf(this string password, char[] listofcharacters)
        {
            foreach (char c in listofcharacters)
            {
                if (password.Contains(c.ToString()))
                    return true;
            }
            return false;
        }

        /// <summary>
        /// Checks if the string contains any of a list of characters
        /// entered as a string.
        /// </summary>
        /// <param name="password">Any string.</param>
        /// <param name="listofcharacters">Any string representing a list of characters.</param>
        /// <returns>bool</returns>
        public static bool ContainsAnyOf(this string password, string listofcharacters)
        {
            return password.ContainsAnyOf(listofcharacters.ToCharArray());
        }

        /// <summary>
        /// Checks if the string has a space.
        /// </summary>
        /// <param name="txt">Any string.</param>
        /// <returns>bool</returns>
        public static bool HasSpaces(string txt)
        {
            if (txt.Contains(" "))
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// To be a strong password, Must have 3 out of 4 of the following conditions:
        /// Uppercase, lowercase, numberic, symbol
        /// </summary>
        /// </summary>
        /// <param name="password"></param>
        /// <returns></returns>
        public static bool IsPasswordStrong(this string password)
        {
            int numTypes = 0;

            if (HasUppercaseCharacter(password) == true)
            {
                numTypes = numTypes + 1;
            }
            if (HasLowercaseCharacter(password) == true)
            {
                numTypes = numTypes + 1;
            }
            if (HasNonAlphaNumeric(password) == true)
            {
                numTypes = numTypes + 1;
            }
            if (HasDigit(password) == true)
            {
                numTypes = numTypes + 1;
            }

            if (numTypes > 2)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public static bool HasUppercaseCharacter(string str)
        {
            if (str == str.ToLower())
                return false;
            return true;
        }

        public static bool HasLowercaseCharacter(string str)
        {
            if (str == str.ToUpper())
                return false;
            return true;
        }

        public static bool HasDigit(string str)
        {
            Regex regexNums = new Regex("[0-9]");
            return regexNums.IsMatch(str);
        }

        public static bool HasNonAlphaNumeric(string str)
        {
            string newString = str;

            Regex regexNums = new Regex("[0-9]");
            Regex regexAlphas = new Regex("[a-z]", RegexOptions.IgnoreCase);

            newString = regexNums.Replace(newString, "");
            newString = regexAlphas.Replace(newString, "");

            return (newString.Length > 0);
        }

        /// <summary>
        /// This adds a function to a string object that RC4 Encrypts a password.
        /// </summary>
        /// <param name="password">A password as a string.</param>
        /// <returns>A byte[] representation of an RC4 password.</returns>
        public static byte[] Encrypt(this string password)
        {
            try
            {
                // Encode the password
                System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
                byte[] encryptedPwd = encoding.GetBytes(password);

                // encrypt password
                Encrypt(encryptedPwd.Length, encryptedPwd);
                return encryptedPwd;
            }
            catch (Exception ex)  
            {
                throw ex;
            }
        }

        /// <summary>
        /// This adds a function to a byte[] object that decrypts an RC4 password.
        /// and returns the password as a string.
        /// </summary>
        /// <param name="encryptedPwd">A byte[] representation of an RC4 password.</param>
        /// <returns>The password as a string.</returns>
        static public string Decrypt(this byte[] encryptedPwd)
        {
            string password = string.Empty;
            try
            {                
                    // decrypt password
                    Decrypt(encryptedPwd.Length, encryptedPwd);

                    // convert from byte array to string
                    password = System.Text.ASCIIEncoding.Default.GetString(encryptedPwd);
                    password = password.Replace("\0", string.Empty);                
            }
            catch (Exception ex) 
            {
                throw ex;
            }
            return password;
        }

        public static string FirstLetterUpperCase(this string inString)
        {
            if (string.IsNullOrEmpty(inString))
                return string.Empty;

            string newString = inString.Substring(0, 1).ToUpper();
            newString += inString.Substring(1, inString.Length - 1);
            return newString;
        }

        public static string LimitTo(this string data, int length) 
        {
            return data.Length < length ? data : data.Substring(0, length);
        }
    }
}

How to have the TextBlock in a left column of a Grid in a ListView Template expand or shrink with text wrapping?

Ok, lets say you want to have a Grid where each item is a row of data in a Grid. The left most column should expand or shrink, and yes the text should wrap when it shrinks.

Not so easy…but it can be done if you use the right tools.

  • Use a DockPanel not a Grid
  • Make the left most column the last one added
<Window x:Class="ListBoxWithWrap.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow"
        mc:Ignorable="d" xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        d:DesignHeight="242"
        d:DesignWidth="388"
        >
    <Grid>
        <DockPanel Name="MainGrid" HorizontalAlignment="Stretch">
            <!-- These four blocks will have other content eventually, but only need to be 45 wide -->
            <TextBlock Text="X" Grid.Column="1" HorizontalAlignment="Center" Width="45" DockPanel.Dock="Right"/>
            <TextBlock Text="X" Grid.Column="2" HorizontalAlignment="Center" Width="45" DockPanel.Dock="Right"/>
            <TextBlock Text="X" Grid.Column="3" HorizontalAlignment="Center" Width="45" DockPanel.Dock="Right"/>
            <TextBlock Text="X" Grid.Column="4" HorizontalAlignment="Center" Width="45" DockPanel.Dock="Right"/>
            <!-- This is the TextBlock that needs to wrap its content (and
                             change the height of the row (so the full content is still
                             visible) to whatever the available space is, but should not
                             make overall ListView wider than the parent's width. -->
            <TextBlock Text="A very long string that should wrap when the window is small." Padding="20,6,6,6" TextWrapping="Wrap" DockPanel.Dock="Right"/>
        </DockPanel>
    </Grid>
</Window>

You will see that this works as you desire.

Now put this in a ListView’s Template and set it to use Binding.

<Window x:Class="ListBoxWithWrap.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow"
        mc:Ignorable="d"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        d:DesignHeight="242"
        d:DesignWidth="388"
        SizeToContent="WidthAndHeight">
    <Grid>
        <ListView Name="lvWrap" ItemsSource="{Binding}">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <DockPanel Name="MainGrid" HorizontalAlignment="Stretch">
                        <!-- These four blocks will have other content eventually, but only need to be 45 wide -->
                        <TextBlock Text="X" Grid.Column="1" HorizontalAlignment="Center" Width="45" DockPanel.Dock="Right"/>
                        <TextBlock Text="X" Grid.Column="2" HorizontalAlignment="Center" Width="45" DockPanel.Dock="Right"/>
                        <TextBlock Text="X" Grid.Column="3" HorizontalAlignment="Center" Width="45" DockPanel.Dock="Right"/>
                        <TextBlock Text="X" Grid.Column="4" HorizontalAlignment="Center" Width="45" DockPanel.Dock="Right"/>
                        <!-- This is the TextBlock that needs to wrap its content (and
                             change the height of the row (so the full content is still
                             visible) to whatever the available space is, but should not
                             make overall ListView wider than the parent's width. -->
                        <TextBlock Text="{Binding Content}" Padding="20,6,6,6" TextWrapping="Wrap" DockPanel.Dock="Right"/>
                    </DockPanel>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </Grid>
</Window>

Now give it some data to bind to.

using System.Collections.Generic;
using System.Windows;
using System.Windows.Documents;

namespace ListBoxWithWrap
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            List<SomeItem> list = new List<SomeItem>();
            list.Add(new SomeItem() { Content = "Some very long string with so many words there should be some wrapping going on to prevent a line of text that is too long" });
            list.Add(new SomeItem() { Content = "Some very long string with so many words there should be some wrapping going on to prevent a line of text that is too long" });
            list.Add(new SomeItem() { Content = "Some very long string with so many words there should be some wrapping going on to prevent a line of text that is too long" });
            list.Add(new SomeItem() { Content = "Some very long string with so many words there should be some wrapping going on to prevent a line of text that is too long" });
            list.Add(new SomeItem() { Content = "Some very long string with so many words there should be some wrapping going on to prevent a line of text that is too long" });

            lvWrap.DataContext = list;
        }

        public class SomeItem
        {
            public string Content { get; set; }
        }
    }
}

The shrink with text wrapping no longer works once inside of the ListView. So that tells you that something to do with the ListView is breaking the feature you want.

Here is how you fix this:

1. Open your project in Expression Blend. (If you don’t have Expression Blend, maybe just look at my code below and copy it)

2. Right-Click on the ListView in the Object and Timeline tab and choose Edit Template | Edit a Copy.

3. Click OK on the next Window.

This will create the following resource code.

<Window.Resources>
		<SolidColorBrush x:Key="ListBorder" Color="#828790"/>
		<Style x:Key="ListViewStyle1" TargetType="{x:Type ListView}">
			<Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.WindowBrushKey}}"/>
			<Setter Property="BorderBrush" Value="{StaticResource ListBorder}"/>
			<Setter Property="BorderThickness" Value="1"/>
			<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
			<Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Auto"/>
			<Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto"/>
			<Setter Property="ScrollViewer.CanContentScroll" Value="true"/>
			<Setter Property="ScrollViewer.PanningMode" Value="Both"/>
			<Setter Property="Stylus.IsFlicksEnabled" Value="False"/>
			<Setter Property="VerticalContentAlignment" Value="Center"/>
			<Setter Property="Template">
				<Setter.Value>
					<ControlTemplate TargetType="{x:Type ListView}">
						<Border x:Name="Bd" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Padding="1" SnapsToDevicePixels="true">
							<ScrollViewer Focusable="false" Padding="{TemplateBinding Padding}">
								<ItemsPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
							</ScrollViewer>
						</Border>
						<ControlTemplate.Triggers>
							<Trigger Property="IsEnabled" Value="false">
								<Setter Property="Background" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"/>
							</Trigger>
							<Trigger Property="IsGrouping" Value="true">
								<Setter Property="ScrollViewer.CanContentScroll" Value="false"/>
							</Trigger>
						</ControlTemplate.Triggers>
					</ControlTemplate>
				</Setter.Value>
			</Setter>
		</Style>
	</Window.Resources>

4. Now look at what is surrounding the ItemPresenter. Yes, you see the ScrollViewer, which is your problem. Delete it.

5. Build you project.

Success! Now your feature to both expand or shrink with text wrapping is back.

Here is the final XAML.

<Window x:Class="ListBoxWithWrap.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow"
        mc:Ignorable="d"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        d:DesignHeight="242"
        d:DesignWidth="388"
        SizeToContent="WidthAndHeight">
    <Window.Resources>
        <SolidColorBrush x:Key="ListBorder" Color="#828790"/>
        <Style x:Key="ListViewStyle1" TargetType="{x:Type ListView}">
            <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.WindowBrushKey}}"/>
            <Setter Property="BorderBrush" Value="{StaticResource ListBorder}"/>
            <Setter Property="BorderThickness" Value="1"/>
            <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
            <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Auto"/>
            <Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto"/>
            <Setter Property="ScrollViewer.CanContentScroll" Value="true"/>
            <Setter Property="ScrollViewer.PanningMode" Value="Both"/>
            <Setter Property="Stylus.IsFlicksEnabled" Value="False"/>
            <Setter Property="VerticalContentAlignment" Value="Center"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type ListView}">
                        <Border x:Name="Bd" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Padding="1" SnapsToDevicePixels="true">
                            <ItemsPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsEnabled" Value="false">
                                <Setter Property="Background" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"/>
                            </Trigger>
                            <Trigger Property="IsGrouping" Value="true">
                                <Setter Property="ScrollViewer.CanContentScroll" Value="false"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Window.Resources>
    <Grid>
        <ListView Name="lvWrap" ItemsSource="{Binding}" Style="{DynamicResource ListViewStyle1}">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <DockPanel Name="MainGrid" HorizontalAlignment="Stretch">
                        <!-- These four blocks will have other content eventually, but only need to be 45 wide -->
                        <TextBlock Text="X" Grid.Column="1" HorizontalAlignment="Center" Width="45" DockPanel.Dock="Right"/>
                        <TextBlock Text="X" Grid.Column="2" HorizontalAlignment="Center" Width="45" DockPanel.Dock="Right"/>
                        <TextBlock Text="X" Grid.Column="3" HorizontalAlignment="Center" Width="45" DockPanel.Dock="Right"/>
                        <TextBlock Text="X" Grid.Column="4" HorizontalAlignment="Center" Width="45" DockPanel.Dock="Right"/>
                        <!-- This is the TextBlock that needs to wrap its content (and
                             change the height of the row (so the full content is still
                             visible) to whatever the available space is, but should not
                             make overall ListView wider than the parent's width. -->
                        <TextBlock Text="{Binding Content}" Padding="20,6,6,6" TextWrapping="Wrap" DockPanel.Dock="Right"/>
                    </DockPanel>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </Grid>
</Window>

You should now have a little bit more understanding of the ListView template and how to manipulate it, which should translate to other objects in WPF as well.

How to copy a directory recursively in C#?

Today I had the task of copying a directory recursively in C#. The first thing I did was type into my project “Directory.” and see if intellisense brought up a function called Directory.Copy. Unfortunately there is not a Directory.Copy, which to me seems like an oversight, as copying a directory is fairly common. Maybe it is not an action commonly done in C#.

So next I went to my browser and did a search. Well, I am not the first, nor will I be the last to solve this. There are a number of solutions online and each seem to have both positive and negative comments.

Option 1 – A directory copy using recursion

Pros: This is simple and to the point and elegant.

Cons: One complaint is that this could cause a stack overly by using recursion but this might not be a valid con as the filesystem restrictions in Windows might prevent a stack overflow anyway.

Since I am dealing with a small controlled tree, the con will not affect me., but since we are dealing with a file system, this is likely not to occur.

        private static void CopyFilesRecursively(DirectoryInfo source, DirectoryInfo target)
        {
            foreach (DirectoryInfo dir in source.GetDirectories())
                CopyFilesRecursively(dir, target.CreateSubdirectory(dir.Name));
            foreach (FileInfo file in source.GetFiles())
                file.CopyTo(Path.Combine(target.FullName, file.Name));
        }

Option 2 – Use SearchOption.AllDirectories to create the directory structure then then copy all the files

Pros: Does not use recursion
Cons: Are there any?

        private static void CopyFilesRecursively(String SourcePath, String DestinationPath)
        {
            // First create all of the directories
            foreach (string dirPath in Directory.GetDirectories(SourcePath, "*", SearchOption.AllDirectories))
                Directory.CreateDirectory(dirPath.Replace(SourcePath, DestinationPath));

            // Copy all the files
            foreach (string newPath in Directory.GetFiles(SourcePath, "*.*", SearchOption.AllDirectories))
                File.Copy(newPath, newPath.Replace(SourcePath, DestinationPath));
        }

Option 3 – Use one of the methods recommended by MSDN

Unfortunately MSDN is not consistent, and recommends two different methods in two different places, which I am sure were written by different teams.

using System;
using System.IO;

class DirectoryCopyExample
{
    static void Main()
    {
        DirectoryCopy(".", @".\temp", true);
    }

    private static void DirectoryCopy(
        string sourceDirName, string destDirName, bool copySubDirs)
    {
      DirectoryInfo dir = new DirectoryInfo(sourceDirName);
      DirectoryInfo[] dirs = dir.GetDirectories();

      // If the source directory does not exist, throw an exception.
        if (!dir.Exists)
        {
            throw new DirectoryNotFoundException(
                "Source directory does not exist or could not be found: "
                + sourceDirName);
        }

        // If the destination directory does not exist, create it.
        if (!Directory.Exists(destDirName))
        {
            Directory.CreateDirectory(destDirName);
        }

        // Get the file contents of the directory to copy.
        FileInfo[] files = dir.GetFiles();

        foreach (FileInfo file in files)
        {
            // Create the path to the new copy of the file.
            string temppath = Path.Combine(destDirName, file.Name);

            // Copy the file.
            file.CopyTo(temppath, false);
        }

        // If copySubDirs is true, copy the subdirectories.
        if (copySubDirs)
        {

            foreach (DirectoryInfo subdir in dirs)
            {
                // Create the subdirectory.
                string temppath = Path.Combine(destDirName, subdir.Name);

                // Copy the subdirectories.
                DirectoryCopy(subdir.FullName, temppath, copySubDirs);
            }
        }
    }
}

Or the different one they posted here:

using System;
using System.IO;

class CopyDir
{
    public static void CopyAll(DirectoryInfo source, DirectoryInfo target)
    {
        if (source.FullName.ToLower() == target.FullName.ToLower())
        {
            return;
        }

        // Check if the target directory exists, if not, create it.
        if (Directory.Exists(target.FullName) == false)
        {
            Directory.CreateDirectory(target.FullName);
        }

        // Copy each file into it's new directory.
        foreach (FileInfo fi in source.GetFiles())
        {
            Console.WriteLine(@"Copying {0}\{1}", target.FullName, fi.Name);
            fi.CopyTo(Path.Combine(target.ToString(), fi.Name), true);
        }

        // Copy each subdirectory using recursion.
        foreach (DirectoryInfo diSourceSubDir in source.GetDirectories())
        {
            DirectoryInfo nextTargetSubDir =
                target.CreateSubdirectory(diSourceSubDir.Name);
            CopyAll(diSourceSubDir, nextTargetSubDir);
        }
    }

    public static void Main()
    {
        string sourceDirectory = @"c:\sourceDirectory";
        string targetDirectory = @"c:\targetDirectory";

        DirectoryInfo diSource = new DirectoryInfo(sourceDirectory);
        DirectoryInfo diTarget = new DirectoryInfo(targetDirectory);

        CopyAll(diSource, diTarget);
    }

    // Output will vary based on the contents of the source directory.
}

Option 4 – Use Microsoft.VisualBasic

Pros: One line.
Cons: You have to reference an additional dll that you otherwise do not need.
This additional dll is not available in Mono for cross-platform development.

new Microsoft.VisualBasic.Devices.Computer().
    FileSystem.CopyDirectory( sourceFolder, outputFolder );

There are various other methods:

Resources:
http://stackoverflow.com/questions/58744/best-way-to-copy-the-entire-contents-of-a-directory-in-c
http://msdn.microsoft.com/en-us/library/system.io.directoryinfo.aspx
http://msdn.microsoft.com/en-us/library/bb762914.aspx
http://xneuron.wordpress.com/2007/04/12/copy-directory-and-its-content-to-another-directory-in-c/
http://www.logiclabz.com/c/copy-directory-in-net-c-including-sub-folders.aspx

A quick overview of MVVM

Model View ViewModel (MVVM) is a design pattern based on Model View Controller (MVC) but specifically tailored to Windows Presentation Foundation (WPF).

MVVM is not a framework per se but many frameworks have been created. Here is a list of MVVM Frameworks from Wikipedia.

See the Wikipedia site here: Open Source MVVM Frameworks.

Another blog, has some basic information on many of these here: A quick tour of existing MVVM frameworks

A framework is actually not necessary to implement MVVM and you should seriously consider whether using one is right for your WPF application or not. Many applications do not need much of the features the frameworks provide. However, there are two common classes that all MVVM frameworks contain. These are ViewModelBase and RelayCommand. Though some frameworks may give them different names or implement them slightly differently, they all have these classes. For example, MVVM Foundation names the ViewModelBase differently. It is called ObservableObject, which is more appropriate because it is incorrect to assume that all objects that implement INotifyPropertyChanged are going to be ViewModel objects.

Instead of installing and using an MVVM framework, you could simply include these classes in your application, as these are all you need to implement MVVM.

  • ObservableObject
  • RelayCommand

While these two classes are enough, you may want to investigate how different MVVM Frameworks implement and what else they implement and why. You may find that another feature implemented is exactly what you need in your application and knowing about it could save you time.

The 8 Types of Technical Documentation and Why Each Is Important

Technical documentation is critical to the success of any software. However, most creators of software struggle to provide adequate documentation for their product. Rare is the software that is praised for its documentation. When documentation is praised, it is often only praised for having some documentation, which is more than most, but in reality documentation is usually still inadequate.

So what constitutes adequate documentation? Well, if a user wants to do something with your software and the documentation helps them succeed in a timely manner, then the documentation is adequate. However, accomplishing this is not as easy as it sounds.

Why most companies fail to document properly

Most companies do not document their product thoroughly for a few reasons.

  • Lack of a defined list of all types of documentation
  • Lack of understanding of each type of documentation
  • Documentation is not made a priority and lacks of funding

Lack of a defined list of all types of documentation

Many cannot name more than one or two forms of documentation. To be successfully with documentation, a software company must first enumerate the types of documentation. Then it must learn about each type of documentation and understand the role that each type of documentation plays. It is also critical to understand the different target audiences each type has. Also, what are the common mistakes made when trying to create each type of documentation so these mistakes can be avoided.

Attempts are made to document software in different ways. However, because a complete documentation set is not defined, success is nearly impossible. To make matters worse, there is little to no reporting or visibility into the level of documentation a given piece of software has. I have never encountered software that has reached a 100% documentation level.

In order to succeed there must be an understanding of the types of documentation.

  1. Step-by-Step Walk-thrus – Also called Guides, How to’s, or Examples, Quick Start Guides
  2. Product feature documentation – This is lists all the features and settings without really any real world examples. Often the help button inside the software points to sections of this document.
  3. Troubleshooting Documentation – What to do when a failure occurs. Where are the logs and how to read them. How to turn on or increase logging and debugging.
  4. Knowledge-base (Problem, Cause, Resolution), Frequently Asked Questions (FAQ), and Forums
  5. Code, API, or SDK Documentation
  6. Internal Development Documentation – Such as code and development documentation, internal only features use by developers and/or testers, architecture documentation (Note: For open source projects this information is usually public)
  7. Real life customer implementations – Examples of how a company has a product implemented in real life
  8. Marketing documentation – Basic over views of the value the software has for the company, ROI claims, general feature lists, costs, etc…

The worst documentation of all is of course the absence of documentation. However, most software companies are unaware that there are entire areas of documentation that are lacking. To have complete documentation you must provide it in all of these areas.

Lack of understanding of each type of documentation

Since most software companies are unaware of the list above, it makes sense that they don’t understand the items on the list. This is why they have no direction and their documentation is a sporadic combination of the different documentation types, never fully succeeding to accomplish the primary goal of documentation, which is to enable the reader to succeed.

In order to create excellent documentation, a full understanding of each type of documentation is requisite. Without this understanding, documentation your documentation will continue to be lacking.

The lack of understanding also leads to assumptions that are not true. Some think that if they try to document every setting their software has they will have complete documentation. Usually when this is done, there is so much effort put into this that providing a simple example is forgotten. Often I hear this question:

Why would an example be needed, every feature is documented?

I would answer this question as follows:

Information overload. Now there is so much documentation in one white paper that someone who wants to do something simple is unsure that it is simple. They don’t know which features they must setup and which are unnecessary or should remain as defaults.

I often find this with Open Source documentation and unfortunately when a user asks for an example they are often rudely told to “Read the Manual” or RTM. However, the manual is usually hundreds of pages and they probably need to read one page of the manual but just don’t know where to start.

If have seen documentation using only examples as well. However, when an attempt is made to deviate from the examples, there is nothing left in the documentation to provide the guidance necessary to succeed.

Some documentation is better defined, such as that created from the results of support calls, forums, or mailing lists. Because this type of documentation is completely reactionary, this is one area of documentation that is better defined. The documentation is created after a problem is experienced and has to be dealt with. However, once created, it exists to benefit others. As this documentation type is better defined you might not be surprised to know that it has its own acronym: KCS or Knowledge Centered Support.

The goal of this article is to raise awareness of all types of technical documentation and make them all as well-defined as support documentation.

Documentation is not made a priority and lacks of funding

Investing in documentation is expensive. But it is usually and expense that pays off. If an analyst has to choose between two competing software applications and one is well documented and one is not, the well documented software application is likely to be chosen. Many organizations fail to see the ROI in documentation and therefore choose not to invest.

It is obviously that lack of funding for documentation is an industry wide phenomenon. While technical writing has been around since even before software, a standard for documentation whether it be creating documentation, updating documentation, managing documentation, and reporting on documentation has yet to formally exist. However, I did find this link, which shows I am not the only one who has identified this problem: http://www.hci.com.au/iso/

So lets get back to our list. Below I will go through each type of documentation and provide some information on it.

Type 1 – Step-by-step Walk-thrus – Also called Guides, How to’s, or Examples, Quick Start Guides

Description

This type of documentation is nothing more than actions that the reader will take to accomplish something with your software. This documentation, when done right, could be followed by the most computer illiterate. If they read and follow each step, even if they have no idea what they are doing, they should succeed.

You may also want to read: Your project’s ‘Getting Started’ tutorial sucks – Why time to success matters

Role

To provide the most common, most tested, most successful, and best overall example of how to accomplish some particular task from start to finish with your software.

Audience

Most commonly, trainees and new or evaluation users. However, anyone who wants to achieve the results the step-by-step guide leads to is included. This is most often, but is not limited to, users of your software. It includes deployment engineers, configuration specialists, support engineers, and demo or sales engineers.

Common Article Names

  • Quick Start Guide
  • Step-by-step Guide for setting up “Software X”
  • How to configure “Some Feature” of “Software X”

Common mistakes

There are many common mistakes

  • Not clearly defining the starting point of the walk-thru. Think of the starting point of a software that installs on Windows. What version of Windows, what other software must be installed, etc…
  • Defining the starting point clearly, but using a starting point most people don’t know how to get to. For example, you starting point should probably not say “have SQL Server installed and a database created with credentials” without providing steps.
  • Assuming the reader knows how to accomplish a task, so the documentation simply states to “do task x” instead of walking the reader through doing the task.
  • Skipping steps or forgetting steps.
  • The development department changes the steps just before release but the documentation is not updated to match.
  • Trying to simultaneously provide Product Feature Documentation in the middle of your steps. A link or note is acceptable for steps or settings that customers commonly customize.
  • Trying to provide comprehensive troubleshooting documentation after each step. It is great to have a link or a reference to troubleshooting documentation but it shouldn’t interfere with the walk-thru.
  • Only creating step-by-step guides for a couple common features of your software.
  • Failing to add documentation after use. For example, when a consultant, support engineer, or other employee struggles to set up a not-well-documented feature and once successful, they still don’t document it.

Type 2 – Product feature documentation

Description

This type of documentation is a description of every feature and setting. What it is used for, when and why one would use the feature or setting.

Role

This is for users who need to stray from the common walk-thrus and need to know what alternate and uncommon settings are used for so they can determine which they need in their particular environment.

Audience

Any customer/user who needs more than the most common features. Your own support representatives and architect or professional services teams. Consultants who recommend your product or are trusted to determine if your product meets a feature set for potential customers/users.

Common Article Names

  • The Software X Handbook
  • Software X: The Complete Reference
  • Understanding Feature Y of Software X

Common mistakes

  • Burying the features in other documentation, such as walk-thrus.
  • Not including at least a comment about when the feature would be used.
  • Not being aware of the features your customers/users are aware of and using. There are lots of “unintended features” and you should capture them in documentation.
  • Not letting customers contribute to this documentation in some way, even if it is just comments (this is the best way to solve the above issue, too).

Type 3 – Troubleshooting documentation

Description

This documentation describes steps to diagnose problems. It includes information on logs files. It includes information on the behind the scenes business your software is doing, such as process/thread work, file or data interaction, etc…

If the users tries to do some task with your software and it fails, to them, a single task failed. However, to fix it, one might need to know that behind the scenes ten different processes occurred. It is important to be able to diagnose which background processes worked and pinpoint which one failed, so you don’t troubleshoot all ten background processes when only maybe the seventh is the problem.

Role

To help customers/users get pasts unexpected issues and to help support engineers diagnose issues. These don’t have to always be public, but should be in the hands of your support engineers.

Audience

This is for support engineers more than customers, though the more experienced and “get your hands dirty” customers/users will use it. Engineers who do on site installation or on site configuration may need this information for when they run into bumps.

Common Article Names

  • Feature X: The complete troubleshoot guide
  • Troubleshooting Feature X

Common mistakes

  • Confusing “Problem, Cause, Resolution” documentation (also called Knowledge Base articles) with Troubleshooting documentation.
  • Not creating this documentation because you assume product feature documentation covers this. It doesn’t.
  • Providing this documentation but not providing complete troubleshooting steps for whatever reason. Especially if troubleshooting is done with 3rd party software and outside your own product it is assumed outside the scope when it is not. For example, a product that requires a DNS server, should provide steps to make sure that a DNS server is configured as the product expects. You may not have to write such documentation if the 3rd party vendor has some, but you should link to/reference it in your own documentation.
  • Writing documents that have lists of “fixes to try”. This documentation should almost never include “fixes”, but instead should diagnose the issue or pinpoint the problem so precisely that the fix becomes obvious whether the fix currently exists or not.

Type 4 – Knowledge-base (Problem, Cause, Resolution)

Description

This documentation is most commonly the result of customer support tickets/cases. It lists a specific problem, a specific cause of the problem, and a single resolution to that problem. As mentioned early this is one of the more well-defined areas of documentation. Read more here about KCS or Knowledge Centered Support.

Role

To make it so an issue only has to be troubleshot and fixed once. Once an issue is fixed, the Problem, Cause, Resolution can be documented and the fix can be applied without troubleshooting when the same Problem and Cause occurs.

To keep knowledge in-house. Tech Support is a high turnover position so keeping knowledge in-house is not always the easiest task.

Audience

Customers who experience a problem. Support engineers or other employees to whom the problem is reported.

Common Article Names

Frequently Asked Questions or FAQ.

Common Issues.

Common mistakes

  • Providing multiple Problems, Causes, or fixes in the same article.
  • Providing a problem and a list of fixes with no way to determine which fix is the correct fix.
  • Having an article that recommends a fix even when a customer is not really having that problem.
  • Failing to provide a good search for the knowledge base articles.

Type 5 – Code, API, or SDK Documentation

Description

This documentation describes how others use your code or libraries to write add-ons, plugins, integration, or otherwise customize your application through code. Do not confuse this with Internal Development Documentation. This type is for external users or resellers or middle-ware companies.

Role

This documentation helps others code with your code and libraries. Software that a customer/user takes the time and expense to modify to fit their environment becomes “sticky”, meaning the customer/user is likely to be loyal.

Audience

Systems Analyst / Developers / Integration Engineers / Middle-ware companies / Resellers. Customers who need to extend your product to meet a business need. Or in an open source environment, how others can use your code to extend their own project.

Common Article Names

  • Software X SDK Documentation
  • Class or Function Reference for Software X API

Common mistakes

  • Providing zero documentation on this
  • Providing incorrect documentation about a function
  • Updating code but not updating the documentation
  • Deprecating code but not informing the consumer
  • Not providing the first type of documentation: Samples, walk-thrus, etc…

Type 6 – Internal Development Documentation

Description

This is used for internal developers continue future enhancements and otherwise maintain a piece of software.

Role

To help developers work with a piece of code. To overcome turnover so new developers can pick up code another developer created. To provide architecture and design of each piece of code. To give UML (usually the classes and their methods),

Audience

Internal developers. Sometimes support.

Common Article Names

There are really no common names, but usually these types of documentation are internal only.

Common mistakes

  • Not writing such documentation at all.
  • Not documentation all parts of the code: Classes, Functions, design and architecture, supported features, etc…

Type 7 – Real life customer implementations

Description

This is documentation about customers success stories. About how they implemented your software in their environment (which is usually as messed up as everyone else’s environments).

Role

To demonstrate that the software can be successful and has proven itself in real life customer environments.

Audience

Other customers / System Analysts / Internal Employees in charge of future enhancements and road maps

Common Article Names

  • Product X Success Stores
  • How Company Y succeeded with Product X

Common mistakes

  • Not providing any customer success stories.
  • Providing success stories from unhappy customers who when contacted, speak poorly of your product

Type 8 – Marketing documentation

Description

This is documentation that doesn’t really say much more than is needed to let a customer know about a software solution.

Role

To acquire more customers. To help potential customers determine features sets quickly.

Audience

Systems Analysts / Consultants / Sales Engineers / Evaluation customers.

Common Article Names

  • Product X
  • The Product X Feature Set
  • What Product X can do for you business

Common mistakes

I don’t know a lot of the mistakes made in this documentation type, as my exposure to marketing is limited. I almost forgot this documentation type.

  • Too complex, including information or overly complex images or diagrams that are hard to understand

Conclusion

Hopefully after reading this article, you have a greater understanding of documentation.

Now that you know all the types of documentation, there are other problems to address. How to write the documentation. How to choose the priority for writing these types of documentation. How to balance the cost of documentation against the opportunity cost of not having documentation.

Some day, I will also have to write a post on how to deal with “versioning” documentation including updating documentation when Software versions change. I think there is a market for a piece of software that does nothing but track documentation. Hopefully it is well documented. 🙂

The Array class in C#

System.Array is an important class to know and understand as almost any programming work has arrays.

Array is an abstract class, so you cannot establish and instance of it. That is OK because it is really just a “function holder” class.  The term “function holder” is not an official C# term, but it is a name I give classes that really just exist to hold functions.  Array is a “function holder” for array functions.

Lets take a moment to learn a few of the functions in the Array class. We are going to use an array of high scores to learn from.

Array.Sort

Let start by creating an array of high scores. The following code has a function that takes a score and adds it to the list if it is greater than the lowest high score and sorting them.

using System;

namespace ArrayLearning
{
    class Program
    {
        static int[] highScores = new int[12];

        static void Main(string[] args)
        {
            // keep that top 12 high scores
            AddScore(10100);
            AddScore(13710);
            AddScore(14190);
            AddScore(12130);
            AddScore(12130);
            AddScore(16140);
            AddScore(19320);
            AddScore(07250);
            AddScore(18140);
            AddScore(08090);
            AddScore(10010);
            AddScore(14380);
            AddScore(18140);
            AddScore(12190);
            AddScore(19320);
        }

        static void AddScore(int newScore)
        {
            // Pre-sort the high scores in case somehow the
            // first one is not the lowest.
            Array.Sort(highScores);

            // If the new score is greater than the lowest
            // high score, replace the lowest high score
            if (highScores[0] < newScore)
                highScores[0] = newScore;

            // Now resort the scores as the new score may
            // not be in the right place
            Array.Sort(highScores);

            DisplayHighScores();
        }

        private static void DisplayHighScores()
        {
            // Print the new high scores
            for (int i = highScores.Length - 1; i > -1; i--)
            {
                Console.WriteLine(highScores[i]);
            }
            Console.WriteLine("Press any key to continue");
            Console.ReadKey();
            Console.WriteLine();
        }
    }
}

Ok, I am not saying this is the proper way to handle high scores in a game.  I am just demonstrating Array.Sort for you.

Array.Clear

Ok, lets show an example of Array.Clear. This is a very simple function that sets all the values in the array to their default value. Booleans have a default value of false. Double, Int32, Int64, etc, have a default value of 0. Reference types have a default value of null.

Here is an enhancement to the above snippet that allows demonstrates clearing an entire array. Notice Array.Clear takes the array, then the starting item in the array and the number items in the array. In this example we clear the entire array, but that is not required.

using System;

namespace ArrayLearning
{
    class Program
    {
        static int[] highScores = new int[12];

        static void Main(string[] args)
        {
            // keep that top 12 high scores
            AddScore(10100);
            AddScore(13710);
            AddScore(14190);
            AddScore(12130);
            AddScore(12130);
            AddScore(16140);
            AddScore(19320);
            AddScore(07250);
            AddScore(18140);
            AddScore(08090);
            AddScore(10010);
            AddScore(14380);
            AddScore(18140);
            AddScore(12190);
            AddScore(19320);

            // Clear the high scores
            ClearHighScores();
        }

        static void AddScore(int newScore)
        {
            // Pre-sort the high scores in case somehow the
            // first one is not the lowest.
            Array.Sort(highScores);

            // If the new score is greater than the lowest
            // high score, replace the lowest high score
            if (highScores[0] < newScore)
                highScores[0] = newScore;

            // Now resort the scores as the new score may
            // not be in the right place
            Array.Sort(highScores);

            DisplayHighScores();
        }

        public static void ClearHighScores()
        {
            Console.WriteLine("Clearing High Scores!");
            Array.Clear(highScores, 0, highScores.Length);
            DisplayHighScores();
        }

        private static void DisplayHighScores()
        {
            // Print the new high scores
            for (int i = highScores.Length - 1; i > -1; i--)
            {
                Console.WriteLine(highScores[i]);
            }
            Console.WriteLine("Press any key to continue");
            Console.ReadKey();
            Console.WriteLine();
        }
    }
}

Array.IndexOf

Maybe you want to display the place in the top 12 that the player scored. However, the way we are adding the score does not track the new score’s place. So we need to find the place. Here is a small enhancement to the above code that demonstrates the use of Array.IndexOf to find the place.

Notice the addition of the DisplayCurrentPlace function and it’s use of Array.IndexOf. Also since the array is actually sorted with the lowest number at index 0 and the highest score at index 11, I can just subtract the index from the max lengh of 12 high scores to get the correct place.

using System;

namespace ArrayLearning
{

    class Program
    {
        public static int MAX = 12;
        static int[] highScores = new int[MAX];

        static void Main(string[] args)
        {
            // keep that top 12 high scores
            AddScore(10100);
            AddScore(13710);
            AddScore(14190);
            AddScore(12130);
            AddScore(12130);
            AddScore(16140);
            AddScore(19320);
            AddScore(07250);
            AddScore(18140);
            AddScore(08090);
            AddScore(10010);
            AddScore(14380);
            AddScore(18140);
            AddScore(12190);
            AddScore(19320);
            AddScore(06320);

            // Clear the high scores
            ClearHighScores();
        }

        static void AddScore(int newScore)
        {
            // Pre-sort the high scores in case somehow the
            // first one is not the lowest.
            Array.Sort(highScores);

            // If the new score is greater than the lowest
            // high score, replace the lowest high score
            if (highScores[0] < newScore)
            {
                highScores[0] = newScore;

                // Now resort the scores as the new score may
                // not be in the right place
                Array.Sort(highScores);

                DisplayHighScores();
                DisplayCurrentPlace(newScore);
            }
        }

        public static void ClearHighScores()
        {
            Console.WriteLine("Clearing High Scores!");
            Array.Clear(highScores, 0, highScores.Length);
            DisplayHighScores();
        }

        private static void DisplayHighScores()
        {
            // Print the new high scores
            for (int i = highScores.Length - 1; i > -1; i--)
            {
                Console.WriteLine(highScores[i]);
            }
        }

        private static void DisplayCurrentPlace(int newScore)
        {
            Console.WriteLine(String.Format("You placed number {0} out the top 12.", MAX - Array.IndexOf(highScores, newScore)));

            Console.WriteLine("Press any key to continue");
            Console.ReadKey();
            Console.WriteLine();
        }
    }
}

Well, these were three simple examples of using the Array class. As you can see it is little more than a “function holder”.

If you have an example of a function in the Array class, please post it here in the comments.

Getting better with the String and StringBuilder classes in C#

So we all use the Strings in C# and most of us consider ourselves experts.  But are we really experts.  When you’re getting tested and you don’t have Visual Studio or MSDN in front of you, are you ready to answer the questions with confidence?

Sometimes it is worth while to re-learn the basics because often simple features you are not using would save you time and prevent you from failing to appear as knowledgeable as you really are. Especially if you have not had a project where you manipulate strings, because you do other complex tasks and you do little more with strings than assign them values and compare them.

Well, here are the two links to the String and StringBuilder objects on MSDN.

Take a moment and read the MSDN pages about these objects even if you have read them before.

Here are some questions that you should answer about these classes.

  1. Which is mutable and which is immutable? What does that mean?

    Answer:

    String is immutable.  That means that once you create a string, it cannot be altered in place in memory, but instead, any alteration causes a copy with the modification to be created in a new memory location and the reference is updated to refer to the copy. For example if you append a character you actually get a whole new string in a whole new memory location.

    StringBuilder is mutable.  That means that you can change the object in place in memory without creating a copy.  For example, if you append a character to StringBuilder it can add it to the same space in memory.  There is one exception.  If you add so many characters that the next character you add make the string bigger than the memory location’s capacity, a new object in memory is created. The default capacity for this implementation is 16 characters, and the default maximum capacity is Int32.MaxValue. (1) So if you are playing with Strings greater than 16 characters, you should instantiate your StringBuilder objects with a capacity greater than 16.

  2. So can you modify a string in place in memory?

    Answer:

    No. And yes.

    Read this MSDN article: How to: Modify String Contents (C# Programming Guide)

    The short version of the above article is that you can’t normally. But if you use “unsafe” you can, the above link provides the following unsafe code:

    class UnsafeString
    {
        unsafe static void Main(string[] args)
        {
            // Compiler will store (intern)
            // these strings in same location.
            string s1 = "Hello";
            string s2 = "Hello";
    
            // Change one string using unsafe code.
            fixed (char* p = s1)
            {
                p[0] = 'C';
            }
    
            //  Both strings have changed.
            Console.WriteLine(s1);
            Console.WriteLine(s2);
    
            // Keep console window open in debug mode.
            Console.WriteLine("Press any key to exit.");
            Console.ReadKey();
        }
    }
    

    Would you actually do this though?

  3. What is the easiest way to reverse a String?
        public static string ReverseString(string s)
        {
    	char[] arr = s.ToCharArray();
    	Array.Reverse(arr);
    	return new string(arr);
        }
    

    Notice another C# class called Array was used. This class, like String and StringBuilder, is another class that you may be forgetting about an overlooking.

    When I was asked how to reverse a string, the Array.Reverse function did not come to mind and so I recreated it with this function, which obviously takes more time and a developer’s time is too expensive to recreate work already done for you.

        private static String Reverse(String inString)
        {
            char[] myChars = inString.ToCharArray();
            for (int i = 0, j = inString.Length - 1; i < inString.Length / 2; i++, j--)
            {
                char tmp = inString[j];
                myChars[j] = inString[i];
                myChars[i] = tmp;
            }
            return new string(myChars);
        }
    
  4. Which is more efficient for concatenation or appending characters, String or StringBuilder?

    StringBuilder is by far more efficient. Microsoft has a knowledge base article on this here: http://support.microsoft.com/kb/306822

    I changed the code slightly to use the Stopwatch to time the ticks to get more accurate data.

    using System;
    using System.Diagnostics;
    
    namespace StringBuilderPerformance
    {
        class Program
        {
            static void Main(string[] args)
            {
                // The greater the loops the better the performance ratio
                const int sLen = 30, Loops = 10000;
                int i;
                string sSource = new String('X', sLen);
                string sDest = "";
                //
                // Time string concatenation.
                //
                Stopwatch timer = new Stopwatch();
                timer.Start();
                for (i = 0; i < Loops; i++) sDest += sSource;
                timer.Stop();
                long stringTicks = timer.ElapsedTicks;
                Console.WriteLine("Concatenation took " + stringTicks + " ticks.");
                //
                // Time StringBuilder.
                //
                timer.Reset();
                timer.Start();
                System.Text.StringBuilder sb = new System.Text.StringBuilder((int)(sLen * Loops * 1.1));
                for (i = 0; i < Loops; i++) sb.Append(sSource);
                sDest = sb.ToString();
                timer.Stop();
                long stringBuilderTicks = timer.ElapsedTicks;
                Console.WriteLine("String Builder took " + stringBuilderTicks + " ticks.");
                Console.WriteLine("Using StringBuilder takes " + ((double)stringBuilderTicks / (double)stringTicks * (double)100).ToString("F") + "% of the time that using String takes.");
    
                //
                // Make the console window stay open
                // so that you can see the results when running from the IDE.
                //
                Console.WriteLine();
                Console.Write("Press any key to finish ... ");
                Console.ReadKey();
            }
        }
    }
    

    Here are the results:

    Concatenation took 5338055 ticks.
    String Builder took 2213 ticks.
    Using StringBuilder takes 0.04% of the time that using String takes.

    Press any key to finish …

Anyway, I hope this post helps you remember the basics.

Three days without internet access

So this weekend my wife’s side of the family took a trip to Bear Lake, where we stayed at “a cabin” (a 6000+ square foot home near the lake). It is tough to ask for a better place to reside while on a trip to a lake. It is only a quarter-mile from the dock, so getting on and off the water is easy to do and possible to do multiple times a day.

However in the later hours of the night as I sat and relaxed my sore shoulders (from wake-boarding and yes I can wake board, do 180’s and cross the wake…maybe a little jump…OK, I barely get air if I am lucky going over the wake but it is fun). Anyway, I desperately missed having the one thing that I have been seldom without since 1999: The Internet.

That is right…there was no internet.  Sure, this is partly my fault as I don’t have an internet capable phone yet (normally I have wi-fi everywhere I go so I haven’t had the need yet).

However, life before the internet was possible so it had to be possible now right! Sure it is. I might be lame but I wanted to brush up on a few C# topics while I relaxed in just before bed. I did have a C# book.  Sure it is a PDF book but it was actually on my computer, not on the internet so I pulled the chapters and read them.  It wasn’t quite the same as seeing a dozen Google links to different blogs explaining the concepts in different ways, but it was still worth-while.

I did have a few hundred email to go through when I got back…I will probably be contacting my internet provider about the option to have an Air card.

Overall, the trip was quite enjoyable and as you can tell from this post…I am back in front of the internet.

A WPF front-end for LDPing

I wrote a front-end to LDPing last week-end. You can check it out here:

LDPing

So I was writing a WPF front-end for LDPing, which is a method of querying a LANDesk Agent for its computer name and Inventory Id. There is a button that you click to launch the ping and I couldn’t get the thing to enable…Anyway, I figured it out and posted the resolution here:

Refreshing a button enabled/disabled by RelayCommand.CanExecute()

So here is a screen shot of LDPing.