Struct by Index - Part 2 << Back



Check out Part 1 if you haven't done so.
It's not all that impressive to hardcode the index to access a struct member. Let's try to initialize the entire struct in a loop to see what happens.

Use UI

#COMMAND MoveStruct R "TO" R R
	#PUSH !g
	#SET G$ !4
	Move !1 To !3VI!g // It was !3CI!4
	#POP G$
#ENDCOMMAND

Struct Test
	Integer hello // Item 0
	Integer world // Item 1
End_Struct

Test t
Integer i
For i From 0 to 1
	MoveStruct (i+53) to t i
Loop
Showln t.Hello
Showln t.World
InKey FieldIndex

So, why change from CI to VI? If you read the documentation on #IFCLASS, you will notice that there are different types of integer. Each type of integer will generate different expression. For example, constant integer will generate the expression of |CI, local integer will generate |SI (S stands for Stack, which is not documented on the web site), and global integer (which is this example) will generate |VI.

Also you might have noticed the new #PUSH #SET #POP. Why are they there? A correct expression starts with the "|" character. If you combine two expressions together, you will get 2 "|" characters. All the #PUSH #SET #POP is to get will rid of the "|VI" part.

Next time we will look at how to write a generic version of MoveStruct that will work with constant, local, and global integer index. Stay tuned for part 3.

Free Web Hosting