tl;dr
Skip to PROBLEM below.
CONTEXT
In my algebraic math program a formula can be entered in the following forms (as lines of VB6 code). (Spaces within strings will be ignored):
formula1$ = "SUM (a,b,c)"
formula2$ = "SUM (x,y,z)"
formula3$ = "PRODUCT (a,x)"
formula4$ = "PRODUCT ((SUM (a,b,c)) , (SUM (x,y,z))) "
formula5$ = "PRODUCT (" + formula1$ +") , (" + formula2$ +")"
(a bit fiddly for the user, I know, but better on balance when dealing with long formulae).
The content of formula5$
is then identical to the content of formula4$
.
It is useful to have a variable formula5_descr$
which when inspected/printed describes the specification of formula5$
thus:
debug.print formula5_descr$ ' --> "PRODUCT (" + formula1$ +") , (" + formula1$ +")"
The code for formula5_descr$
can be produced manually. However it is difficult to do and the user cannot be expected to do it.
I want to let the user enter a simpler form of code like:
formula5$ = "PRODUCT (formula1$ ) , ( formula2$ )"
Then the program could be modified to parse the formula string "PRODUCT (formula1$ ) , ( formula2$ )"
and replace any string references (i.e. formula1$
and formula2$
) by the contents of the relevant strings (i.e. SUM (a,b,c)
and SUM (x,y,z)
).
PROBLEM
And here is the problem - how in VB6 to replace the name of a string variable inside a string by the contents of that string variable?
(User Joel Coehoorn has pointed out that a process for this called string interpolation is available in newer languages, like VB14+, C#, javascript.)
Ideally there would be a function REVEAL()
such that:
REVEAL("formula1$") --> "SUM (a,b,c)"
NOTE The fact that a solution exists for VBA in MS Access does not mean that the solution automatically works for VB6. They are similar but distinct languages and an installation of VB6 will not necessarily have the same components installed as for MS Access.
Using a dictionary to store your formulas by key. After that just replace the formula names by the value of them.
Option Explicit
Dim dictFormulas As New Dictionary
Private Sub Command1_Click()
ReadLines Text1.Text
PrintFormulas
End Sub
Private Sub ReadLines(strText As Variant)
Dim i As Long
Dim strLines() As String
strLines = Split(strText, vbNewLine)
For i = LBound(strLines) To UBound(strLines)
AddFormula strLines(i)
Next
End Sub
Private Sub AddFormula(strLine As String)
Dim strFormulaName As String
Dim strFormulaValue As String
Dim strItem As Variant
Dim strFormulaVect() As String
strFormulaVect = Split(strLine, "=")
strFormulaName = Trim(strFormulaVect(0))
strFormulaValue = Trim(strFormulaVect(1))
For Each strItem In dictFormulas.Keys
strFormulaValue = Replace(strFormulaValue, strItem, CStr(dictFormulas(strItem)))
Next
dictFormulas.Add strFormulaName, strFormulaValue
End Sub
Private Sub PrintFormulas()
Dim strItem As Variant
For Each strItem In dictFormulas.Keys
Debug.Print strItem & "=" & CStr(dictFormulas(strItem))
Next
End Sub
Dictionary is part of Microsoft Scripting Library (SCRRUN.DLL) must be added as reference in Project > References