EtanSivad
EtanSivad

Reputation: 511

How can I apply an extension method to a datarow column?

I have a datarow filled with ints stored as strings. I can parse each one like so: Convert.ToInt32(row[1].ToString())

It works, but is a bit unsightly. I got the idea that maybe I could use an extension class to try and make a .ToInt method. Simplifies things a bit. After some research, I wrote this up:

static class DataRowHelper
{
    public static int ToInt(this DataRow row)
    {
          return Convert.ToInt32( row.ToString());   
    }
}

This almost gets me there, but the problem is that it attaches the new method to the row like this: row.ToInt() instead of of attaching it like this: row[1].ToInt()

What am I missing here?

Upvotes: 2

Views: 2518

Answers (5)

Grant Winney
Grant Winney

Reputation: 66489

You don't have to apply your own extension method to achieve what you're describing in your question, because the MS team already wrote one for you (terrybozzio hinted at it in a comment).

Instead of using syntax like this:

var id = Convert.ToInt32(row[1]);

var id = Convert.ToInt32(row["id"]);

There's a strongly-typed Field extension method that allows you to specify the data type:

var id = row.Field<int>(1);

var id = row.Field<int>("id");

I can't imagine you'll get much shorter or clearer than that by writing your own extension method.

Upvotes: 2

Codeman
Codeman

Reputation: 12375

Even better if you want to handle generics and DbNull:

static class DataRowHelper
{
    public static int ToInt(this object item)
    {
        return Convert.ToInt32(item == null ? 0 : item);
    }
}

This is not very exception-safe, and will throw on quite a few conditions. You might want to add some checking here, or implement a TryToInt() type method.

Upvotes: 0

Daniel May
Daniel May

Reputation: 8236

I feel the other answers are offering solutions, but not answers.

The reason your extension method isn't working is because the return type of the indexer of your DataRow - row[1] isn't a DataRow, it's a string.

You should enhance your extension method to allow for this indexer:

public static class DataRowExtensions
{
    public static int ToInt(this DataRow row, int index)
    {
          return Convert.ToInt32(row[index].ToString());   
    }
}

Upvotes: 4

D Stanley
D Stanley

Reputation: 152624

You could just add the column number as a parameter:

int i1 = row.ToInt(1);
int i2 = row.ToInt(2);

etc.

in which case the extension method would be (error handling not included):

public static int ToInt(this DataRow row, int column)
{
      return Convert.ToInt32(row[column].ToString());   
}

Upvotes: 1

entropic
entropic

Reputation: 1683

Your extension method should inherit a string, not a DataRow. It should look like this (keep in mind this is very simple... and you should probably include error handling of some sort):

static class DataRowHelper
{
    public static int ToInt(this string item)
    {
          return Convert.ToInt32(item);   
    }
}

Upvotes: -2

Related Questions