Welcome, Guest
Username: Password: Remember me
  • Page:
  • 1

TOPIC:

Foreach errors 01 Oct 2022 22:23 #24081

  • ic2
  • ic2's Avatar
  • Topic Author


  • Posts: 1555
  • I must confess that I avoid Foreach as much as possible but in this code I want to go through the control of a Winforms Window to read/write values for which I don't know how to 'move through' using For Next . So I use something like this:
    Local t As System.Type
    Local controls As System.ComponentModel
    
    t=Self:GetType()
    controls=t.GetProperties()
    Foreach (Var fi In controls)
    	Var x:=fi.PropertyType.ToString()
    	Var y:=fi.Name
    Next

    I'd say that the Foreach looks as it should but I get 2 compiler errors on this line:

    XS9002 Parser unexpected input 'fi'
    XS9065 Assignment operator ':=' expected

    I probably overlook something obvious but I don't see why I get these 2 errors.

    Dick

    Please Log in or Create an account to join the conversation.

    Foreach errors 01 Oct 2022 23:35 #24082

    • Chris
    • Chris's Avatar


  • Posts: 3657
  • Hi Dick,

    For the first error, you just need to remove the parentheses, this is a c# syntax thing.

    For the other error, again assignment with "=" is a c# thing, for VO/X# you need to use ":=". The same for "." in t.GetProperties(), fi.PropertyType.ToString() and fi.Name, in VO/X# you need to use ":"
    XSharp Development Team
    chris(at)xsharp.eu

    Please Log in or Create an account to join the conversation.

    Foreach errors 02 Oct 2022 06:58 #24085

    • ic2
    • ic2's Avatar
    • Topic Author


  • Posts: 1555
  • Hello Chris,

    As nearly always you are right.

    The first confusing thing was that both errors appeared in the same (For Each) line. Indeed the code came from C# but I forgot to change the '=' to ':=' in the 2 lines above For each. So it is not so strange that I didn't understand an assignment error appearing in a line without an assignment.

    The second confusion is that AFAIK you can always add parentheses around part of your code. I just kept them from the C# sample, not realizing that in For Each they would cause a compiler error.

    I have good reasons to avoid For Each :P

    Anyway it's solved, thanks!

    Dick

    Please Log in or Create an account to join the conversation.

    Foreach errors 02 Oct 2022 08:52 #24087

    • Chris
    • Chris's Avatar


  • Posts: 3657
  • Hi Dick,

    Well, you can try any of the following in VO (not even in X#):

    FOR (n := 1) UPTO 10
    FOR (n := 1 UPTO 10)
    FOR n := 1 (UPTO 10)

    all of them will lead to compiler errors. So it's not a problem of FOREACH :)

    .
    XSharp Development Team
    chris(at)xsharp.eu

    Please Log in or Create an account to join the conversation.

    Foreach errors 02 Oct 2022 15:07 #24088

    • Meinhard
    • Meinhard's Avatar


  • Posts: 73
  • Dick,

    >The second confusion is that AFAIK you can always add parentheses around part of your code. I just kept them from the C# sample, not realizing that in For Each they would cause a compiler error.

    you usually can put parentheses around expressions. But in the case of a for/foreach/if ... statement, they are part of statement definition in the language grammar, as you can see in :

    foreach_statement
    : 'foreach' '(' local_variable_type identifier 'in' expression ')'
    embedded_statement

    In the X# grammar it is:

    FOREACH [IMPLIED <idElement> | VAR <idElement> | <idElement> AS <idType>] IN <container>
    <Statements>...
    [EXIT]
    <Statements>...
    [LOOP]
    NEXT

    so no parentheses designated.

    BTW, when working with enumerables of 3rd party libraries (including the framework) I strongly recommend to use FOREACH instead of FOR..NEXT. If you use FOR..NEXT you need to know the implementation and your code is tied to this implementation. If the implementor decides to change the implementation your code might break.

    Regards
    Meinhard

    Please Log in or Create an account to join the conversation.

    Foreach errors 03 Oct 2022 13:39 #24092

    • ic2
    • ic2's Avatar
    • Topic Author


  • Posts: 1555
  • Hello Meinhard, Chris,

    Thanks for your explanations. Indeed parentheses aren't accepted in FOR..NEXT either. Not that I used it obviously (the parentheses in the FOR EACH where a leftover of C#) but I really thought you could add parentheses almost always & everywhere. Apparently not!

    About using ForEach in 3rd party enumerables: I agree on that. Only when I can query the number of elements to go through and also can 'move' to the next element easily (something like odb:Goto(aRec[ni]) I prefer FOR..NEXT because it is clearer what you are doing and you can also easier to debug on specific iterations e.g. when ni is 10.

    Dick

    Please Log in or Create an account to join the conversation.

    • Page:
    • 1