473,396 Members | 1,968 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Better way to go from ArrayList to Object()

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
Jan 30 '06 #1
5 951
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:
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

Jan 30 '06 #2
"Chris" <no@spam.com> schrieb im Newsbeitrag
news:OF**************@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

Jan 30 '06 #3
Armin Zingler wrote:
"Chris" <no@spam.com> schrieb im Newsbeitrag
news:OF**************@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
Jan 30 '06 #4
"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())
///
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/>
Jan 30 '06 #5
CMM
It doesn't get more elegant than that! :-)
Jan 31 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Tamir Khason | last post by:
How can I set the type of the object added to ArrayList (type of Array List Members) Here is the code: protected ArrayList tabs = new ArrayList(); public ArrayList Tabs {
3
by: Stephen | last post by:
I was wondering if someone can help me with an web application design problem. I have a aspx page which builds up an arraylist called addresses and outputs the values in the arraylist items to a...
3
by: JJ | last post by:
Hi, I have created an Arraylist object from an Arraylist class. I added rows to the arraylist object and I need to find a particular record in my arraylist. How do I do this? Also if I was in...
3
by: Fred | last post by:
I'm trying to build a hashtable and a arraylist as object value I'm not able to retrieve stored object from the hashtable. Hashtable mp = new Hashtable(); // THE HASHTABLE ArrayList...
10
by: C Downey | last post by:
Hello: I have an arraylist storing some very basic objects. The object is very basic, it has 2 properties : ID, and COUNT Before I add an object to the arraylist, I want to check if an...
9
by: Scott Reynolds | last post by:
Hi, I am using following code to read and generate SQL query based on values stored in ArrayList. But I am not sure if it is the best way... all suggestions are welcome! Thank you! Scott
20
by: Dennis | last post by:
I use the following code for a strongly typed arraylist and it works great. However, I was wondering if this is the proper way to do it. I realize that if I want to implement sorting of the...
18
by: JohnR | last post by:
From reading the documentation, this should be a relatively easy thing. I have an arraylist of custom class instances which I want to search with an"indexof" where I'm passing an instance if the...
12
by: garyusenet | last post by:
I have had no replies to my previous post so perhaps I didn't write it good enough. Please excuse new thread but i wanted to break from the last thread hopefully this thread will be better. ...
6
by: fniles | last post by:
I am using VB.NET 2003 and a socket control to receive and sending data to clients. As I receive data in 1 thread, I put it into an arraylist, and then I remove the data from arraylist and send it...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.