Solved

vb.net collection - data type question - newbie

Posted on 2006-07-10
3
309 Views
Last Modified: 2013-12-03
I am converting a vb6 application.

In it I use a collection of classes with the index being the number, in order that the class was added.

like

mycollection.add myclass, "1"

so I can loop through

for i = 0 to mycollection.count
   myclass.item(cstr(i))
next

Is there a better data type to use?

I don't remember why I used them in the first place, it could have been because there was no hash type.
0
Comment
Question by:jackjohnson44
3 Comments
 
LVL 35

Accepted Solution

by:
YZlat earned 300 total points
ID: 17074779
for VB.NET use Hashtable
0
 
LVL 8

Assisted Solution

by:Shakti109
Shakti109 earned 100 total points
ID: 17074826

There are a few limitations of using a "standard" collection, one of them is resizing (you have to manage the overall size yourself, etc).

You CAN use a hashtable, but there is no guarantee that you will always 100% get the itmes back out in the order you put them in (due to the hashing algorithm that makes hashtables so fast)  and the microsoft documentation is full of warnings to use your own algorithm to override the GetHashCode method, which most people (including myself quite often) ignore.

An alternative is to use an arraylist. The arraylist handles resizing itself, and you are guaranteed to get the items back out in the exact order they went in. The arraylist also has native methods for removing (removing at an index, and others), adding ranges, and a host of other native functions that standard collections don't.

0
 
LVL 24

Assisted Solution

by:Jeff Certain
Jeff Certain earned 100 total points
ID: 17075152
If you're using VB2005, forget about the System.Collections namespace and move to Systems.Collections.Generic. Generics are strongly typed, so they provide a significant performance improvement (especially with value types, such as Integer) as well as reducing the number of run-time errors that occur, since those errors are now design-time errors.

Dim myCollection as List(of myClass)

For Each m as myClass in myCollection
' Do something with myClass
Next
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Allow user to hide and show datagridview columns 4 29
Error handling in asp.net site 5 29
VB.net and sql server 4 36
Complex SQL statement in VB.NET 7 16
Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

832 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question