Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Regression calls using custom class

Status
Not open for further replies.

ramani

Programmer
Mar 15, 2001
4,336
AE
I was just doing a testing on index types.
Refer thread..

multiple idx or cdx files for different users?
thread1252-953855

In the process, I did not want my fields to have digits, though STR(number) produces an Alfa field, I wanted that to be alphanumeric. That means I have to convert the numbers into alpha. For example
1 = 'A'
2 = 'B'
..
26 = 'Z'
27 = 'AA'
..
52 = 'AZ'
53 = 'BA'
..
702 = 'ZZ'
703 = 'AAA' .... and so on

There is a similarity in the convertion. A little code should do the job.
A to Z is nothing but CHR(64+nNumber) where nNumber is 1 to 26
The same can be regressively called to build AA... etc.

But if the same where to be put in a PRG or as a function, calling itself will give an error. So we have to create a little custom class so that the object can be created within itself with ADDOBJECT until required. This is the concept I used to build that little tester. The class code is named 'AlfaRegression'.

Now to create a test cursor with 100,00 records, you can run the code..
Code:
*******************************************
SET TALK OFF
SET CONFIRM OFF
SET SAFETY OFF

CLEAR ALL

** Add 100,000 transactions and check
#define nCount 10000

** Create a dummy table
CREATE CURSOR dbfTest1 (nField1 I, cField2 C(20))

FOR i = 1 TO nCount
    INSERT INTO dbfTest1 (nField1, cField2) ;
        VALUES (i,'MySeed '+getAlfa(i))
ENDFOR
BROW
USE

RETURN
********************************************
FUNCTION getAlfa
LPARAMETERS tnInteger

    LOCAL lcWord
    oGetAlfa = NewObject('alfaRegression')
    lcWord = oGetAlfa.getAlfa(tnInteger)
    RELEASE oGetAlfa

RETURN lcWord

**************************************************
*-- Class:        alfaregression (alfaregression.vcx)
*-- ParentClass:  custom
*-- BaseClass:    custom
*-- Time Stamp:   11/20/04 02:48:08 PM
*
DEFINE CLASS AlfaRegression AS custom

  Name = "AlfaRegression"

  *-- Convert integer to Alfa
  PROCEDURE getAlfa
    LPARAMETERS tnInteger

    IF PCOUNT() < 1 OR ;
        VARTYPE(tnInteger) # 'N' OR ;
        tnInteger = 0
      RETURN ''
    ENDIF

    LOCAL lcWord, lnNumber
    lnNumber = MOD(tnInteger-1,26)+1
    lcWord = CHR(64+lnNumber)
    lnNumber = FLOOR((tnInteger - lnNumber)/26)
    IF lnNumber > 0
      This.AddObject('oGetAlfa', 'AlfaRegression')
      lcWord = This.oGetAlfa.getAlfa(lnNumber) ;
                  +lcWord
      This.RemoveObject('oGetAlfa')
    ENDIF
    RETURN lcWord
  ENDPROC
ENDDEFINE
*
*-- EndDefine: AlfaRegression
**************************************************

Now where is this concept useful ?

We may want to create dummy records like this for testing and could be any numbe of records .. could million+ records.
OR
We may want to do similar regression in many places. Example 'resize class'. We have to resize each and every object inside a container object. We dont know how many PageFrame, Containers etc inside a form. In each of the Pageframe, there could be further Pageframes. I have forms in this manner. So we can use the syntax.. in a custom object..

FOR EACH oControl IN This.Controls
IF UPPER(oControl.BaseClass) $ 'PAGEFRAME#CONTAINER#whatever'
.. do whatever..
.. Add as an object in this object itself, when another PageFrame is found
ENDIF
ENDFOR

And there can be many other uses, once we understand that we can call the class from within itself and accept back a return value and cumulate that to be again returned to the calling method/procedure.

Good luck. :)

____________________________________________
ramani - (Subramanian.G) :)
 
Ramani,

This is very interesting. Thank you for posting it.

However, I hope you don't think I'm nit-picking, but could you explain what you mean by the term "regression"? I have always taken it to mean -- in the context of testing -- a test which proves that a recent change has not broken something that had already been tested.

It sound to me like you might have meant "recursion". Or am I jumpting to the wrong conclusion?

Mike


Mike Lewis
Edinburgh, Scotland

My Visual Foxpro web site: My Crystal Reports web site:
 
Yes Mike you are right in your assumptions. I mean 'recursion'. I dont know about the strict terminology, but we use this term regression also for that atleast in my surroundings.

:)

____________________________________________
ramani - (Subramanian.G) :)
 
Hello Ramani.

If you are interested in the uses of Recursion, you mat find it interesting to review some Kitbox articles in the past Issues of FoxTalk Magazine:

1. Curses and Recurses - March 1999

2. Let 'er Rip - July 2003

Marcia G. Akins
 
Hi ramani,

I don't see any advantage in adding an already existing object to itself like you do here:
Code:
...
This.AddObject('oGetAlfa', 'AlfaRegression')
lcWord = This.oGetAlfa.getAlfa(lnNumber)+lcWord
This.RemoveObject('oGetAlfa')#
...

This works too:
Code:
...
lcWord = This.getAlfa(lnNumber)+lcWord
...

And you even don't need the class/object, because that
can be done with a simple function:
Code:
FUNCTION getAlfa
   LPARAMETERS tnInteger

   IF PCOUNT() < 1 OR ;
        VARTYPE(tnInteger) # 'N' OR tnInteger = 0
      RETURN ''
    ENDIF

    LOCAL lcWord, lnNumber
    lnNumber = MOD(tnInteger-1,26)+1
    lcWord = CHR(64+lnNumber)
    lnNumber = FLOOR((tnInteger - lnNumber)/26)
    IF lnNumber > 0
      lcWord = getAlfa(lnNumber)+lcWord
    ENDIF
    RETURN lcWord
ENDFUNC

Well, and it's recursion you mean, as Marcia already said.

Bye, Olaf.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top