xsharp.eu • Foreach errors
Page 1 of 1

Foreach errors

Posted: Sat Oct 01, 2022 8:23 pm
by ic2
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:

Code: Select all

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

Foreach errors

Posted: Sat Oct 01, 2022 9:35 pm
by Chris
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 ":"

Foreach errors

Posted: Sun Oct 02, 2022 4:58 am
by ic2
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

Foreach errors

Posted: Sun Oct 02, 2022 6:52 am
by Chris
Hi Dick,

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

Code: Select all

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 :)

.

Foreach errors

Posted: Sun Oct 02, 2022 1:07 pm
by Meinhard
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

Foreach errors

Posted: Mon Oct 03, 2022 11:39 am
by ic2
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