Click here to Skip to main content
15,908,254 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This is my code that does not work, but it demonstrates what I need to do:
create about 1000 stringbuilders who's name is defined by a string of two characters (names defined by the records in file "C:\Temp\NGDDIVlist.txt").

Record = Split(File.ReadAllText("C:\Temp\NGDDIVlist.txt"), vbCrLf)
'Record holds the names for the stringbuilders
        For i = 0 To Record.length-1
            Dim Record(i) As StringBuilder
        Next


It is not possible to declare a variable where the name of this variable is a "string"... How can I overcome this issue?

More explanation:
Well, the question really is:
On a daily basis a textfile is generated with about a million strings (C:\Temp\NGDDIVlist.txt). Each string is in fact a record with two fields. The project needs to search this textfile for matches on field 0 of each record and has to do that for more or less 400.000 times. For the time being this is done using Instr, but I am looking for faster ways to do this. If I can split up the initial textfile in less longer subfiles, the Instr will take less time. Therefore these subfiles need to be named with the 'key' used for splitting. For this key I want to use the last two characters of field 0 of each record in the initial textfile. To build those subfiles fast, I want to use stringbuilders and since the 'key' = 2 characters, there are 36 x 36 = cases...

What I have tried:

see the code
searched the internet for solutions but didn't find any
Posted
Updated 9-Jul-19 9:12am
v2
Comments
Maciej Los 8-Jul-19 14:10pm    
Why do you need to create over 1000 stringbuilders? This sounds very strange...
MadMyche 8-Jul-19 15:02pm    
Is this just VB (as in VB6) or is this VB.Net?

Record is already defined: it's an array of strings, because that is what Split returns.
You could create an array of StringBuilder under a different name:
VB
Dim builders As StringBuilder() = New StringBuilder(Record.Length - 1) {}

For i As Integer = 0 To Records.Length - 1
    builders(i) = New StringBuilder(Records(i))
Next
But ... unless you propose to alter them, it's a pointless and memory consuming thing to do!

You can't define variables at runtime using a string as the name: you could use a Dictionary to do something similar:
VB
    Dim builders As Dictionary(Of String, StringBuilder) = New Dictionary(Of String, StringBuilder)()

    For i As Integer = 0 To Records.Length - 1
        builders(Record(i)) = New StringBuilder()
    Next
End Sub
And access each StringBuilder separately via builders
VB
builders("AB").Append("Some text")
Console.WriteLine(builders("XY").ToString())
But to be honest, I think you are overthinking this and would be better off taking a step back and asking yourself what you are doing that you think this is a good idea. This doesn't seem like a good solution to any real world problem to me, and then normally means there have been some fundamental poor decisions made earlier on in the project design.
 
Share this answer
 
Comments
CDRxxx 8-Jul-19 14:47pm    
Well, the question really is:
On a daily basis a textfile is generated with about a million strings (C:\Temp\NGDDIVlist.txt). Each string is in fact a record with two fields. The project needs to search this textfile for matches on field 0 of each record and has to do that for more or less 400.000 times. For the time being this is done using Instr, but I am looking for faster ways to do this. If I can split up the initial textfile in less longer subfiles, the Instr will take less time. Therefore these subfiles need to be named with the 'key' used for splitting. For this key I want to use the last two characters of field 0 of each record in the initial textfile. To build those subfiles fast, I want to use stringbuilders and since the 'key' = 2 characters, there are 36 x 36 = cases...
OriginalGriff 8-Jul-19 15:07pm    
First off, splitting will not be faster: there is some serious work done behind the scenes to allocate a string on the heap for each separate line and / or "key".
Have a look here:
https://www.codeproject.com/Tips/312312/Counting-Lines-in-a-String
Which shows the difference between several ways of just counting lines - which is surprisingly close to what you are doing when you think about it.
Split is very, very much slower than a solution based on Index of purely because the memory construction overhead.
Patrice T 9-Jul-19 15:39pm    
Hi OG, have a look at S3, it is a message to us.
Quote:
Vb: how to declare about 1000 stringbuilders by code

You try things to get a working solution, this is a good state of mind, but in your case, your 'Solution' smells like a wrong approach to your problem.

From the information you gave, I would look at dictionary and databases.

Using Instr is very simple minded, but is not efficient on long strings, breaking the file in 1200 part will improve the runtime, but each part is still about 1000 records and Instr can match in second field, which you have to handle.
With dictionary or database (with index) a search on full file is done in about scanning 20 records and only on first field.
The 20 comes from the fact that both dictionary and database are using an ordered list of records and a search is done by dichotomy, 1000000 records is about 2^20, so 20 keys to check for a search.

VBA Dictionary - Using the VBA Dictionary. Key Value pairs[^]

Advice: learn about structures and objects available, it can only help you to choose wise.
 
Share this answer
 
Thanks to "OriginalGriff" & "Patrice T".
Both responses are helpfull!

I did, for the time beeing, adapt the procedure by splitting up the file 'to check' in 1296 (36x36) subfiles. The splitting up takes 10 Sec, but the searches later on directly in the right subfile speeds up the overall proces significantly!

On the other hand, I need to study the ways I can handle in VB textfiles as indexed dB files... I'm looking now into dictionary ...
 
Share this answer
 
Comments
Maciej Los 9-Jul-19 15:31pm    
This is not answer. PLease, delete it to avoid down-voting. Such of content should be posted as a comment to the solution.
Patrice T 9-Jul-19 15:42pm    
Hi,
To discuss with author of a solution, use the button 'Have a question or comment' at bottom of Solution;
Advantage, author get notified.
To answer a comment, use the button 'Reply' on right of author name.
Advice: avoid using a solution for anything else than a solution.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900