Home All Groups Group Topic Archive Search About

Better way to go from ArrayList to Object()

Author
30 Jan 2006 8:37 PM
Chris
I have the following code where I want to return a type Hotel()

Function GetHotel as Hotel()

'...  Init Code here

         Dim arrH As New ArrayList
         Do While Reader.Read
             Dim H As New Hotel(Reader.GetString(1), Reader.GetInt32(0))
             arrH.Add(H)
         Loop
         Reader.Close()

         Dim Hs(arrH.Count - 1) As Hotel
         For ii As Integer = 0 To arrH.Count - 1
             Hs(ii) = DirectCast(arrH(ii), Hotel)
         Next
         Return Hs

End Function

Is this the best way to do this?  I guess I could use reDim Preserve but
thought that was an ugly way to do it too?  What's best way to do this?

Chris

Author
30 Jan 2006 8:44 PM
Armin Zingler
Show quote Hide quote
"Chris" <no@spam.com> schrieb im Newsbeitrag
news:OF89mzdJGHA.3224@TK2MSFTNGP09.phx.gbl...
>I have the following code where I want to return a type Hotel()
>
> Function GetHotel as Hotel()
>
> '...  Init Code here
>
>         Dim arrH As New ArrayList
>         Do While Reader.Read
>             Dim H As New Hotel(Reader.GetString(1), Reader.GetInt32(0))
>             arrH.Add(H)
>         Loop
>         Reader.Close()
>
>         Dim Hs(arrH.Count - 1) As Hotel
>         For ii As Integer = 0 To arrH.Count - 1
>             Hs(ii) = DirectCast(arrH(ii), Hotel)
>         Next
>         Return Hs
>
> End Function
>
> Is this the best way to do this?  I guess I could use reDim Preserve but
> thought that was an ugly way to do it too?  What's best way to do this?


hs = directcast(arrh.toarray(gettype(hotel)), hotel())


Armin
Author
30 Jan 2006 8:52 PM
Chris
Armin Zingler wrote:
Show quoteHide quote
> "Chris" <no@spam.com> schrieb im Newsbeitrag
> news:OF89mzdJGHA.3224@TK2MSFTNGP09.phx.gbl...
>
>> I have the following code where I want to return a type Hotel()
>>
>> Function GetHotel as Hotel()
>>
>> '...  Init Code here
>>
>>         Dim arrH As New ArrayList
>>         Do While Reader.Read
>>             Dim H As New Hotel(Reader.GetString(1), Reader.GetInt32(0))
>>             arrH.Add(H)
>>         Loop
>>         Reader.Close()
>>
>>         Dim Hs(arrH.Count - 1) As Hotel
>>         For ii As Integer = 0 To arrH.Count - 1
>>             Hs(ii) = DirectCast(arrH(ii), Hotel)
>>         Next
>>         Return Hs
>>
>> End Function
>>
>> Is this the best way to do this?  I guess I could use reDim Preserve
>> but thought that was an ugly way to do it too?  What's best way to do
>> this?
>
>
>
> hs = directcast(arrh.toarray(gettype(hotel)), hotel())
>
>
> Armin

Thanks, I just didn't pass in the gettype parameter... maybe if there
was some document somewhere that showed how to use the toarray function...

Oh wait...  There it is...

Thanks again
Chris
Author
31 Jan 2006 7:00 AM
CMM
It doesn't get more elegant than that! :-)
Author
30 Jan 2006 8:46 PM
Jason Newell
Chris,

I'm not sure about VB.NET but in C# we would use:

Hotel[] hotels = (Hotel[])arrH.ToArray(typeof(Hotel));

Keypoint here is using the .ToArray() method.

Jason


Chris wrote:
Show quoteHide quote
> I have the following code where I want to return a type Hotel()
>
> Function GetHotel as Hotel()
>
> '...  Init Code here
>
>         Dim arrH As New ArrayList
>         Do While Reader.Read
>             Dim H As New Hotel(Reader.GetString(1), Reader.GetInt32(0))
>             arrH.Add(H)
>         Loop
>         Reader.Close()
>
>         Dim Hs(arrH.Count - 1) As Hotel
>         For ii As Integer = 0 To arrH.Count - 1
>             Hs(ii) = DirectCast(arrH(ii), Hotel)
>         Next
>         Return Hs
>
> End Function
>
> Is this the best way to do this?  I guess I could use reDim Preserve but
> thought that was an ugly way to do it too?  What's best way to do this?
>
> Chris
Author
30 Jan 2006 9:36 PM
Herfried K. Wagner [MVP]
"Chris" <no@spam.com> schrieb:
>I have the following code where I want to return a type Hotel()
>
> Function GetHotel as Hotel()
>
> '...  Init Code here
>
>         Dim arrH As New ArrayList
>         Do While Reader.Read
>             Dim H As New Hotel(Reader.GetString(1), Reader.GetInt32(0))
>             arrH.Add(H)
>         Loop
>         Reader.Close()

\\\
Return DirectCast(arrH.ToArray(GetType(Hotel)), Hotel())
///

Show quoteHide quote
> End Function
>
> Is this the best way to do this?  I guess I could use reDim Preserve but
> thought that was an ugly way to do it too?  What's best way to do this?

--
M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://classicvb.org/petition/>