Tags: downside, errorprovider, idataerrorinfo, incombination, interface, net, sharp

IDataErrorInfo Interface

On .Net » .Net C# (C sharp)

14,651 words with 4 Comments; publish: Wed, 02 Jan 2008 22:32:00 GMT; (100101.56, « »)

I really want to use the IDataErrorInfo interface. I'm using it now in

combination with an ErrorProvider and this seems really good.

But the downside: IDataErrorInfo wants to use a string indexer on my

class. No big deal, but now I have a class where I want to use my own

string indexer. This is frustrating.

Do I have to do something funky like add an extra argument to my

indexer? Is there a better way?

Help...

--Brian

Code: ( text )
  1. class MyClass
  2. {
  3. //Here is the indexer for IDataErrorInfo:
  4. public string this[string name]
  5. {
  6. get { ... }
  7. }
  8. //Here is my indexer:
  9. public string this[string name, bool x]
  10. {
  11. get { ... }
  12. }
  13. }

All Comments

Leave a comment...

  • 4 Comments
    • Brian <no.net-csharp.todaysummary.com.email.com> wrote:

      > I really want to use the IDataErrorInfo interface. I'm using it now in

      > combination with an ErrorProvider and this seems really good.

      > But the downside: IDataErrorInfo wants to use a string indexer on my

      > class. No big deal, but now I have a class where I want to use my own

      > string indexer. This is frustrating.

      > Do I have to do something funky like add an extra argument to my

      > indexer? Is there a better way?

      Yes. Use explicit interface implementation.

      > class MyClass

      class MyClass : IDataErrorInfo

      > {

      > //Here is the indexer for IDataErrorInfo:

      > public string this[string name]

      string IDataErrorInfo.this[string name]

      > {

      > get { ... }

      > }

      > //Here is my indexer:

      > public string this[string name, bool x]

      public string this[string name]

      > {

      > get { ... }

      > }

      > }

      -- Barry

      --

      http://barrkel.blogspot.com/

      #1; Wed, 02 Jan 2008 22:33:00 GMT
    • Brian wrote:

      > I really want to use the IDataErrorInfo interface. I'm using it now in

      > combination with an ErrorProvider and this seems really good.

      > But the downside: IDataErrorInfo wants to use a string indexer on my

      > class. No big deal, but now I have a class where I want to use my own

      > string indexer. This is frustrating.

      > Do I have to do something funky like add an extra argument to my

      > indexer? Is there a better way?

      > Help...

      > --Brian

      >

      Code: ( text )
      1. >
      2. >
      3. > class MyClass
      4. > {
      5. > //Here is the indexer for IDataErrorInfo:
      6. > public string this[string name]
      7. > {
      8. > get { ... }
      9. > }
      10. >
      11. > //Here is my indexer:
      12. > public string this[string name, bool x]
      13. > {
      14. > get { ... }
      15. > }
      16. > }
      17. >
      18. >

      Hi Brian, just explicitly implement the IDataErrorInfo interface:

      class MyClass

      {

      public IDataErrorInfo.this[string columnName]

      {

      get { ... }

      }

      public string this[string name]

      {

      get { ... }

      }

      }

      This does however introduce some implications when you want to access the

      IDataErrorInfo implementation of the indexer, that is, the instance of

      MyClass will need to be explicitly cast to IDataErrorInfo in order to

      access the implementation of the indexer:

      ((IDataErrorInfo)MyClassInstance)["foobar"]

      Hope this helps,

      -- Tom Spink

#2; Wed, 02 Jan 2008 22:34:00 GMT
    • Brian wrote:

      > I really want to use the IDataErrorInfo interface. I'm using it now

      > in combination with an ErrorProvider and this seems really good.

      > But the downside: IDataErrorInfo wants to use a string indexer on my

      > class. No big deal, but now I have a class where I want to use my

      > own string indexer. This is frustrating.

      > Do I have to do something funky like add an extra argument to my

      > indexer? Is there a better way?

      The IDataErrorInfo interface is used for runtime databinding info

      reporting, it's not used for error info reporting for your own usage.

      This means that the bound controls know what to pass to IDataErrorInfo

      to retrieve an error message. If you want to use the IDataErrorInfo

      interface for something else, you're using it for a different purpose

      and then it might well be that it pretty lacks a lot. So for those

      purposes, implement your own interface.

      FB

      --

      ----------------------

      Lead developer of LLBLGen Pro, the productive O/R mapper for .NET

      LLBLGen Pro website: http://www.llblgen.com

      My .NET blog: http://weblogs.asp.net/fbouma

      Microsoft MVP (C#)

      ----------------------

      #3; Wed, 02 Jan 2008 22:35:00 GMT
    • Tom Spink wrote:

      > Brian wrote:

      >> I really want to use the IDataErrorInfo interface. I'm using it now in

      >> combination with an ErrorProvider and this seems really good.

      >>

      >> But the downside: IDataErrorInfo wants to use a string indexer on my

      >> class. No big deal, but now I have a class where I want to use my own

      >> string indexer. This is frustrating.

      >>

      >> Do I have to do something funky like add an extra argument to my

      >> indexer? Is there a better way?

      >>

      >> Help...

      >>

      >> --Brian

      >>

      >>

      Code: ( text )
      1. >>
      2. >>
      3. >> class MyClass
      4. >> {
      5. >> //Here is the indexer for IDataErrorInfo:
      6. >> public string this[string name]
      7. >> {
      8. >> get { ... }
      9. >> }
      10. >>
      11. >> //Here is my indexer:
      12. >> public string this[string name, bool x]
      13. >> {
      14. >> get { ... }
      15. >> }
      16. >> }
      17. >>
      18. >>

      > Hi Brian, just explicitly implement the IDataErrorInfo interface:

      > class MyClass

      > {

      > public IDataErrorInfo.this[string columnName]

      > {

      > get { ... }

      > }

      > public string this[string name]

      > {

      > get { ... }

      > }

      > }

      > This does however introduce some implications when you want to access the

      > IDataErrorInfo implementation of the indexer, that is, the instance of

      > MyClass will need to be explicitly cast to IDataErrorInfo in order to

      > access the implementation of the indexer:

      > ((IDataErrorInfo)MyClassInstance)["foobar"]

      > Hope this helps,

      > -- Tom Spink

      That is AWESOME!!!

      Oh this is too cool. Thank you guys so much!

      --Brian

  • #4; Wed, 02 Jan 2008 22:36:00 GMT