Foreach errors

This forum is meant for questions and discussions about the X# language and tools
Post Reply
ic2
Posts: 1858
Joined: Sun Feb 28, 2016 11:30 pm
Location: Holland

Foreach errors

Post 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
User avatar
Chris
Posts: 4911
Joined: Thu Oct 08, 2015 7:48 am
Location: Greece

Foreach errors

Post 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 ":"
Chris Pyrgas

XSharp Development Team
chris(at)xsharp.eu
ic2
Posts: 1858
Joined: Sun Feb 28, 2016 11:30 pm
Location: Holland

Foreach errors

Post 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
User avatar
Chris
Posts: 4911
Joined: Thu Oct 08, 2015 7:48 am
Location: Greece

Foreach errors

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

.
Chris Pyrgas

XSharp Development Team
chris(at)xsharp.eu
User avatar
Meinhard
Posts: 81
Joined: Thu Oct 01, 2015 4:51 pm

Foreach errors

Post 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
ic2
Posts: 1858
Joined: Sun Feb 28, 2016 11:30 pm
Location: Holland

Foreach errors

Post 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
Post Reply