Home All Groups Group Topic Archive Search About

Creating an array of datarows

Author
16 Sep 2006 12:58 AM
creator_bob
How do I create an array of datarows from a sorted list?

I put a bunch of datarows into a sorted list to sort them.  Then I got
an array of the sorted elements.  However, I cannot typecast them.  I
get an invalid cast error.  I notice that in the debugger, that arrays
of datarows are listed as {Length=xx}, but my arrays are listed as
{system.array} and refuse to typecast.

    Public Class mySortedList
        Inherits SortedList

        Public ReadOnly Property array() As Object
            Get
                Dim Result() As Object, X As Integer
                ReDim Result(Me.Count - 1)
                For X = 0 To Me.Count - 1
                    Result(X) = Me.GetByIndex(X)
                Next
                Return Result
            End Get
        End Property
    End Class


Dim O As Object, TestRows() As DataRow
O = EndRingsList.array  ' OK to here
TestRows = DirectCast(O, PressData.End_RingsRow()) ' Fails here

O is listed in the debugger as {system.array} instead of {Length=xx}

Author
16 Sep 2006 2:17 AM
creator_bob
It sounds like I need to understand arrays better.

getchildrows(...) returns DataRow() and I can typecast it to a specific
type of row array, but if I create my own function that returns
DataRow(), I can't type cast it.

Example:

Dim Test1 as DataRow(), Test2 as pressdata.end_ringsRow()

Test1=xx.GetChildRows("Relation")
Test2=directcast(Test1, pressdata.end_ringsRow())   ' This works.

Test1=myfunction()   ' MyFunction returns type DataRow()
Test2=directcast(Test1, Pressdata.end_ringsRow())   ' Gives an invalid
cast error.

I can get around this by creating a casting function for each type I
need.  I simply dimensions an array according to the number of elements
in the source array and copies them all over and returns the result.

How does the dataset class return DataRow() arrays that can be
typecasted and my own functions can't?
Author
17 Sep 2006 1:57 PM
Dennis
I would think that the DataSet (or DataTable) has schema information whereas
the Arraylist of datarows does not.  Why are you not work with DataTables if
you want to work with DataRows.  You can sort datatables easier.
--
Dennis in Houston


Show quoteHide quote
"creator_bob" wrote:

> It sounds like I need to understand arrays better.
>
> getchildrows(...) returns DataRow() and I can typecast it to a specific
> type of row array, but if I create my own function that returns
> DataRow(), I can't type cast it.
>
> Example:
>
> Dim Test1 as DataRow(), Test2 as pressdata.end_ringsRow()
>
> Test1=xx.GetChildRows("Relation")
> Test2=directcast(Test1, pressdata.end_ringsRow())   ' This works.
>
> Test1=myfunction()   ' MyFunction returns type DataRow()
> Test2=directcast(Test1, Pressdata.end_ringsRow())   ' Gives an invalid
> cast error.
>
> I can get around this by creating a casting function for each type I
> need.  I simply dimensions an array according to the number of elements
> in the source array and copies them all over and returns the result.
>
> How does the dataset class return DataRow() arrays that can be
> typecasted and my own functions can't?
>
>
Author
18 Sep 2006 5:25 PM
creator_bob
Dennis wrote:
Show quoteHide quote
> I would think that the DataSet (or DataTable) has schema information whereas
> the Arraylist of datarows does not.  Why are you not work with DataTables if
> you want to work with DataRows.  You can sort datatables easier.
> --
> Dennis in Houston
>
>
> "creator_bob" wrote:
>
> > It sounds like I need to understand arrays better.
> >
> > getchildrows(...) returns DataRow() and I can typecast it to a specific
> > type of row array, but if I create my own function that returns
> > DataRow(), I can't type cast it.
> >
> > Example:
> >
> > Dim Test1 as DataRow(), Test2 as pressdata.end_ringsRow()
> >
> > Test1=xx.GetChildRows("Relation")
> > Test2=directcast(Test1, pressdata.end_ringsRow())   ' This works.
> >
> > Test1=myfunction()   ' MyFunction returns type DataRow()
> > Test2=directcast(Test1, Pressdata.end_ringsRow())   ' Gives an invalid
> > cast error.
> >
> > I can get around this by creating a casting function for each type I
> > need.  I simply dimensions an array according to the number of elements
> > in the source array and copies them all over and returns the result.
> >
> > How does the dataset class return DataRow() arrays that can be
> > typecasted and my own functions can't?
> >
> >

My situation is I needed a sorted list of grandchildren, and my
grandchild table, even if it was sorted, wouldn't be sorted after
getting all child rows; then with each of those child rows, getting its
child row.

I found the solution was to use the arraylist object.  Then, when I am
ready to return, I use ToArray(Element0.GetType).  This returns an
object that can be typecast.

Working with arrays of datarows is quite customary, since a datarow can
only belong to one datatable.