Multiple 'Dim's within a loop

I’ve been trying to get a Dim statment working within a loop, but failing so far.

A simple folderitem, like so :

Dim i As Integer
For i=1 to 10
Dim MyItem+Str(i) As FolderItem
Next

I’ve also tried using MyItem+i

Is there a way to use multple 'Dim’s in a loop ?

Thanks.

You can’t use a variable as the name (using “+Str(i)” is a problem). Inside the loop you could do:

Dim i As Integer
For i=1 to 10
Dim f As FolderItem
… do stuff with f
Next

If you want to keep track of the new variables, make an array outside the loop, then append your new variable to it at the end of the loop:

Dim i As Integer
Dim f() as FolderItem
For i=1 to 10
Dim tmpF As FolderItem
...do stuff with tmpF

f.append tmpF 'This will store tmpF in your f array
Next

'Now outside the loop, f() will contain all the tmpFs you created inside the loop.

I personally don’t like statements like this as they can easily lead to confusion, especially as f is often used for just a single folderitem.

I would rather use something like

Dim fList() as FolderItem

To be explicit enough.

Good point, Markus. In my defense, I was being lazy. :slight_smile:

I don’t see the point of using different names, since the variable only exists inside the loop. It’s not like you’ll have several variables or an array to use in the rest of your code.

To give you a little more background to this, I’ve been testing how I could setup a small map tile server. I wanted to create a loop that would load a given number of map tiles into memoryblocks, convert them to images and then use them when requested.

The number of tiles could easily exceed 10 though.

Not being able to use a variable as part of a name means potentially a lot of code. Im still pondering if there are more efficient methods of achieving this. A small sqlite table could hold the names of every tile available on the system, so I was hoping to loop through each tile name and load it in turn until all tiles are loaded.

a dictionary with keys that are the names

I can see how to use a dictionary for the map tile names, but not the Dim statements, unless Im missing something ?

Actually the variable exists outside the loop, but more importantly it is a coding standard that I developed for myself. It is a good idea to be consistent, especially when you tend to reuse code fragments by copying and pasting.

Dim listOfNamedTiles as new Dictionary Dim i As Integer For i=1 to 10 Dim theName as string= MyItem+Str(i) // .... do whatever work listOfNamedTiles.value( theName ) = whatever Next

its not directly the item its a name that you can use to look up the item in the dictionary

The code Im using right now to load a single tile is this :

[code] Dim MapMemoryBlock As New MemoryBlock(0) ’ Start with a memoryblock since binary loading is generally fast

Dim MapSource As BinaryStream ' This will be the route from the source file to the memoryblock

Dim MapItem As FolderItem ' Standard FolderItem where we define the on-disk file name

Dim MapImage As Picture

MapItem=GetFolderItem("sk38ne.tif") ' Set the on-disk file name

MapSource=BinaryStream.Open(MapItem, False) ' Connect the binarystream to the on-disk map file

MapMemoryBlock=MapSource.Read(MapSource.Length) ' Read in the data

MapSource.Close ' Close the binarystream after we are finished

MapImage=Picture.FromData(MapMemoryBlock) ' Set the MapImage as the contents of the memoryblock[/code]

I can put this in a loop easily enough, but the ‘MapImage’ needs to be defined with a different name each time, otherwise it will just overwrite the previously converted image. All of the Dim statements can be reused each time the code loops, with the exception of the MapImage. I started trying it out using the FolderItem as above, which didnt work, but only the MapImage needs a different name each time.

[code]
Dim listOfNamedTiles as new Dictionary
Dim i As Integer
For i=1 to 10
Dim MapMemoryBlock As New MemoryBlock(0) ’ Start with a memoryblock since binary loading is generally fast

Dim MapSource As BinaryStream ' This will be the route from the source file to the memoryblock

Dim MapItem As FolderItem ' Standard FolderItem where we define the on-disk file name

Dim MapImage As Picture

MapItem=GetFolderItem("sk38ne.tif") ' Set the on-disk file name

MapSource=BinaryStream.Open(MapItem, False) ' Connect the binarystream to the on-disk map file

MapMemoryBlock=MapSource.Read(MapSource.Length) ' Read in the data

MapSource.Close ' Close the binarystream after we are finished

MapImage=Picture.FromData(MapMemoryBlock) ' Set the MapImage as the contents of the memoryblock

listOfNamedTiles.value("mapImage" + Str(i) ) = mapImage

Next[/code]

The trick now is you NEVER use MapImage for anything else
All the images are accessible BUT only by name like

    listOfNamedTiles.value("mapImage0")

Or an array where you just use the index

[code]
Dim listOfNamedTiles() as Picture
Dim i As Integer
For i=1 to 10
Dim MapMemoryBlock As New MemoryBlock(0) ’ Start with a memoryblock since binary loading is generally fast

Dim MapSource As BinaryStream ' This will be the route from the source file to the memoryblock

Dim MapItem As FolderItem ' Standard FolderItem where we define the on-disk file name

Dim MapImage As Picture

MapItem=GetFolderItem("sk38ne.tif") ' Set the on-disk file name

MapSource=BinaryStream.Open(MapItem, False) ' Connect the binarystream to the on-disk map file

MapMemoryBlock=MapSource.Read(MapSource.Length) ' Read in the data

MapSource.Close ' Close the binarystream after we are finished

MapImage=Picture.FromData(MapMemoryBlock) ' Set the MapImage as the contents of the memoryblock

listOfNamedTiles.append mapImage

Next[/code]

and now you can access the picture using JUST the index

Oh i kinda see what you are doing now Norman. I didnt think about moving the image itself into the dictionary, just the tile name itself. That makes more sense now, thanks!

Worth reading or rereading if necessary
Introduction to Programming with Xojo PDF

User Guide Book 1: Fundamentals PDF