FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index FiveWin for Harbour/xHarbour Create a variable
Posts: 1392
Joined: Mon May 14, 2007 09:49 AM
Create a variable
Posted: Tue Nov 15, 2022 07:20 PM
Hi,

To create a variable, I can write, for example: LOCAL AAA
Is it possible to create such a variable programmatically ?
Posts: 44162
Joined: Thu Oct 06, 2005 05:47 PM
Re: Create a variable
Posted: Wed Nov 16, 2022 08: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
Posts: 1392
Joined: Mon May 14, 2007 09:49 AM
Re: Create a variable
Posted: Wed Nov 16, 2022 09: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

Posts: 1515
Joined: Thu Oct 30, 2008 02:37 PM
Re: Create a variable
Posted: Wed Nov 16, 2022 09:29 AM

Perhaps,

for st=1 to 10

 cName:= "n_"+ltrim(str(st))

 PRIVATE &cName

next

regards

Posts: 1487
Joined: Tue Jun 14, 2016 07:51 AM
Re: Create a variable
Posted: Wed Nov 16, 2022 09: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 // 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
Posts: 1487
Joined: Tue Jun 14, 2016 07:51 AM
Re: Create a variable
Posted: Wed Nov 16, 2022 10:21 AM
Marc Venken 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/viewtopic.php?f=3&t=19895&p=104891&hilit=hash#p104891
Marc Venken

Using: FWH 23.08 with Harbour
Posts: 1392
Joined: Mon May 14, 2007 09:49 AM
Re: Create a variable
Posted: Wed Nov 16, 2022 11:49 AM

Thank you, an interesting opportunity !

Posts: 10733
Joined: Sun Nov 19, 2006 05:22 AM
Re: Create a variable
Posted: Wed Nov 16, 2022 07: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
Posts: 1392
Joined: Mon May 14, 2007 09:49 AM
Re: Create a variable
Posted: Wed Nov 16, 2022 08:09 PM

Thank you, Rao !

Posts: 44162
Joined: Thu Oct 06, 2005 05:47 PM
Re: Create a variable
Posted: Wed Nov 16, 2022 09:38 PM
Dear Yuri,

This is another possible way:
Code (fw): Select all Collapse
#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
Posts: 1392
Joined: Mon May 14, 2007 09:49 AM
Re: Create a variable
Posted: Thu Nov 17, 2022 06:48 AM

Thank you very much for an interesting opportunity !

Posts: 6984
Joined: Fri Oct 07, 2005 07:07 PM
Re: Create a variable
Posted: Thu Nov 17, 2022 06:54 AM

Hello,

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

Best regards,

Otto

Continue the discussion