substitution with variable value...

henry1311

Member
Licensed User
Longtime User
Hello all.

How do you B4A in the substitution of a variable with its value?

dim abc = "test" as string
dim xyz = "abc" as string
log("xyz="&xyz) ' it returns "abc"

how do to return "test"?
in visual foxpro &xyz : it returns "test"

Thank you.
Hello
Enrico
 
Last edited:

henry1311

Member
Licensed User
Longtime User
for now I solved it like this:

B4X:
  Dim c1 = "test [[s1]] - [[s2]] - [[s3]]" As String
  Dim m1 As Map
  m1.Initialize
  m1.Put("s1", "10")
  m1.Put("s2", "t2")
  m1.Put("s3", "x5")
  
  Dim stmp As String
  For Each t1 As String In m1.Keys
    stmp = m1.Get(t1)
    Log("key="&t1&"-srmp="&stmp)
    c1=c1.Replace("[["&t1&"]]", stmp)
  Next
  Log("c1="&c1)

result : c1=test 10 - t2 - x5

Ciao
Enrico
 
Upvote 0

edgar_ortiz

Active Member
Licensed User
Longtime User
Ok... I'm LOST...

What do you expect?

In my opinion... the BEST advantage of Macro Substitution is that you can "create" code in the run time.

Regards,

Edgar

P.D.:

GO VFP
 
Upvote 0

henry1311

Member
Licensed User
Longtime User
I had not explained it well!

B4X:
Sub Activity_Create(FirstTime As Boolean)
  Dim c1 = "select * from logs where log_id>=[[w_log_id]] and log_app = '[[w_log_app]]' order by [[ordlog]]" As String

  Dim w_log_id As Int
  Dim w_log_app, ordlog As String
  w_log_id=10 ' value returned by calculations and processing
  w_log_app="app p28"   ' value returned from editText.Text and/or value returned by calculations and processing
  ordlog="log_id"

  Dim m1 As Map
  m1.Initialize
  m1.Put("w_log_id", w_log_id)
  m1.Put("w_log_app", w_log_app)

  Dim ret As String
  ret=CreateW(c1, m1)
  Log("ret="&ret)
   
' RETURN : ret=select * from logs where log_id>=10 and log_app = 'app p28' order by log_id
   
End Sub

Sub CreateW(pC1 As String, pMap As Map) As String
  Dim stmp As String
  For Each t1 As String In pMap.Keys
    stmp = pMap.Get(t1)
    'Log("key="&t1&"-stmp="&stmp)
    pC1=pC1.Replace("[["&t1&"]]", stmp)
  Next
  Return pC1
End Sub

since the 'select statements' and the filter fields in the 'Where statements' are many I did not want to do for each:
c1 = "select * from logs where log_id>="&w_log_id&"and log_app='"&w_log_app&"'"&etc...etc...&" order by"&ordlog

With macrosubstitution would avoid loading the Map (m1) because, as I say, can also be a lot of filters.

B4X:
Sub CreateW1(pC1 As String) As String
  dim i=Pc1.indexof("[[") as int
  dim f=pC1.indexof("]]") as int
  dim s1 as string
  s1=pC1.substring2(i+2, f-i+2)      ' it will return = w_log_id/w_log_app/ordlog
  return pC1.replace("[["&s1&"]]", ValueOfs1)  
End Sub


However, for now I do with the map ...
Thank you for the help you have given me.
Ciao
Enrico
 
Upvote 0
Top