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

WITH... ENDWITH Syntax 2

Status
Not open for further replies.

TheProgrammerGuy

Programmer
Nov 12, 2019
29
US
I need to flip a bunch of checkboxes and I've forgotten how. They are all named the same with 1 to 60 at the end. I have started looking through old code but that could take a LONG time. :) Any help is greatly appreciated.
All I need is the syntax for the command that actually does the flipping.

Thanks!

Code:
WITH thisform.pageframe.page1.whatever.
[indent]FOR i = 1 TO 60[/indent]
[indent][indent]mName= checkboxname+ ALLTRIM(STR(i))[/indent][/indent]
[indent][indent]How do I flip them true to false?[/indent][/indent]
[indent]NEXT i[/indent]
ENDWITH
 
Are they bound to data? Then flip the data. You could do a for each loop on objects. Using macro substitution on an "array" of checkboxes named that way is the last thing I'd program. But that would be the usual solution. This can be done without, too, using GetPEM to get the checkboxes as form/page/container object "properties" or members.

Code:
Local lnCount, loObject

For lnCount = 1 to 60
    loObject = GetPEM(thisform.pageframe.page1.whatever,"checkbox"+Transform(lnCount)) 
    loObject.value = not loObject.value
EndFor

This doesn't use WITH..ENDWITH at all, I didn't forget it. The "whatever" must be the same object you used for WITH. The object that has the 60 checkboxes. If you use WITH, though, you can't pass just a dot as the first parameter of GetPEM. So when there still is more that depends on WITH ENDWITH let it step one hierarchy level to the parent object, so you can use GetPEM(.whatever,...).

I usually avoid WITH..ENDWITH as a RETURN from within it can cause trouble and I better like to have an object reference variable.

Well, but the major point is, that to address an object with such a numbered name you can use a function that works on a name as parameter, too: GetPEM()

Instead of all this using a class you could simply switch by adding a switch method to it and trigger it as an assign method using one SetAll() call. Use OOP and you have less such problems at all.

Bye, Olaf.

Olaf Doschke Software Engineering
 
Or even nest it like this if you're fan of WITH..ENDWITH:

Code:
Local lnCount

With thisform.pageframe.page1
    For lnCount = 1 to 60
       With GetPEM(.whatever,"checkbox"+Transform(lnCount)) 
           .value = not .value
       Endwith
    EndFor
Endwith

Bye, Olaf.

Olaf Doschke Software Engineering
 
Thanks, Olaf. But I got it. I did use macros as it doesn't make any sense (to me) to make it any more complicated. The "usual" solution is sometimes the best :). Can't flip the data as that would be very counter-productive in this case.
Also, I NEVER use "RETURN" in my code, it is BAD logic. You should ALWAYS exit any kind of loop normally or you are asking for trouble. That is programming 101.

Thanks!
 
Haha, functions return values. Anyway, there are certain philisophies to always return at the end of code. There also are cases that overcomplicated things.

Bye, Olaf.

Olaf Doschke Software Engineering
 
Also, I NEVER use "RETURN" in my code, it is BAD logic. You should ALWAYS exit any kind of loop normally or you are asking for trouble. That is programming 101.

What on earth does that mean? How can a function return a value unless you use RETURN? And what do you mean by existing a loop "normally"? How would you exit a loop abnormally?

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
Olaf was specifically talking about RETURNing from within a LOOP. NOT a good idea in every case. Of course I use RETURN for functions and ending procedures. It is even moronic to suggest that I don't.
Yes, after just now re-reading Olaf's post I see he was talking about using RETURN in WITH... ENDWITH so that was my mistake. But my code has a loop in it and this is what was on my mind. He was answering a "question" that I did not ask. Bad on both of us, I think.
 
WITH ENDWITH is no loop. It's just a nested part of code acting on the same object, abbreviation its name!

There is no technical risk to exit a loop on local variables, but I'd break out of a loop before exiting normally. Anyway we're talking about WITH ENDWITH and no loops, you just have a loop here on your elements I would solve in any other way, but that's out of the scope of this topic of the technical bug of WITH.ENDWITH, no matter if there is a loop within it or not. Bit the whole construct is nothing else but setting come "invisible" nameless variable to the object and then address it with just a dot. If you use a real variable instead of VFPs construct, you still have to write the variable name every time, but you have an "honest" object and no problems, as its scope is just the variable scope and its handled cleanly.

I wouldn't even trust logic to always handle this hidden object reference correctly in complex nested situations, no matter if you exit the code from within it or noz, I don't trust the construct and the better credibility it gives you for extremely long names like the second container in the third page of the fourth pageframe of the form, can also be shortened by storing it to a variable.

Bye, Olaf.

Olaf Doschke Software Engineering
 
And now look at WITH..ENDWITH from another perspective, from the OOP perspective (again): What object reference is most often used in good encapsilated object oriented code? THIS. Objects handle themselves, not other objects.

Any code using WITH..ENDWITH in the end points out it's code belonging inside that object you now make a surrogate "THIS" reference.

The whole construct has good intentions, but actually even without the flaw you can refactor code and do without it.

Rethink about why I recommend flipping data: Forms/UI is about representing data or states, information, it merely should serve to display the actual model of the state you have in the backend layer. And then you don't ever act on value properties from outside of something, you bind controls to data and change data. The representation could change and your code handling the data still is reusable.

Bye, Olaf.

Olaf Doschke Software Engineering
 
Of course I use RETURN for functions and ending procedures. It is even moronic to suggest that I don't.

Well, speaking as a long-standing moron, I have to say that that might have been what you meant, but it wasn't what you said. But never mind. Since when was precision a requirement of a good programmer?

Mike


__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
This comment is totally abusive.



If you want to get the best response to a question, please check out FAQ184-2483 first.
 
I see, TheProgrammerGuy

but if you click on "Post Edited" you can see the history and that you added that insight later. I am not angry and not moronic, I just add a little more value, and since you started talking about programming styles. That's an opportunity to exchange ideas on that. But sure, I see you don't like that and so that ends here. And thanks for making me younger than I am, I am about as old as you.

Bye, Olaf.

Olaf Doschke Software Engineering
 

This comment is totally abusive.

I fully agree - unfortunately I happened to think that programmers/IT people were cool, reasonable and logically thinking individuals and would not let themselves be misled to such irreverent comments. My advice to TheProgrammerGuy - si tacuisses philosophus mansisses

MK
 
It turns out the post was removed, oouf



If you want to get the best response to a question, please check out FAQ184-2483 first.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top