locked
Parameters in a VFP method RRS feed

  • Question

  • Hi All,

    I want to use PARAMETERS in a VFP method. How can I achieve this.

    Example:
    In a method called MyMethod
    FUNCTION getperiod(ldDateIn, lnPeriodIdIn)

    SELECT myTable
    SELECT periodid FROM myTable WHERE DateIn = ldDateIn AND PeriodIdIn = lnPeriodIdIn INTO CURSOR myCursor

    RETURN PeriodID (Which in this case is a numeric value)

    Thanks for any help or insight.

    Regards,

    Dave


    Wednesday, February 7, 2007 8:37 PM

Answers

  • >> I want to use PARAMETERS in a VFP method. How can I achieve this.

    There are three ways:

    First there is the implicit declaration - which is what you are using in your example.
    FUNCTION Name( parameter_name1, parameter_name2 )
    Parameters created this way are scoped as LOCAL and so are usable only in the method, procedure or function that defines them.

    Second is the Explicit Declaration, using the LPARAMETERS keyword, like this:
    FUNCTION Name
    LPARAMETERS 
    parameter_name1, parameter_name2
    Parameters created this way are also scoped as LOCAL and so are usable only in the method, procedure or function that defines them.

    Third is the Explicit Declaration, using the PARAMETERS keyword, like this:
    FUNCTION Name
    PARAMETERS 
    parameter_name1, parameter_name2
    Parameters created this way are also scoped as PRIVATE and so are usable in the method, procedure or function that defines them and in any subordinate (i.e. called from) method, procedure or function

    Unless you have a particular reason, making parameters PRIVATE is generally a bad idea as it can lead to name conflicts. As to which of the methods of declaring local parameters you use it is entirely up to you - they are functionally identical.

    For what it's worth, I prefer the implicit declaration because the method signature matches the calling signature...but it makes no difference.

     

    Thursday, February 8, 2007 10:20 AM
  • You are saying "in a method called MyMethod" and then defining a function named "getPeriod". So what are you really asking? Is there a method called MyMethod? If there is who is the owner of that method (a form method, custom object method etc). IOW where do you really write your code? If you mean your form has a method called 'MyMethod' then there wouldn't be any "function" there (you can't create functions within methods - and in practice method, event, function, procedure are same things unless you're a purist on terminology).

    Considering you really meant that your form has a method called 'MyMethod':

    *form.MyMethod
    lparameters ldDateIn, lnPeriodIDIn
    local array aResult[1]
    select periodID from myTable ;
       where DateIn = m.ldDateIn and PeriodIDIn = m.ldPeriodIDIn ;
       into array aResult
    return iif( _Tally = 0, 0, aResult )

     

    You might want to return .null. in case there is no match. If it is an external function then the only thing that changes is the "function functionName" line at top.

    Thursday, February 8, 2007 2:12 PM

All replies

  • I'm not really sure what you're asking. The method you show has two parameters, ldDateIn and lnPeriodIDIn and you're using them.

    To return the value selected, you should add the alias:

    RETURN myCursor.PeriodID

    Tamar
    Wednesday, February 7, 2007 9:51 PM
    Answerer
  • I might understand:

    The getperiod-Method you create must have 'LParameters' as the 1st line:

    LParameters ldDateIn, lnPeriodIdIn

    SELECT myTable
    SELECT periodid FROM myTable WHERE DateIn = ldDateIn AND PeriodIdIn = lnPeriodIdIn INTO CURSOR myCursor

    RETURN (PeriodID)    &&--(Which in this case is a numeric value)

    ***************************************************

    You might Issue the command from another method within the same form:

    (i.e., if "set strictdate off" was issued)

    lPERIOD = thisform.getperiod({12/12/2006}, 40)

    or you might issue the command from a myGetPeriodRoutine in a myProgram.prg

    lPERIOD = _screen.activeform.getperiod({12/12/2006}, 40)

     

    Hope this helps

    Thursday, February 8, 2007 3:28 AM
  • >> I want to use PARAMETERS in a VFP method. How can I achieve this.

    There are three ways:

    First there is the implicit declaration - which is what you are using in your example.
    FUNCTION Name( parameter_name1, parameter_name2 )
    Parameters created this way are scoped as LOCAL and so are usable only in the method, procedure or function that defines them.

    Second is the Explicit Declaration, using the LPARAMETERS keyword, like this:
    FUNCTION Name
    LPARAMETERS 
    parameter_name1, parameter_name2
    Parameters created this way are also scoped as LOCAL and so are usable only in the method, procedure or function that defines them.

    Third is the Explicit Declaration, using the PARAMETERS keyword, like this:
    FUNCTION Name
    PARAMETERS 
    parameter_name1, parameter_name2
    Parameters created this way are also scoped as PRIVATE and so are usable in the method, procedure or function that defines them and in any subordinate (i.e. called from) method, procedure or function

    Unless you have a particular reason, making parameters PRIVATE is generally a bad idea as it can lead to name conflicts. As to which of the methods of declaring local parameters you use it is entirely up to you - they are functionally identical.

    For what it's worth, I prefer the implicit declaration because the method signature matches the calling signature...but it makes no difference.

     

    Thursday, February 8, 2007 10:20 AM
  • You are saying "in a method called MyMethod" and then defining a function named "getPeriod". So what are you really asking? Is there a method called MyMethod? If there is who is the owner of that method (a form method, custom object method etc). IOW where do you really write your code? If you mean your form has a method called 'MyMethod' then there wouldn't be any "function" there (you can't create functions within methods - and in practice method, event, function, procedure are same things unless you're a purist on terminology).

    Considering you really meant that your form has a method called 'MyMethod':

    *form.MyMethod
    lparameters ldDateIn, lnPeriodIDIn
    local array aResult[1]
    select periodID from myTable ;
       where DateIn = m.ldDateIn and PeriodIDIn = m.ldPeriodIDIn ;
       into array aResult
    return iif( _Tally = 0, 0, aResult )

     

    You might want to return .null. in case there is no match. If it is an external function then the only thing that changes is the "function functionName" line at top.

    Thursday, February 8, 2007 2:12 PM