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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using variables or fields dynamically and looping through sequentially named fields

Status
Not open for further replies.

dantheinfoman

Programmer
May 5, 2015
131
0
0
US

Hi All,

Another stumper, I think. I'd like to loop through fields once a record is selected.

If I have fields named Cri1, Cri2, Cri3 etc up until Cri15, how can I loop between them dynamically?

For example, if I was analyzing just those three (Cri1, Cri2, and Cri3), Id rather do something like this

Code:
SCAN
	IF !DELETED() then    
		WITH thisform.nsavedSC
			.AddItem(cstrptnam)
                        for x = 1 to 3
                               IF !EMPTY(cri + x)
			          .AddListItem((cri + x), .NewIndex, x+1)
                               ENDIF
                        endfor
		ENDWITH                
	ENDIF
ENDSCAN

Rather than how I think I'll have to do it. . .

Code:
SCAN
	IF !DELETED() then    
		WITH thisform.nsavedSC
			.AddItem(cstrptnam)                        
                        if !EMPTY(cri1)
                           .AddListItem(cri1, .NewIndex, 2)
                        endif
                        if !EMPTY(cri2)
                           .AddListItem(cri2, .NewIndex, 3)
                        endif
                        if !EMPTY(cri3)
                           .AddListItem(cri3, .NewIndex, 4)
                        endif
                        *!* Ad infinitum. . .
		ENDWITH                
	ENDIF
ENDSCAN

Some languages allow this type of thing using EVALUATE or INDIRECT type functions. Thoughts?

Thanks great ones!
Dan
 
Yes, you can do this in VFP. You can use FIELD() or AFIELDS() to loop through the fields in a record; and you can use EVALUATE() to determined the contents of a field. So you will need something like this:

Code:
FOR lnI = 1 TO FCOUNT()
  ....
  luValue = EVALUATE(FIELDS(lnI))
  IF NOT EMPTY(luValue)
    .AddListItem(luValue, .NewIndex, lnI + 1)
  ...
  ENDIF
  ...
ENDFOR

The above is not meant to be fully working code. It's just to give you the general idea.

Mike


__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
EVALUATE() is a very handy function, it's fundamental to one of my favorite apps, which is extremely extensible by virtue of that function. The app uses metadata extensively.
 
Instead of cri + x you need Evaluate("cri" + transform(x)).

Even if you come from javascript you can to "cri"+x there to get either "cri1","cri2" or "cri3", but you can't do cri+x in any programming language without cri on it's own existing as field or variable.
In any case you'd need to compose a full string of the name and either evaluate it or use macro substitution.

Bye, Olaf.
 
Thanks all, I can't wait to try this very soon! Brilliant!
Dan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top