• Code
  • Tags
  • Users
  • Titles
  • Log in
  • Feedback
  • FAQ
Share Code
Welcome to ForkCan.com

ForkCan is all about sharing code in a social way.

Discuss, debate or argue with other devs about their or your own code.

Give other devs feedback or make a Fork (Make a better version of a shared code).

Rate the code, if you use the code mark it as used so others can see if the shared code is used by someone.

Help each other to be better devs and to be more productive.


Features not working yet:

Flag a post


QR Code

Tiny Url

http://4kcan.com/s/MzEy

TryGetValueOrDefault<T>() extension for string array

A generic TryGetValueOrDefault method to be used on string arrays -as read from e.g. csv-files that uses an enum as indexer

0
606 0 0 0 0 0

// Define an enum that assignes readable names to positions in an array
public enum DataFormatField
{
    Id = 0,
    GivenName = 1,
    Surname = 2,
    DateOfBirth = 3,
    UniqueId = 4,
}

class Program
{
    static void Main(string[] args)
    {
        var lineReadFromCsv = 
            "42;Daniel;Fisher;1980-03-25;{79704C0D-1A4F-4DDD-80F6-CA79E81BF7CD}";

        var fields = lineReadFromCsv.Split(';');

        int id;
        if (fields.TryGetValueOrDefault(
            DataFormatField.Id,
            out id))
        {
            Console.WriteLine(id);
        }

        string givenName;
        if (fields.TryGetValueOrDefault(
            DataFormatField.GivenName, 
            out givenName))
        {
            Console.WriteLine(givenName);
        }

        DateTime birthDay;
        if (fields.TryGetValueOrDefault(
            DataFormatField.DateOfBirth,
            out birthDay))
        {
            Console.WriteLine(birthDay);
        }

        Guid uniqueId;
        if (fields.TryGetValueOrDefault(
            DataFormatField.UniqueId,
            out uniqueId))
        {
            Console.WriteLine(uniqueId);
        }
        Console.ReadLine();
    }
}

public static class StringArrayExtensions
{
    #region TryGetValueOrDefault<T>()
    /// <summary>
    /// Tries the get value or default.
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="data">The data.</param>
    /// <param name="index">The index.</param>
    /// <param name="value">The value.</param>
    /// <returns></returns>
    public static bool TryGetValueOrDefault<T>(
        this string[] data,
        Enum index,
        out T value)
    {
        var valueToParse = data[(int)(object)index];

        if (string.IsNullOrEmpty(valueToParse))
        {
            value = default(T);
            return false;
        }

        var typeOfT = typeof(T).Name;

        if (typeOfT == "Decimal")
        {
            decimal decimalValue;
            if (decimal.TryParse(
                valueToParse,
                NumberStyles.Any,
                CultureInfo.CurrentCulture,
                out decimalValue))
            {
                value = (T)(object)decimalValue;
                return true;
            }
        }

        if (typeOfT == "Int32")
        {
            int intValue;
            if (int.TryParse(
                valueToParse,
                NumberStyles.Any,
                CultureInfo.CurrentCulture,
                out intValue))
            {
                value = (T)(object)intValue;
                return true;
            }
        }

        if (typeOfT == "Int64")
        {
            long longValue;
            if (long.TryParse(
                valueToParse,
                NumberStyles.Any,
                CultureInfo.CurrentCulture,
                out longValue))
            {
                value = (T)(object)longValue;
                return true;
            }
        }

        if (typeOfT == "Single")
        {
            float floatValue;
            if (float.TryParse(
                valueToParse,
                NumberStyles.Any,
                CultureInfo.CurrentCulture,
                out floatValue))
            {
                value = (T)(object)floatValue;
                return true;
            }
        }

        if (typeOfT == "Double")
        {
            double doubleValue;
            if (double.TryParse(
                valueToParse,
                NumberStyles.Any,
                CultureInfo.CurrentCulture,
                out doubleValue))
            {
                value = (T)(object)doubleValue;
                return true;
            }
        }

        if (typeOfT == "DateTime")
        {
            DateTime dateTimeValue;
            if (DateTime.TryParse(
                valueToParse,
                CultureInfo.CurrentCulture,
                DateTimeStyles.AssumeLocal,
                out dateTimeValue))
            {
                value = (T)(object)dateTimeValue;
                return true;
            }
        }

        if (typeOfT == "Guid")
        {
            Guid guidValue;
            if (Guid.TryParse(valueToParse, out guidValue))
            {
                value = (T)(object)new Guid(valueToParse);
                return true;
            }
        }

        if (typeOfT == "String")
        {
            value = (T)(object)valueToParse;
            return true;
        }

        value = default(T);
        return false;
    } 
    #endregion
}

Share: twitter | facebook   Action: used | fork | flag

c#

Mark 'c#' tag as 'like'

Mark 'c#' tag as 'ignore'


 @lennybacon
222
October 19, 2010 3:05 PM

Fork

 TryGetValueOrDefault<T>() extension for string array -  @lennybacon Tuesday 19, 2010 3:05 PM


0 Feedback


You must log in before you can give any feedback


0 Discussion(s)

Newest Oldest

You must log in before you can post a comment

Squeed
Made by: Fredrik Normén 2010