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
Thanks rjm, but unfortunately it's not enough since, as stated in my thread subject, both the number of arrays and the number of elements in each array are unpredictable.
Then, of course, I'll use the Redim statement to resize the arrCourses (array of variant-arrays) or your sArray (array of arrays strings) ; and the Preserve option is not really necessary in my case since arrCourses is declared empty in a first pass, then Redim one time only later.
But the problem is more on the next level, the arrCourses(n) or sArray(n) ones, which will have, as shown in my first example with courses and students, different number of elements each... And there I have to add one student per one student.
...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...
...at present time, using mainly Net 4.0, Vs 2010
Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
all the other wonderful people who made and make Codeguru a great place.
Come back soon, you Gurus.
Seven o'clock here, then my brain is not fully open to the world... But... ;-)
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 ?
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) :-)
This is one of ways you could do it.
I used Collection, Dictionary (=reference to Microsft Scripting Runtime) and a class.
It is a simple example to give you an idea on how to. You will have to adjust it...
...at present time, using mainly Net 4.0, Vs 2010
Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
all the other wonderful people who made and make Codeguru a great place.
Come back soon, you Gurus.
Is it possible to speed up a string search in a collection? VB7 offers a class "HashTable" where we can add to a collection object and search via a primary key, thing that makes the record search really instant. But using the great VB6, can we avoid using For Each .. Loop for searching an incomplete string? (like a part of a name). I dont want to use ADO for data manipulation in the specific app I am making, I like collections a lot...
If you can hold an array of keys, you could search via keys
Or you could use dictionary, which allow you to search for a key and if it exists.
Your keys could be the equivalent of the has table....
Dictionary allows also you to get items like array of values
Then you could search through the arrya result really fast if you ordered it.
Dictionaries are File scripting Runtime objects
...at present time, using mainly Net 4.0, Vs 2010
Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
all the other wonderful people who made and make Codeguru a great place.
Come back soon, you Gurus.
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).
Last edited by dtv; October 20th, 2003 at 10:11 AM.
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).
Last edited by Cimperiali; October 20th, 2003 at 10:51 AM.
...at present time, using mainly Net 4.0, Vs 2010
Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
all the other wonderful people who made and make Codeguru a great place.
Come back soon, you Gurus.
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.
I agree dtv.
Dictionary is not a solution in this task.
...at present time, using mainly Net 4.0, Vs 2010
Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
all the other wonderful people who made and make Codeguru a great place.
Come back soon, you Gurus.
Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
all the other wonderful people who made and make Codeguru a great place.
Come back soon, you Gurus.
* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.