Home > .net 2.0, c#, Visual Studio 2008 > Converting String[] or CSV to Int[] – A Helper Method

Converting String[] or CSV to Int[] – A Helper Method

June 7, 2007

The simpliest things turn out to be really, really troublesome.

Unfortunately, you cannot explicitly cast arrays to a new reference type:

int[] intArray = (int[])stringArray;

I found several websites noting that you have to loop through the values one-by-one; however, that seemed a bit arcane with the Array.ConvertAll method.  I kept getting hung up on the fact that it wanted a Converter object passed to it.  Little did I realize, until I started browsing through what a convert WAS, that I already had it!

Note: This code is setup using a plain object.  Why?  So I can pass DataRow or most anything to it and allow it to do the conversion for me. 

public static int[] ConvertArrayStringtoInt(object convertible)

{

string convertedString = ConversionTools.ConvertString(convertible);

string[] stringArray;

stringArray = convertedString.Split(new char[] { ‘,’ });

return Array.ConvertAll<string, int>(stringArray, ConversionTools.ConvertInteger);

}

This uses two pre-existing helper methods that I had in our framework, ConvertString and ConvertInteger.  ConvertString is providing a simple conversion and assurance that culture is taken into consideration.  I prefer to use this method rather than .ToString() for everything to centralize how I handle culture and provide catching if .ToString() is not possible or fails.

ConvertInteger, on the other hand, provides our Converter delegate.  My first reaction was to use an inline generic delegate that looked a bit (okay, very) dirty:

int[] intArray = Array.ConvertAll<string, int>(

stringArray,

delegate(string s) { int i; if (!Int32.TryParse(s, out i)) { i = -1; } return i; });

*blink*… *blink*  Does it work?  Yeah, it seems to… but I realized, it’s simply converting an Integer, which is what my ConvertInteger method does for me…  and it’s a method, so using it keeps things simple (oh, and it works!).

/// <summary>

/// Converts the object to a String; if possible.

/// </summary>

/// <param name=”convertible”>The object.</param>

/// <returns>

/// The string conversion of the object, if possible.

/// An empty string if not possible.

/// </returns>

public static string ConvertString(object convertible)
{

try

{

return Convert.ToString(convertible, CultureInfo.CurrentCulture);

}

catch (NullReferenceException)

{

return “”;

}

catch (InvalidCastException)

{

return “”;

}

}

 

/// <summary>

/// Converts the object to an Int32; if possible.

/// </summary>

/// <param name=”convertible”>The object.</param>

/// <returns>The Int32 conversion of the object.</returns>

public static int ConvertInteger(object convertible)

{

try

{

return Convert.ToInt32(convertible, CultureInfo.CurrentCulture);

}

catch (NullReferenceException)

{

return 0;

}

}

 As far as using it, I added the helper method to the framework and recompiled.  It’s now available as:

int[] intArray = ConversionTools.ConvertArrayStringtoInt(stringArray);

 

%d bloggers like this: