Jump to content
Xtreme .Net Talk

Recommended Posts

Posted (edited)

[PLAIN]HashTable vs Jagged Array for an array of arrays[vb.net][/PLAIN]

 

I have a custom class which holds about 8 different variables of differing types. I need to create various Arrays of those classes... but i'd also like to sort all of those arrays for easier programming by enumerating those arrays.

Is Hashtable a low-system resource thing or what? I'd like to simply use a jagged array but the documentation says they're not CLS compliant.

 

I'm wondering what the best solution to this is, performance is probably an issue.

Is there a simple way to enumerate class arrays?

Edited by NeuralJack
Currently Using: Visual Basic.Net 2005, .Net Framework 2.0
Posted

Thanks PD , the generic class "List" seems to be perfect.

 

As you are using .Net 2 then you might want to investigate generics as an alternative to both arrays / HashTables.
Currently Using: Visual Basic.Net 2005, .Net Framework 2.0
Posted

Actually, I spoke too soon. I'm having the same problem figuring out how to declare and initialize an array of an array with List as I am with other types of arrays. Any help with the syntax would be appreciated.

Lets say I need an array of an array of this class:

Public Class AnyClass
Public int1 As Integer = 0
Public int2 As Integer = 1
Public str1 As String = String.Empty
Public str2 As String = String.Empty
End Class

 

This has evolved mainly into a syntax question , i think. Eventually I want to be able to access one of the entries like:

Integer2 = ArrayOfArrays(1)(1).int2

 

Lets say I want to make 2 different Array of Arrays We'll name them ArrayOfArraysOne And ArrayOfArraysTwo. In ArrayOfArraysOne I want 5 different Arrays of the class AnyClass. Lets say the subarrays in that array need 5, 3, 3, 4, 5 elements in each of those subarrays.

 

In ArrayOfArraysTwo I want 10 different Arrays of the class AnyClass.Lets say the subarrays in that array need 1, 2, 3, 4, 5 elements in each of those subarrays.

 

I'd, ideally, like to use the List object from System.Collections.Generic.. which doesnt look all that different to implement than other arrays.

Currently Using: Visual Basic.Net 2005, .Net Framework 2.0
Posted

Ok, i've figured this out after lots o trial and error. To create an array of arrays with the generic List obect do this. The array will be an array of arrays of the class 'AnyClass'.

 

 

 

'The Class:
Public Class AnyClass
Public int1 As Integer = 0
Public int2 As Integer = 1
Public str1 As String = String.Empty
Public str2 As String = String.Empty
End Class 



'To Declare:
Public ArrayOfArrays As List(Of List(Of AnyClass)) = New List(Of List(Of AnyClass))(2)  

'To Initialize:
For i As Integer = 0 To 1  'Add 2 Subarrays to ArrayOfArrays
ArrayOfArrays.Add(New List(Of AnyClass))
Next
       	    
For j As Integer = 0 To 4   'Add 5 elements to the first subarray of ArrayOfArrays
ArrayOfArrays(0).Add(New AnyClass)
Next

For j As Integer = 0 To 9   'Add 10 elements to the second subarray of ArrayOfArrays
ArrayOfArrays(1).Add(New AnyClass)
Next

'To Use:
ArrayOfArrays(0)(0).str1 = "Hello World"
Debug.WriteLine(ArrayOfArrays(0)(0).str1)


Currently Using: Visual Basic.Net 2005, .Net Framework 2.0

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...