|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Better way to go from ArrayList to Object()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
Show quote
Hide quote
"Chris" <no@spam.com> schrieb im Newsbeitrag hs = directcast(arrh.toarray(gettype(hotel)), hotel())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? Armin Armin Zingler wrote:
Show quoteHide quote > "Chris" <no@spam.com> schrieb im Newsbeitrag Thanks, I just didn't pass in the gettype parameter... maybe if there > 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 was some document somewhere that showed how to use the toarray function... Oh wait... There it is... Thanks again Chris 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 "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/>
IsDate("ISomeTimesHateProgrammingMarch2005") = True
Query Builder Cancel a thread - please help Dataset requery Convert from C# to VB.Net Simple (I hope) SteamReader DirectoryInfo question Assigning ListBox Current Selection to TextBox FileSystemObject filter? File Info Question extracting part of a graphic in a PictureBox to the clipboard |
|||||||||||||||||||||||