What is the reasoning behind having to specify a data type when looping through objects in a for each loop. If you have declared your array of a specific data type then wouldn’t the computer know the data type? Is it used for arrays with objects of different data types or something? If I only use one data type for certain array, it seems pointless to me that I have to specify the data type I am looking. THIS IS NOT VERY IMPORTANT but I am just curious to know because of the kind of project that I am working on.
Thanks in advanced btw.
You can use variant in your loop if you prefer. Technically Xojo could of course support auto typing like C++ does it in latest versions.
Maybe you fill a feature request in Feedback?
maybe like this:
dim a() as string
for each x as elementtype(a) in a
[quote=24266:@Christian Schmitz]You can use variant in your loop if you prefer. Technically Xojo could of course support auto typing like C++ does it in latest versions.
Maybe you fill a feature request in Feedback?
maybe like this:
dim a() as string
for each x as elementtype(a) in a[/quote]
It is not a problem for me but I was just eager to know because for my project, I think it would be useful to know the logic behind things like this. Btw I was thinking of being able to write something more like this:
dim a() as string
for each x in a
Rather than:
dim a() as string
for each x as elementtype(a) in a
Thanks btw.
You may not want the type in the array
for each obj As MySuperClass in ArrayOfMySubClasses
Xojo is very explicit about it’s typing, Variants and few implicit castings are the exception.
[quote=24267:@Oliver Scott-Brown]It is not a problem for me but I was just eager to know because for my project, I think it would be useful to know the logic behind things like this. Btw I was thinking of being able to write something more like this:
dim a() as string
for each x in a
Rather than:
dim a() as string
for each x as elementtype(a) in a
Thanks btw.[/quote]
You can also use a variable declared outside the loop:
dim a(), x as string
for each x in a
The syntax, for each x as y in a, declares a new variable, x, as opposed to using an already defined variable. Otherwise, it would be a “this name does not exist” situation. The compiler needs to know explicitly what you’re doing, rather than make an assumption that you want to declare a new variable.