Subject Re: Control passing back and fro in a form sub routine
From Mervyn Bick <invalid@invalid.invalid>
Date Wed, 26 Jul 2023 10:56:57 +0200
Newsgroups dbase.getting-started
Attachment(s) square1.prgsquare1.wfmsquare2.wfm

On 2023/07/26 03:19, Milind Nighojkar wrote:

> Mevin to clarify myslef I want to build scenerio on your example itself
>
> You have used set procedure to square.prg. I want to put something like this
>
> function square(nVal)
>       local retVal
>       do while nVal < = 5
>            retVal = nVal*nVal
>            *Display nVal onto the form and come baclk
>
>            nVal = nVal+1
>        endd
>    return retVal
>
> here I want to go to square.form and display nVal onto the
>            *form and come back for next nVal
>
> Hope I clarify
>

Milind (and Akshat and anyone else out there :-) ), it helps if you
quote only the relevant parts from a message when you reply but it is
counter productive to quote the entire message.  Using Web-News makes it
worse as it includes the text from any attachements as well.  Having to
scroll through literally pages of meaningless text to get to your input
is a nuisance.  Please take the time to delete irrelevant text.

Once again, there is more than one way to address your problem and it
really depends on how you want to display the returned values.  A
function returns 1 value but this can be an array so you could do
something like the following..

function square1(nVal)
    local n, nVal, aArray
    aArray = new array()
    for n = nVal to nVal + 4 //will calculate 5 values
      aArray.add(n*n)
    next
    return aArray


In your form, say in a pushbutton's onClick event handler,

      form.squares = square1(nStartvalue)

This would place the values calculated in the program in an array stored
in a user-defined property of the form.  It would then be up to you to
write code in the form to display the values in the array.  See
squares1.wfm

It is, however, more usual to pass the values to be processed to the
function one by one and deal with displaying them as each value is
returned.  See squares2.wfm  In this case the loop to increment the
values to be calculated is in the form rather than in the function.

Neither way is "right" and neither way is "wrong" although my preference
is to do things "one by one" with multiple calls to the function.  By
making the decision in the form of how many values to calculate gives
the programmer far more control.  If, after you have used the function
that returns 5 values in several forms, you find you now need 7 values
there is no problem if the number of iterations is detemined in
individual forms.  If you have to change the function this may cause
problems in forms expecting only 5 values.  It means you may need to
write a new function.  Having two fuctions that are almost, but not
completely, identical is not good programming practice.

Mervyn.








function square1(nVal)
   local n, nVal, aArray
   aArray = new array()
   for n = nVal to nVal + 4
     aArray.add(n*n)
   next
   return aArray

** END HEADER -- do not remove this line
//
// Generated on 2023-07-26
//
parameter bModal
local f
f = new square2Form()
if (bModal)
   f.mdi = false // ensure not MDI
   f.readModal()
else
   f.open()
endif

class square2Form of FORM
   with (this)
      onOpen = class::FORM_ONOPEN
      height = 16.0
      left = 7.4286
      top = 1.6818
      width = 67.5714
      text = ""
   endwith

   this.PUSHBUTTON1 = new PUSHBUTTON(this)
   with (this.PUSHBUTTON1)
      onClick = class::PUSHBUTTON1_ONCLICK
      height = 2.0909
      left = 13.0
      top = 7.4091
      width = 15.2857
      text = "Calculate squares"
   endwith

   this.EDITOR1 = new EDITOR(this)
   with (this.EDITOR1)
      height = 8.1818
      left = 37.2857
      top = 3.5909
      width = 20.0
      value = ""
   endwith


   function ENTRYFIELD1_onGotFocus()
      this.value = ''
      form.entryfield2.value = ''
      return

   function PUSHBUTTON1_onClick()
     nStart = 3
     form.squares = square1(nStart)
     crlf = chr(13)+chr(10)
      for n = 1 to 5
           form.editor1.value += (n-1+nStart)+'  * '+(n-1+nStart)+' = '+ form.squares[n]+CRLF
      next
      return

   function form_onOpen()
      set procedure to square1.prg
      return

endclass



** END HEADER -- do not remove this line
//
// Generated on 2023-07-26
//
parameter bModal
local f
f = new square2Form()
if (bModal)
   f.mdi = false // ensure not MDI
   f.readModal()
else
   f.open()
endif

class square2Form of FORM
   with (this)
      onOpen = class::FORM_ONOPEN
      height = 16.0
      left = 7.4286
      top = 1.6818
      width = 67.5714
      text = ""
   endwith

   this.PUSHBUTTON1 = new PUSHBUTTON(this)
   with (this.PUSHBUTTON1)
      onClick = class::PUSHBUTTON1_ONCLICK
      height = 2.0909
      left = 13.0
      top = 7.4091
      width = 15.2857
      text = "Calculate squares"
   endwith

   this.EDITOR1 = new EDITOR(this)
   with (this.EDITOR1)
      height = 8.1818
      left = 37.2857
      top = 3.5909
      width = 20.0
      value = ""
   endwith


   function ENTRYFIELD1_onGotFocus()
      this.value = ''
      form.entryfield2.value = ''
      return

   function PUSHBUTTON1_onClick()
     crlf = chr(13)+chr(10)
     nStart = 3
      for n = nStart to 4+nStart
           form.editor1.value += str(n,2)+'  * '+str(n,2)+' = '+ square(n)+CRLF
      next
      return

   function form_onOpen()
      set procedure to square.prg
      return

endclass