How to INDIRECTLY change a variable's value

General Help regarding HMG, Compilation, Linking, Samples

Moderator: Rathinagiri

Post Reply
Red2
Posts: 271
Joined: Sat May 18, 2019 2:11 pm
DBs Used: Visual FoxPro, FoxPro
Location: United States of America

How to INDIRECTLY change a variable's value

Post by Red2 »

Hi All,

1) I have a valid object:
oUserAns:OStreet1 := "3333 Some Street"

2) I need to indirectly change its value using another variable with its name stored as a string:
pcVarName := "oUserAns:OStreet1"

3) A third variable has new value to assign to oUserAns:OStreet1
lcNewVal := "123 Different Street"

// This is assignment is simple in VFP using indirect reference.
// However, in HMG this fails to change the value of oUserAns:OStreet1

Code: Select all

store lcNewVal to ( pcVarName )    // Indirect reference FAILS in HMG
QUESTION: Using just the name string (variable pcVarName) how is this done in HMG? (Change the actual value of oUserAns:OStreet1)

I am sure can be done in HMG. I would really appreciate your kind guidance and assistance.

Thank you,
Red2
SvargasD
Posts: 12
Joined: Sat Mar 24, 2018 2:50 pm
DBs Used: DBF

Re: How to INDIRECTLY change a variable's value

Post by SvargasD »

Buen Día:
Disculpa que no lo escriba en ingles (ya que no se), pero espero se entienda con el traductor de google.

1. Según veo, tienes un error de sintaxis:
store lcNewVal to ( pcVarName ) // Forma incorrecta
store lcNewVal to pcVarName // Forma correcta

2. No tengo mucha experiencia en programación, pero tengo un libro de clipper que menciona el comando store como obsoleto, se recomienda la asignación en linea, mucho mas practico:
lcNewVal := pcVarName

Saludos!

Traductor: https://www.deepl.com/translator
Red2
Posts: 271
Joined: Sat May 18, 2019 2:11 pm
DBs Used: Visual FoxPro, FoxPro
Location: United States of America

Re: How to INDIRECTLY change a variable's value

Post by Red2 »

Hi SvargasD,

Gracias por su amable respuesta. La sintaxis que sugiere es correcta si quiero cambiar el valor de la variable lcNewVal.
Su resultado es idéntico a: lcNewVal: = pcVarName.

Lo que realmente necesito cambiar es el valor de la variable llamada en pcVarName. No quiero simplemente cambiar el valor de pcVarName en sí. En cambio, es la variable nombrada en su contenido (pcVarName: = "oUserAns: OStreet1"). Es decir, el punto es cambiar (indirectamente) el valor de oUserAns: OStreet1.

En última instancia, quiero que el valor de oUserAns: OStreet1 sea "123 Different Street" (es decir, lcNewVal).

Simplemente no entiendo cómo Clipper, Harbor y HMG codifican esto. En FoxPro y Visual FoxPro, esto se llama "asignación indirecta", según recuerdo.

Estoy seguro de que hay alguna sintaxis para hacer esto en HMG, pero aún no la he encontrado. ¿Alguna otra idea?

De nuevo, gracias por tu ayuda.

Saludos cordiales,
Red2

/////////////

Thank you for your kind response. The syntax you suggest is correct if I want to change the valuable of variable lcNewVal.
Its result is identical to: lcNewVal := pcVarName.

What I need to actually change is the value of the variable named in pcVarName. I do not want to simply change the value of pcVarName itself. Instead, it is the variable named in it's contents (pcVarName := "oUserAns:OStreet1"). That is, the point is to change (indirectly) the value of oUserAns:OStreet1.

Ultimately I want the value of oUserAns:OStreet1 to be "123 Different Street" (i.e. lcNewVal).

I simply don't understand how Clipper, Harbour, and HMG code this. In FoxPro and Visual FoxPro this is called "indirect assignment" as I recall.

I am sure that there is some syntax to do this in HMG but I have not found it yet. Any other ideas?

Again, thank you for your help.

Kind regards,
Red2
User avatar
AUGE_OHR
Posts: 2060
Joined: Sun Aug 25, 2019 3:12 pm
DBs Used: DBF, PostgreSQL, MySQL, SQLite
Location: Hamburg, Germany

Re: How to INDIRECTLY change a variable's value

Post by AUGE_OHR »

Red2 wrote: Tue Oct 22, 2019 5:36 pm 1) I have a valid object:
oUserAns:OStreet1 := "3333 Some Street"

2) I need to indirectly change its value using another variable with its name stored as a string:
pcVarName := "oUserAns:OStreet1"

3) A third variable has new value to assign to oUserAns:OStreet1
lcNewVal := "123 Different Street"

// This is assignment is simple in VFP using indirect reference.
// However, in HMG this fails to change the value of oUserAns:OStreet1

Code: Select all

store lcNewVal to ( pcVarName )    // Indirect reference FAILS in HMG
have you try

Code: Select all

lcNewVal :=  &pcVarName
or
lcNewVal :=  &(pcVarName)
pcVarName is a String but if you want to have value you must use a Macro
have fun
Jimmy
Red2
Posts: 271
Joined: Sat May 18, 2019 2:11 pm
DBs Used: Visual FoxPro, FoxPro
Location: United States of America

Re: How to INDIRECTLY change a variable's value

Post by Red2 »

Hallo AUGE_OHR,

Vielen Dank, ich schätze Ihre Hilfe sehr. Ich glaube, dass Ihr Vorschlag den Wert von lcNewVal ändert.

Eigentlich muss ich (indirekt) den Wert von oUserAns: OStreet1 in den Wert von lcNewVal ändern. Wichtig: Ich möchte nicht, dass sich die Werte von lcNewVal oder pcVarName ändern.

Um dies (indirekt) zu tun, muss die Variable, die durch den Zeichenfolgenwert von pcVarName ("oUserAns: OStreet1") angegeben ist, aktualisiert werden. Mache ich hier sinn?

Dies ist in VFP einfach zu bewerkstelligen. Wie geht das in HMG / Harbour?

Nochmals vielen Dank für Ihre großzügige Anleitung.

Red2

* * * * * * * * * * *
Hello AUGE_OHR,

Thank you, I very much appreciate your help. I that believe that your suggestion changes the value of lcNewVal.

Actually, I need to (indirectly) change the value of oUserAns:OStreet1 to the value of lcNewVal. Important: I do NOT want the values of either lcNewVal or pcVarName to change.

To do this (indirectly) I need the variable indicated by the string value of pcVarName ("oUserAns:OStreet1") to be updated. Do I make sense here?

This is easy to do in VFP. So, how is this done in HMG/Harbour?

Again, thank you for your generous guidance.

Red2
Red2
Posts: 271
Joined: Sat May 18, 2019 2:11 pm
DBs Used: Visual FoxPro, FoxPro
Location: United States of America

Re: How to INDIRECTLY change a variable's value

Post by Red2 »

Hi All,

I believe that I finally have a solution after considering AUGE_OHR's post:

Code: Select all

oUserAns:OStreet1 := "444 Some Street"
pcVarName    := "oUserAns:OStreet1"
lcNewValue   := "1234 Another Street"
&(pcVarName) := lcNewValue    //  This indirect "macro" syntax works!
msginfo( oUserAns:OStreet1 )  // returns:  "1234 Another Street"
Thank you everyone for your helpful guidance!

Kind regards,
Red2
SvargasD
Posts: 12
Joined: Sat Mar 24, 2018 2:50 pm
DBs Used: DBF

Re: How to INDIRECTLY change a variable's value

Post by SvargasD »

Disculpa mi ignorancia:
¿por qué no cambiar el valor directamente?, ¿en que tipo de situaciones es útil emplearlo? xD
Red2
Posts: 271
Joined: Sat May 18, 2019 2:11 pm
DBs Used: Visual FoxPro, FoxPro
Location: United States of America

Re: How to INDIRECTLY change a variable's value

Post by Red2 »

Hola SvargasD

Claro, esa es una gran pregunta. Haré todo lo posible para decir por qué. Durante el OnInit de un formulario, el contenido de un archivo de texto se lee en una variable. Esa variable se analiza y se asignan ciertos valores a un objeto de formulario.
Los valores de propiedad de este objeto, entonces, se asignan a los valores de TextBox del formulario.

Cuando se guarda el formulario, recorro todos los cuadros de texto. Si un TextBox ha cambiado, se pasan dos parámetros a una función:
1) El nombre de la propiedad del objeto relevante como una cadena, es decir, "oUserAns: OStreet1" (no la propiedad del objeto en sí).
2) El valor actual de TextBox

La función debe:
1) Actualice el objeto de formulario que originalmente asignó el valor de TextBox
2) La variable de memoria del archivo de texto (completo) debe analizarse y actualizarse con precisión.

El punto es que puedo minimizar la cantidad de código y hacer de manera inteligente todas las actualizaciones necesarias. Sin duda, otro programador presentaría una solución superior.

Espero que esta simple explicación sea útil.

Saludos cordiales,

* * * * * * * * *
Sure, that's a great question. I'll do my best to state why. During a form's OnInit a text file's contents is read into a variable. That variable is parsed and certain values are assigned to a form object.
This object's property values are, then, assigned to the form's TextBox values.

When the form is saved I iterate through all TextBoxes. If a TextBox has changed then two parameters are passed to a function:
1) The name of the relevant object property as a string, i.e. "oUserAns:OStreet1" (not the object property itself).
2) The TextBox's current value

The function must:
1) Update the form object that originally assigned the TextBox's value
2) The memory variable of (entire) text file must be parsed and accurately updated.

The point is that I can minimize the amount of code and intelligently make all necessary updates. Another programmer would undoubtedly come up with a superior solution.

I hope that this simple explanation is helpful.

Kind Regards,
Red2
SvargasD
Posts: 12
Joined: Sat Mar 24, 2018 2:50 pm
DBs Used: DBF

Re: How to INDIRECTLY change a variable's value

Post by SvargasD »

Excelente, muchas gracias por tu respuesta.
De hecho creo que vi algo parecido por aquí, alguien de este foro comento que trataba de normalizar los nombres de los campos de una base de datos, con los nombres de los texbox, y mediante un ciclo recorrer todos los campos del formulario e ir actualizando la base da datos.
La propuesta que tu presentas por lo que entiendo va encaminado a la misma solución y objetivo de ahorro de código, solo que a mi en lo particular todavía me cuesta un poco aplicar.

Voy a practicar mas!!!

Saludos!
Post Reply