none
Return type of DataRom[DataColumn] is {} RRS feed

  • Question

  • Here's a sample code:

    Code Snippet

    class SomeRow : DataRow
    {
      CustomDataTable _cTable; // this is set using setters in this class

      public int PropertyA {
        get {return (int) (this[_cTable.CColumn]); }
        set { this[_cTable.CColumn] = value; }
      }
    }



    whereas CustomDataTable is just a subclass of DataTable with a public property CColumn (of type DataColumn). The issue is that the statement in the getter throws an exception for not being able to cast to int. My debug shows that this[_cTable.CColumn]) returns {} which I don't understand why since I thought it'd be an integer value. Can someone explain what's happening here? This is not my code and I'm assigned to debug it.

    Thanks.

    P.S> I do realize that something might've gone wrong when the setter was invoked but I don't get it why {} is returned.
    Monday, May 12, 2008 7:37 PM

All replies

  • The DataRow indexer returns the type Object.  Verify that the column being accessed via the indexer is in fact stored as an int.  If not, you will get that exception.

     

    Monday, May 12, 2008 8:09 PM
  •  asadim wrote:
    Here's a sample code:

    Code Snippet

    class SomeRow : DataRow
    {
      CustomDataTable _cTable; // this is set using setters in this class

      public int PropertyA {
        get {return (int) (this[_cTable.CColumn]); }
        set { this[_cTable.CColumn] = value; }
      }
    }



    whereas CustomDataTable is just a subclass of DataTable with a public property SomeColumn. The issue is that the statement in the getter throws an exception for not being able to cast to int. My debug shows that this[_cTable.CColumn]) returns {} which I don't understand why since I thought it'd be an integer value. Can someone explain what's happening here? This is not my code and I'm assigned to debug it.

    Thanks.

    P.S> I do realize that something might've gone wrong when the setter was invoked but I don't get it why {} is returned.

    This begs the question.  What type is CColumn? 

     

    What is a subclass?  Is it a nested class?

     

    Rudedog

    Monday, May 12, 2008 8:09 PM
  • Oops in my description I used SomeColumn instead of CColumn. They're the same and are of type DataColumn.

    CustomDataTable is defined as a subclass of DataTable like follows: public class CustomDataTable : DataTable

    I also do know that the exception is thrown because this[_cTable.CColumn] does not return an int. But what does {} mean as its return value??!! Btw I have checked to make sure that CColumn in the indexer is stored as Int32.
    Monday, May 12, 2008 8:22 PM
  •  asadim wrote:
    Oops in my description I used SomeColumn instead of CColumn. They're the same and are of type DataColumn.

    CustomDataTable is defined as a subclass of DataTable like follows: public class CustomDataTable : DataTable

    I also do know that the exception is thrown because this[_cTable.CColumn] does not return an int. But what does {} mean as its return value??!! Btw I have checked to make sure that CColumn in the indexer is stored as Int32.

     

    Sounds like you are trying to cast type DataColumn to type int.  The indexer type is not the object type.  I think your return value is a null, BTW.  Are you getting that from the Local Window?

     

    Rudedog

    Monday, May 12, 2008 8:39 PM
  • I type this[_cTable.CColumn] in the Immediate Window and I get {}. Typing this[_cTable.CColumn].GetType() gives me an Int32 type.
    Monday, May 12, 2008 8:58 PM
  •  asadim wrote:
    I type this[_cTable.CColumn] in the Immediate Window and I get {}. Typing this[_cTable.CColumn].GetType() gives me an Int32 type.

    I thought you said above that CColumn was of type DataColumn, not int.  If CColumn were actually of type int, then why are you trying to cast it to type int?  And generating an exception?

     

    Rudedog

    Tuesday, May 13, 2008 12:59 AM
  • > I thought you said above that CColumn was of type DataColumn, not int.
    That's correct. As I mentioned,
    this[_cTable.CColumn] is of type int not CColumn itself. And yet this[_cTable.CColumn] returns a value of {} when debugged through the Immed Window. And I'm trying to cast it for the same reason; i.e. {} is not an int.

    To reiterate everything, I'm wondering why
    this[_cTable.CColumn] which is of type int returns {}?

    Thanks.
    Tuesday, May 13, 2008 6:08 PM
  •  asadim wrote:
    > I thought you said above that CColumn was of type DataColumn, not int.
    That's correct. As I mentioned,
    this[_cTable.CColumn] is of type int not CColumn itself. And yet this[_cTable.CColumn] returns a value of {} when debugged through the Immed Window. And I'm trying to cast it for the same reason; i.e. {} is not an int.

    To reiterate everything, I'm wondering why
    this[_cTable.CColumn] which is of type int returns {}?

    Thanks.

     

    I think if you look in the class that defines CColumn you might find your answer.  That return value is a null.  Int is a value type, which is normally not nullable.  You must specifically declare a value type as nullable, default behavior is that it cannot be set to null. 

     

    The fact that the value is apparently null very strongly suggests the type is something other than a value type like int.  Reference types are nullable, and DataColumn is most certainly a reference type. 

     

    Let's operate on the assumption that your mystery object is of type DataColumn.  What happens when you try to cast it to that type, DataColumn?  Any compile or runtime errors?

     

     

    Rudedog

     

    EDIT

     

    You could also do this, which compiles when the cast fails, but returns null when if it fails at runtime.

     

    int CastObject = _cTable.CColumn as Int32;

    Tuesday, May 13, 2008 6:58 PM