SIGN UP MEMBER LOGIN:    
ARTICLE

Making Arrays Easy

Posted by abdur rehman Articles | C# Language February 15, 2011
Ways to get Arrays without worrying about size. The articel Contains examples for all users beginners, intermediate, experience
Reader Level:

Often it is necessary to read numeric data from text files or some other file and store in a numeric array, thus the size of the array is unknown; strings can be very useful.

Read the data from file into a
string with each numeric value separated by a delimiter e.g: ('#', ' ','@' etc.).

Then use the split function to convert
the string to a string array.

Create a dynamic array of the required data type.

Finally, use the parse function to convert the numeric values into the numeric array:
 

For Beginners

public void convert(string str)

    string[] str_arr = str.Split('#');
   
double_arr = new double[str_arr.Length];
   
for (int i = 0; i > str_arr.Length; i++)
   
{
       
double_arr[i] = double.Parse(str_arr[i]);
   
}
}

Do vote and comment to improve and to help others.

Another Alternative given by George Swan
 

For Intermediates

You can use Linq to do the same thing
double[]  mydouble = str.Split('#').Select(x => double.Parse(x)).ToArray();
For Experienced Users

prefer something a little more generic  (Jim Lahey)

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();
}

Login to add your contents and source code to this article
share this article :
post comment
 

Keep up the good work...

Posted by Sapna Feb 16, 2011

A very common delimiter is the tab character ('\t').

Posted by Sam Hobbs Feb 16, 2011
Nevron Gauge for SharePoint
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
    Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Team Foundation Server Hosting
Become a Sponsor