LevendFW

Last year Levend started the open source project LevendFW under my techincal lead. This is a framework that offers a web interface for asp.net application development. Free for all to download, use and extend.

We are still building the the major features, but there is code submitted already. Soon we will publish the first modules for this framework.

We are looking for developers and designers to contribute to the project. Please get intouch when you like join the project.

Nerd nor trendy, but having good ideas? Don't hesitate and tell us! 

project details

Resumé

I got my bachelor's degree in engineering in 2003. After working as a project engineer at a company that sells electrotechnical systems I started as a freelance software developer.

As a freelancer I worked at small and bigger (international) companies.

In 2009 I started together with Jeroen Rozeboom "Levend", a website and software company.

My LinkedIn Profile

 

Jan 1

Written by: Willem de Jong
1/1/2010 10:10 PM 

For a project I am currently working on I needed to convert a tab separated database file into a Datatable. The database file is a plain text file and the fields are tab separated, every record is a new line.

Just extend the string class, I thought, and add a function ToDataTable() which converts the string to a corresponding datatable. But then I found out that Microsoft decided to make the string class sealed. This means that you are not allowed to extend it.

But what if you really want to extend it? Build a wrapper? That is a lot of work and not very maintainable. Then I found out you can actually extend a sealed class by putting “this” as a parameter.

Public static DataTable ToDataTable(this string s)

If you put a function like this somewhere in a (static) class you can use it like this:

String TestString = “Test”;
DataTable Dt = TestString.ToDataTable();

With this you can, after reading out the database file, convert the text very easily to a DataTable en bind it to a Gridview.

Click on read more for the complete code to convert the tab separated string to a DataTable.

using System.Data;
using System.IO;
using System.Text.RegularExpressions;
 
namespace LevendFW
{
      public static class Extensions
      {
            public static DataTable ToTable(this string s)
            {
                  DataTable dt = new DataTable();
 
                  string[] sRows = Regex.Split(s, "\r");
 
                  //eerste rij is columnname
                  string[] sColumns = Regex.Split(sRows[0], "\t");
 
                  foreach (string sColumn in sColumns)
                  {
                        DataColumn dc = new DataColumn(sColumn);
                        dt.Columns.Add(dc);
                  }
 
                  int rowcount = sRows.Length;
 
                  for (int i = 1; i < rowcount; i++)
                  {
                        DataRow dr = dt.NewRow();
                        string[] sCells = Regex.Split(sRows[i], "\t");
 
                        for (int y = 0; y < sCells.Length; y++)
                        {
                             dr[y] = sCells[y];
                        }
 
                        dt.Rows.Add(dr);
                  }
 
                  return dt;
            }
     }
}

Tags:

1 comment(s) so far...

Good Article***

:)

By Natalia García on   1/3/2010 8:15 PM

Your name:
Your website:
Comment:
Security Code
Enter the code shown above in the box below
Add Comment   Cancel 

Favorites