Page 1 of 1

Create a variable

PostPosted: Tue Nov 15, 2022 7:20 pm
by Natter
Hi,

To create a variable, I can write, for example: LOCAL AAA
Is it possible to create such a variable programmatically ?

Re: Create a variable

PostPosted: Wed Nov 16, 2022 8:09 am
by Antonio Linares
Yes, but it won't be local

Simply assign it. At the top write:

memvar MyVar

later in your code:

MyVar = ...

but it will not be local, it will be public

Re: Create a variable

PostPosted: Wed Nov 16, 2022 9:22 am
by Natter
Thanks Antonio, but this is somewhat not what I need. For example, I need to make 10 variables

for st=1 to 10
create variable "n_"+ltrim(str(st))
next

Re: Create a variable

PostPosted: Wed Nov 16, 2022 9:29 am
by hmpaquito
Perhaps,

for st=1 to 10
cName:= "n_"+ltrim(str(st))
PRIVATE &cName
next

regards

Re: Create a variable

PostPosted: Wed Nov 16, 2022 9:53 am
by Marc Venken
Natter wrote:Thanks Antonio, but this is somewhat not what I need. For example, I need to make 10 variables

for st=1 to 10
create variable "n_"+ltrim(str(st))
next


Maybe better use a array ?

Local aGallery := ARRAY(12) // Max = 12 items

is very easy to programmaticaly fill these with specific data in a loop

for i = 1 to len(aGallery)
aGallery[i] = i // or any dbf value or .....
next

You only have to think of 1 var name .... (Local of public,...)

Re: Create a variable

PostPosted: Wed Nov 16, 2022 10:21 am
by Marc Venken
Marc Venken wrote:
Natter wrote:Thanks Antonio, but this is somewhat not what I need. For example, I need to make 10 variables

for st=1 to 10
create variable "n_"+ltrim(str(st))
next


Maybe better use a array ?

Local aGallery := ARRAY(12) // Max = 12 items

is very easy to programmaticaly fill these with specific data in a loop

for i = 1 to len(aGallery)
aGallery[i] = i // or any dbf value or .....
next

You maybe also consider using Hashes (I prefer them)

look : viewtopic.php?f=3&t=19895&p=104891&hilit=hash#p104891

Re: Create a variable

PostPosted: Wed Nov 16, 2022 11:49 am
by Natter
Thank you, an interesting opportunity !

Re: Create a variable

PostPosted: Wed Nov 16, 2022 7:02 pm
by nageswaragunupudi
Natter wrote:Hi,

To create a variable, I can write, for example: LOCAL AAA
Is it possible to create such a variable programmatically ?


No.
Because, the local declarations must be available to the compiler.

Ofc, you can create private variables at runtime.

Re: Create a variable

PostPosted: Wed Nov 16, 2022 8:09 pm
by Natter
Thank you, Rao !

Re: Create a variable

PostPosted: Wed Nov 16, 2022 9:38 pm
by Antonio Linares
Dear Yuri,

This is another possible way:
Code: Select all  Expand view
#include "hbclass.ch"

function Main()

   local oVars := TLocal(), n

   for n = 1 to 10
      __objAddData( oVars, "n_" + LTrim( Str( n ) ) )    
   next
   
   oVars:n_1 = 123
   ? oVars:n_1

return nil  

return nil

CLASS TLocal

ENDCLASS
 

Re: Create a variable

PostPosted: Thu Nov 17, 2022 6:48 am
by Natter
Thank you very much for an interesting opportunity !

Re: Create a variable

PostPosted: Thu Nov 17, 2022 6:54 am
by Otto
Hello,

May I ask, what is the purpose of these variables?
Best regards,
Otto