Create a variable

Create a variable

Postby Natter » Tue Nov 15, 2022 7:20 pm

Hi,

To create a variable, I can write, for example: LOCAL AAA
Is it possible to create such a variable programmatically ?
Natter
 
Posts: 1221
Joined: Mon May 14, 2007 9:49 am

Re: Create a variable

Postby Antonio Linares » Wed Nov 16, 2022 8:09 am

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
User avatar
Antonio Linares
Site Admin
 
Posts: 42099
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: Create a variable

Postby Natter » Wed Nov 16, 2022 9:22 am

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
Natter
 
Posts: 1221
Joined: Mon May 14, 2007 9:49 am

Re: Create a variable

Postby hmpaquito » Wed Nov 16, 2022 9:29 am

Perhaps,

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

regards
hmpaquito
 
Posts: 1482
Joined: Thu Oct 30, 2008 2:37 pm

Re: Create a variable

Postby Marc Venken » Wed Nov 16, 2022 9:53 am

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,...)
Marc Venken
Using: FWH 23.04 with Harbour
User avatar
Marc Venken
 
Posts: 1434
Joined: Tue Jun 14, 2016 7:51 am
Location: Belgium

Re: Create a variable

Postby Marc Venken » Wed Nov 16, 2022 10:21 am

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
Marc Venken
Using: FWH 23.04 with Harbour
User avatar
Marc Venken
 
Posts: 1434
Joined: Tue Jun 14, 2016 7:51 am
Location: Belgium

Re: Create a variable

Postby Natter » Wed Nov 16, 2022 11:49 am

Thank you, an interesting opportunity !
Last edited by Natter on Thu Nov 17, 2022 6:54 am, edited 1 time in total.
Natter
 
Posts: 1221
Joined: Mon May 14, 2007 9:49 am

Re: Create a variable

Postby nageswaragunupudi » Wed Nov 16, 2022 7:02 pm

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
User avatar
nageswaragunupudi
 
Posts: 10643
Joined: Sun Nov 19, 2006 5:22 am
Location: India

Re: Create a variable

Postby Natter » Wed Nov 16, 2022 8:09 pm

Thank you, Rao !
Natter
 
Posts: 1221
Joined: Mon May 14, 2007 9:49 am

Re: Create a variable

Postby Antonio Linares » Wed Nov 16, 2022 9:38 pm

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

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 42099
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: Create a variable

Postby Natter » Thu Nov 17, 2022 6:48 am

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.
Natter
 
Posts: 1221
Joined: Mon May 14, 2007 9:49 am

Re: Create a variable

Postby Otto » Thu Nov 17, 2022 6:54 am

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
********************************************************************
User avatar
Otto
 
Posts: 6332
Joined: Fri Oct 07, 2005 7:07 pm


Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: No registered users and 88 guests