views:

57

answers:

2

Is it possible to use indexers with extension methods.

eg. Consider it as an example only.

    public static object SelectedValue(this DataGridView dgv, string ColumnName)
    {            
        return dgv.SelectedRows[0].Cells[ColumnName].Value;
    }

EDIT

  1. usage mygrid.SelectedValue("mycol")

  2. How to use it as an indexer mygrid.SelectedValue["mycol"] rather than above one.

  3. Is it possible to use it like this as well ? mygrid.SelectedValue["mycol"](out somevalue);

What are the syntax of getting this kind of values. Any simple example or link will work.

+1  A: 

Well, there are two issues here:

  • C# doesn't (by and large) support named indexers1
  • C# doesn't support extension properties, so you can't make SelectedValue a property returning something indexable instead

So no, the syntax you've specified there won't work. You could get this to work:

mygrid.SelectedValue()["mycol"]

but that's a bit ugly. I'd stick with the method form if I were you.


1 C# 4 supports calling named indexers on COM objects.

Jon Skeet
@Jon: What kind of indexer i need to create here in that case. http://msdn.microsoft.com/en-us/library/6x16t2tx.aspx. I read this link to create indexer but how will it work within extension class. Is it possible to work out using this within static class as i need to create indexer also static
Shantanu Gupta
I too would prefer the method specified above in my ques until unless i have named indexer for which i have to start working on C# 4
Shantanu Gupta
Why only supporting named indexers on COM?
simendsjo
@Jon: How it is possible to use named indexers in 2K8 with datatables and datasets and many more. i.e. `(new DataTable).Rows[0]["mycol"]`. Doesn't .net opens this indexers to every one in previous versions itself
Shantanu Gupta
@Shantanu: That's using the `Rows` property, which returns a `DataRowCollection` which itself has an indexer. See my second bullet point - you can't create extension properties.
Jon Skeet
@simendsjo: I believe there was a desire to support it more widely, but the cost was seen to be greater than the value provided. The C# team only has limited resources.
Jon Skeet
@Jon: I read related article on msdn for C# 4. But still i would like to know named indexers are available to Collections in all previous version (<C#4) but are not exposed to programmers. Isn't it something strange that having some functionality defined long ago was not provided earlier or microsoft didn't recognised that
Shantanu Gupta
@Shantanu: Named indexers just aren't supported in C# - the idea is that it's generally better to write a property which returns an indexable collection. I'm not sure to what extent I agree with that, but that's the explanation usually given.
Jon Skeet
A: 

Let me try to clarify the usage and intentions of Extension Method.

Consider a Extension Method

public static string IsNullOrEmpty(this string source)
{
    return source == null || source == string.Empty;
}

Now you extend your string class with this Extension Method

var myString = "Hello World";
Assert.AreEqual(myString.IsNullOrEmpty(), false);

This is what .NET does on compilation:

public string IsNullOrEmpty(string source)
{
    return source == null || source == string.Empty;
}

Using our old school

var myString = "Hello World";
Assert.AreEqual(IsNullOrEmpty(myString), false);

Extension method is nothing but a visualization to what we were used to do.

Well, extending indexers could be possible but Microsoft did not think about it.

Munim Abdul