With ... EndWith

How does nesting work with With ... EndWith Does VFP work out which object I am referring to if I just enter .cDescription for example Is there a potential for ambiguity if the 2 objects have the same property I would like to use nested With ... EndWith but I am wary of introducing some difficult to find bugs.

Answer this question

With ... EndWith

  • wigga

     CetinBasoz wrote:
    I prefer to use with ... endwith for maintainability and readability:) Completeley reverse POVs for same purpose (and of course I also do it for performance).

    Just to clarify. I said nested WITH..ENDWITH. I do use plain WITH..ENDWITH a lot.

    Just that I try to avoid nesting or sometimes nesting more than once for readability purposes.


  • Anders Olsson - Sweden

    Yes I spotted that.

    On a side issue... Curiously I never selected which post was the answer. It seems the forum has decided it for me. Strange.


  • Bill Cumming

    Yes it translates to oInnerObject.cModified. Another sample:

    with thisform
       with .PageFrame1
             with .Page1
                 with .cntMyContainer
                     with .Text1
                          .Value = "Hello"
                     endwith
                 endwith
                 with .Text1
                     .Value = "there"
                 endwith
            endwith
        endwith
        .Text1 = "again"
    endwith

    Translates to:

    thisform.PageFrame1.Page1.cntMyContainer.Text1.Value = "Hello"
    thisform.PageFrame1.Page1.Text1.Value = "there"
    thisform.Text1.Value = "again"

    Or with a loop and evaluation:

    with thisform.myGrid1
            for ix = 1 to .ColumnCount
               with evaluate(".Column"+ltrim(str(m.ix)))
                    .Header1.Caption = .Name
               endwith
            endfor
    endwith

    translates to (assumed columns are named default Column1,Column2 ...):

    thisform.myGrid1.Column1.Header1.Caption = thisform.myGrid1.Column1.Name
    thisform.myGrid1.Column2.Header1.Caption = thisform.myGrid1.Column2.Name
    * ...

    It also means that if you would do multiple set/get with an object VFP doesn't need to resolve object hierarchy with each call. I find it more maintainable because if somehow you change object name and/or hierarchy all you need to change is enclosing "with ..." line.

     

     

     

     

     

     


  • lost.sync

    Fair enough. I didn't want Alex to think I did not value his input also. I am used to Experts Exchange where competition for points can get quite high and users get upset when they miss out :)

    So thank you Alex and CetinBasoz for your valuable input. And thank you Andy for clearing it all up.


     


  • pete_smith74

    Hey Alex,

    I prefer to use with ... endwith for maintainability and readability:) Completeley reverse POVs for same purpose (and of course I also do it for performance).

    PS: As to do original question with ..endwith works nicely to any level (though it doesn't go much to deep). When you say .cDescription it refers to oObject in its enclosing block of "with  oObject ... endwith". No hassle if multiple classes have same property.


  • Mark Pemberton

    Although it is helpful when I need a really quick answer I must admit the quality of answer there is sometimes low. It bugs me when someone 'answers' with a load of copy and paste or dozens of urls from a google search that might or might not be relevant. Others then see a reply and don't see the need to assist. Finding this forum was a real boon for me.
  • Vinayakg

     CetinBasoz wrote:

    with ..endwith works nicely to any level (though it doesn't go much to deep). When you say .cDescription it refers to oObject in its enclosing block of "with  oObject ... endwith". No hassle if multiple classes have same property.

    So is it guaranteed in the following code that oInnerObject will be modified

    With oOuterObject

    With oInnerObject

    .cModified = "Test"

    EndWith

    EndWith

     


  • Alan M.

    It should work fine with simple nesting.

    I prefer not to use nested WITH..ENDWITH for maintainability and readability.

    Just my .02


  • SivaRaja MSFT

    Oh it is partially my fault I think. Probaly I sounded to be competing/objecting with my message. Sorry. 


  • Smalldust

    >> On a side issue... Curiously I never selected which post was the answer. It seems the forum has decided it for me. Strange

    You can mark any question as an answer, and even have multiple questions as flagged as "answers" in a thread.

    Personally I think the terminology is misleading because the  real objective here is to give other readers an indication of which messages they should read if they are interested in the question - most threads are short but when you get a long thread with hundreds of answers it is useful to be able to identify the most relevant one(s).

    For this reason, the Forum Moderators (of which group I am one) can also flag replies as answers and I guess that in your case one of them did so - though I don't remember doing so in this case.

    Looking at the flagged answer I see that it does, in fact, contain the exact answer so it seems a reasonable enough one to mark and that distinguishes the message from other that simply containt a side discussion between two other members of the group which, while interesting, is not strictly relevant.



  • Adrijan

    In addition to what Alex said, I would suggest you try www.foxite.com it is also free, and whilst not as busy as the Universal Thread, has a reasonable volume of traffic and a good archive and search facilities.

    (Personally I find it less intimidating than the UT - which I tend to avoid)

     



  • Jayadev

    Darren,

    I am glad you are finding help. VFP is a wonderful product.

    Do not worry about my sensibilities, Cetin's or Andy's. We are here to help. If my answer or some other one is of help then we are happy. We do not compete for points or for being the "first to answer". The important issue is to get you running, solving your problem or at least pointing you in the right direction.

    There are many forums, some with more signal to noise ratio than others. You might also want to check http://www.universalthread.com which has a lively VFP section with several experts answering. It is free for basic usage.

    This forum (MS) although with a smaller traffic than others has IMO a higher signal to noise ratio (i.e. the good or helpful answers comprise a higher percentage of the message base).

    Cheers!

     


  • MarkCrowley

    >>  I am used to Experts Exchange where competition for points can get quite high and users get upset when they miss out :)

    Fortunately we do not do that here - I don't really see that it benefits the person asking a question and, while it is mildly interesting, it does tend (in my opinion) to lead to unnecessary 'races to answer'.

    This is one of the (many) reasons why I eschew that and similarly organized fora.



  • With ... EndWith