Create a variable

Post Reply
Natter
Posts: 1241
Joined: Mon May 14, 2007 9:49 am

Create a variable

Post by Natter »

Hi,

To create a variable, I can write, for example: LOCAL AAA
Is it possible to create such a variable programmatically ?
User avatar
Antonio Linares
Site Admin
Posts: 42521
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain
Has thanked: 31 times
Been thanked: 76 times
Contact:

Re: Create a variable

Post 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
regards, saludos

Antonio Linares
www.fivetechsoft.com
Natter
Posts: 1241
Joined: Mon May 14, 2007 9:49 am

Re: Create a variable

Post 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
hmpaquito
Posts: 1482
Joined: Thu Oct 30, 2008 2:37 pm

Re: Create a variable

Post by hmpaquito »

Perhaps,

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

regards
User avatar
Marc Venken
Posts: 1485
Joined: Tue Jun 14, 2016 7:51 am
Location: Belgium

Re: Create a variable

Post 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 // or any dbf value or .....
next

You only have to think of 1 var name .... (Local of public,...)
Marc Venken
Using: FWH 23.08 with Harbour
User avatar
Marc Venken
Posts: 1485
Joined: Tue Jun 14, 2016 7:51 am
Location: Belgium

Re: Create a variable

Post 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 // or any dbf value or .....
next

You maybe also consider using Hashes (I prefer them)

look : http://forums.fivetechsupport.com/viewt ... sh#p104891
Marc Venken
Using: FWH 23.08 with Harbour
Natter
Posts: 1241
Joined: Mon May 14, 2007 9:49 am

Re: Create a variable

Post by Natter »

Thank you, an interesting opportunity !
Last edited by Natter on Thu Nov 17, 2022 6:54 am, edited 1 time in total.
User avatar
nageswaragunupudi
Posts: 10721
Joined: Sun Nov 19, 2006 5:22 am
Location: India
Been thanked: 8 times
Contact:

Re: Create a variable

Post 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.
Regards

G. N. Rao.
Hyderabad, India
Natter
Posts: 1241
Joined: Mon May 14, 2007 9:49 am

Re: Create a variable

Post by Natter »

Thank you, Rao !
User avatar
Antonio Linares
Site Admin
Posts: 42521
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain
Has thanked: 31 times
Been thanked: 76 times
Contact:

Re: Create a variable

Post by Antonio Linares »

Dear Yuri,

This is another possible way:

Code: Select all | Expand

#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
 
regards, saludos

Antonio Linares
www.fivetechsoft.com
Natter
Posts: 1241
Joined: Mon May 14, 2007 9:49 am

Re: Create a variable

Post by Natter »

Thank you very much for an interesting opportunity !
Last edited by Natter on Thu Nov 17, 2022 6:59 am, edited 1 time in total.
User avatar
Otto
Posts: 6403
Joined: Fri Oct 07, 2005 7:07 pm
Has thanked: 24 times
Been thanked: 2 times
Contact:

Re: Create a variable

Post by Otto »

Hello,

May I ask, what is the purpose of these variables?
Best regards,
Otto
********************************************************************
mod harbour - Vamos a la conquista de la Web
modharbour.org
https://www.facebook.com/groups/modharbour.club
********************************************************************
Post Reply