Click here to Skip to main content
12,403,728 members (68,841 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

1.7K views
2 bookmarked
Posted

Unknown size Numeric Arrays from Strings - dontumindit

, 14 Feb 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
I prefer something a little more generic:public static IEnumerable Convert(string value, char separator) where TTypeConverter : TypeConverter { var typeConverter = (TTypeConverter)TypeDescriptor.GetConverter(typeof(TValue)); ...
I prefer something a little more generic:

public static IEnumerable<TValue> Convert<TValue, TTypeConverter>(string value, char separator) where TTypeConverter : TypeConverter
       {
           var typeConverter = (TTypeConverter)TypeDescriptor.GetConverter(typeof(TValue));
          return new List<string>(value.Split(separator))
              .ConvertAll<TValue>(s => (TValue)typeConverter.ConvertFrom(s));
       }

That way I can convert a delimited string to a large number of IEnumerable<T> collections in just one line and I don't have to worry about the size of the collection either:

public static void UsageMethod()
        {
            var myIntegers = ((List<int>)Convert<int, TypeConverter>("1,2,3,4,5", ',')).ToArray();
        }

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

jim lahey
Software Developer (Senior)
United Kingdom United Kingdom
First batch file in 1987

Messed around with the Bullfrog C++ Libraries for Syndicate in 1994

Web Developer since 2000

.net Developer since 2001

MCTS: Microsoft® .NET Framework 2.0 - Web-based Client Development

MCTS: Web Applications Development with Microsoft .NET Framework 4

You may also be interested in...

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160721.1 | Last Updated 15 Feb 2011
Article Copyright 2011 by jim lahey
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid