Array of variable # of arrays containing variable # of elts
Quick question !
How would you build an array of variable number of arrays containing each a variable number of strings.
For example, to build an array of x courses containing a variable number of students, I could do something like :
Option Base 1
Dim arrCourses() As Variant
'and later, when nNbCourses would be known
ReDim arrCourses(nNbCourses)
'then, I could def the nNbCourses arrays with students lists and a course name as first elt :
arrCourses(1) = Array("MATH", "Paul Smith", "Arthur Dupond", "Isabelle Dupres")
arrCourse(2) = Array("HIST", "Hector Biltran", "Isabelle Dupres", "Henry Clark", "Theo Done")
...
But what if I have to add every students one by one, since I can't "ReDim-like" my arrCourse, then define every element in a for/next loop.
Any idea (in VB only, of course) and point of vue welcome
Thanks in advance
Cordially
Re: How about collections...
Hi,
Seven o'clock here, then my brain is not fully open to the world... But... ;-)
Quote:
Originally posted by Cimperiali
...holding classes?
the collection could be the equivalent of array holding
variable number of instances of a class
The instances of the class could contain strings and arrays of strings...
Hummm... Maybe a nice solution ! If I was in C++, I would use something derived from the STL classes, but does that possible in VB ? Well, honnestly, I've never taken the classes-oriented way in VB. Then, could you provide me with a concret sample to avoid misunderstanding on my side ?
Quote:
Originally posted by Heulsay
i'd go with something like that :
Code:
Type TStudent
Name as string
End Type
Type TCourse
CourseName as string
aStudents() as TStudent
End Type
Private aCourses() as TCourse
The structure way... Well, not so far than the Cimperiali's classes way... And if we consider I don't really need any methods to manipulate my elements, it could be shorter ; saying this without any knowledge about how VB handle classes.
Well, in fact two ways possible there. Now, the only things to do - unless oversight - is to ta take the best one in term of working (size - maybe the struct way - and speed) :-)
Thanks, Cimperiali and Heulsay.
Dictionary looks like Hashtable
I noticed that Dictionary looks exactly like Vb.net's HashTable. And it performs search only based on key-strings, not some part of a text.
Code:
Dim D As New Dictionary
D.Add "CimP", "Cimperiali"
D.Add "dtV", "Dtv new instance!"
D.Exists("cimp") returns false
D.Exists("Cim") returns false
D.Exists( "CimP") returns True, so it needs an absolute key.
My need is to search for a part of string, the first letters of a name for example.
I think I cant avoid using a For Each .. Loop. Another practice is to sort the items, but this works also slow for about 10,000 records. I wonder how a database engine finds so fast information, even if the table is a mess (no sort at all, or is it some internal index-sort).
dictionary.CompareMode =TextCompare
Have a look at compareMode property of your dictionary instance...
from msdn:
CompareMode Property
Description
Sets and returns the comparison mode for comparing string keys in a Dictionary object.
Syntax
object.CompareMode[ = compare]
The CompareMode property has the following parts:
Part Description
object Required. Always the name of a Dictionary object.
compare Optional. If provided, compare is a value representing the comparison mode used by functions such as StrComp.
Settings
The compare argument can have the following values:
Constant Value Description
vbUseCompareOption –1 Performs a comparison using the setting of the Option Compare statement.
vbBinaryCompare 0 Performs a binary comparison.
vbTextCompare 1 Performs a textual comparison.
vbDatabaseCompare 2 Microsoft Access only. Performs a comparison based on information in your database.
Remarks
An error occurs if you try to change the comparison mode of a Dictionary object that already contains data.
The CompareMode property uses the same values as the compare argument for the StrComp function. Values greater than 2 can be used to refer to comparisons using specific Locale IDs (LCID).
yes, but I am not looking for a KEY
The comparemode is nice, but I am not searching an exactly KEY in the collection.
I am searching for the real text of the Dictionary/Collection.
The Dictionary is great for unique Keys.
For example lets say we have a Collection of fixed length records, including as their fields: name, email, address, phone number etc. The collection has about 10,000 records of this type. How to search in one step the structure, instead of using For Each.. Loop. If one name in some index is "John doe", then by giving "john" to that search function i want the entire record to return (or just the index number of the collection).
The Dictionary.Exists works only for unique keys, as MSDN says. So with it we must give the entire "John Doe" for the function to return true.