Posted: 20th Jun 2007 1:47
You could also global the array... don't know if that will help any. global dim is the syntax.
Posted: 20th Jun 2007 1:51
No, you only need to dim these things once. I put it at the top of the main source code file. It then works like a dynamic array, you don't need to redim it. All of the queue and stack stuff is DBPro iterating it for you. Its not needed at all, and amounts to fluff. I can't think of a reason to use them since all they do is try to insulate you from managing the iterator yourself. Since that iterator is no more than an integer, its just as easy to use a loop that protected itself from attempting to index invalid elements in the array. Given the fact that they needed fixing in the first place, and don't add any real functionality; I don't use them, but I still like the dynamic arrays. They are one of the best features of DBPro!

You first example had an invalid index value because it was not dimmed. All you need to do is dim it at the top of your main source file. I have used them this way on just about every bit of code I have, and that is over 10,000 lines right now. I have had no errors and they work great for all kinds of things.

Shiny - Not necessary, they are already global by default. just use dim arrayname() just like that.
Posted: 20th Jun 2007 6:29
...and since I had already DIMmed it GLOBAL in the first place, just not at the top of the main source file, I thought I had those bases covered. Guess not. Silly me.

Question: If I #INCLUDE a file - say, from the main source file - would the #INCLUDEd file get "processed" at the point which it was #INCLUDEd, or would it just be relegated to "just another file" status, like in my current example?
Posted: 20th Jun 2007 7:41
includes can only have functions in them and are added at the end of the main compiled file if I'm not mistaken...
Posted: 20th Jun 2007 9:37
Includes can have more than functions, but they go at the end of the main source file, just as if they were other source files in the project, not "in-place".

Here is a sample of what I mean. Its contrived, but I want to tell you that alot of people complained about array index valid as a range check, because it does not work very well as a range check, but array count does.

This first bit is the main source:

+ Code Snippet
dim players() as PLAYER

global i as integer

print "players() contains " + str$(array count(players()))
for i = 0 to 15
    array insert at bottom players()
    select i
        case 0
            players().name = "Alpha"
            players().number = 1024
            endcase
        case 1
            players().name = "Bravo"
            players().number = 11
            endcase
        case 2
            players().name = "Charlie"
            players().number = 64
            endcase
        case 3
            players().name = "Delta"
            players().number = 1024
            endcase
        case 4
            players().name = "Echo"
            players().number = 5
            endcase
        case 5
            players().name = "Foxtrot"
            players().number = 1
            endcase
        case 6
            players().name = "Golf"
            players().number = 334
            endcase
        case 7
            players().name = "India"
            players().number = 12
            endcase
        case 8
            players().name = "Juliet"
            players().number = 3
            endcase
        case 9
            players().name = "Kilo"
            players().number = 12235
            endcase
        case 10
            players().name = "Lima"
            players().number = 7
            endcase
        case 11
            players().name = "Mike"
            players().number = 698
            endcase
        case 12
            players().name = "November"
            players().number = 557
            endcase
        case 13
            players().name = "Oscar"
            players().number = 87
            endcase
        case 14
            players().name = "Papa"
            players().number = 709
            endcase
        case 15
            players().name = "Quebec"
            players().number = 3678
            endcase
    endselect
next i

print "players() contains " + str$(array count(players()))
PrintPlayers()
wait key
cls
array insert at element players(), 7
players(7).name = "Hotel"
players(7).number = 9999
PrintPlayers()
do
loop
end


I use the arrays by defining them and functions for accessing them in a separate file, usually with my "jz" brand on them. Here is the file that goes with the code above. You could put it with the other code, too.

+ Code Snippet
type PLAYER
    name as string
    number as integer
endtype

function PrintPlayers()
    local i as integer

    for i = 0 to 16
        if array count(players()) > -1
            if i <= array count(players())
                print players(i).name + " : " + str$(players(i).number)
            endif
        endif
    next i
endfunction



Notice the dim players() at the top of the main source. Also, notice that the type is defined (in my case) in a different file, and the functions that use it are there, too. In order for this to compile, the dim must be in the main file. After that, it is all gravy.

It will print out the array count before any elements are added, and then again after. Then, it will print the array elements. Notice that there is no "H" player. Press a key, and "Hotel" will be inserted at its correct position, and it will print out again.
Posted: 20th Jun 2007 15:03
You can also have a function or a subroutine accessed via gosub in your included file and run the initialisation of your DIMs and global variables there. I do this all of the time with no problems.

Anything you can put in your main source file can also be put in your includes - there are some issues with types being used before they are defined, but that's the only thing I'm aware of that limits you.
Posted: 21st Jun 2007 0:44
Ah. Got it. Just a slight difference in where items are placed as to when/how they are recognized, eh? I guess I can make that change to my thinking - but it's as un-natural as object-oriented app development was to me when I first encountered it.

Believe it or don't, this has been educational.
Posted: 21st Jun 2007 10:32
The type arrays just need to be initialized before any code that runs then. The locations of the type statements don't matter, but you need to initialize them first. I do the same thing as IanM with gosub.

It's just a matter of include order. You can alter it in CodeSurge, or you can swap them yourself in Notepad by opening the .dbpro file. Once my .dbpro file got rewritten and Geisha House wouldn't compile any more. All I did was swap the file order back to their original configuration and everything worked okay.

As for the array commands, I just tried to use the Array Delete Element command. It worked just fine. However, when I used Dim on that array again the program crashed. I had to go back use sorting the array, copying the contents to a temporary array, then dimming the original array back to smaller dimensions and copying the data back. I realize that this is what that command does internally anyway, but it was still annoying the time that it cost me.
Posted: 21st Jun 2007 11:39
...which is why I dim it once and only once at the top of the main source file. I read a post you made about how you use them, and also, IanM has showed this before. I have also seen that error before, but cannot remember where. This code does not have that error.

It is the same code as above, but...I put in two elements, "Hotel" and "OOPS". Then, I delete "OOPS" and then add "Romeo".

I don't think it does the same thing internally with respect to copying and redimming. At least it must not follow the same path...would my code crash similarly? (I mean, I don't know the exact nature of your code, but...here is how I would handle deleting and adding elements.) It is interesting to me how different this is; seems significant.

+ Code Snippet
REM Project: usingdynamicarrays
REM Created: 6/20/2007 12:51:07 AM
REM
REM ***** Main Source File *****
REM
type PLAYER
    name as string
    number as integer
endtype

dim players() as PLAYER

#constant MAX_PLAYER    = 16

global i as integer

print "players() contains " + str$(array count(players()))
for i = 0 to 15
    array insert at bottom players()
    select i
        case 0
            players().name = "Alpha"
            players().number = 1024
            endcase
        case 1
            players().name = "Bravo"
            players().number = 11
            endcase
        case 2
            players().name = "Charlie"
            players().number = 64
            endcase
        case 3
            players().name = "Delta"
            players().number = 1024
            endcase
        case 4
            players().name = "Echo"
            players().number = 5
            endcase
        case 5
            players().name = "Foxtrot"
            players().number = 1
            endcase
        case 6
            players().name = "Golf"
            players().number = 334
            endcase
        case 7
            players().name = "India"
            players().number = 12
            endcase
        case 8
            players().name = "Juliet"
            players().number = 3
            endcase
        case 9
            players().name = "Kilo"
            players().number = 12235
            endcase
        case 10
            players().name = "Lima"
            players().number = 7
            endcase
        case 11
            players().name = "Mike"
            players().number = 698
            endcase
        case 12
            players().name = "November"
            players().number = 557
            endcase
        case 13
            players().name = "Oscar"
            players().number = 87
            endcase
        case 14
            players().name = "Papa"
            players().number = 709
            endcase
        case 15
            players().name = "Quebec"
            players().number = 3678
            endcase
    endselect
next i

print "players() contains " + str$(array count(players()))
PrintPlayers()
wait key
cls
array insert at element players(), 7
players(7).name = "Hotel"
players(7).number = 9999
array insert at element players(), 7
players().name = "OOPS!!!!!"
players().number = 8888
PrintPlayers()
wait key
cls
array delete element players(), 7
PrintPlayers()
wait key
cls
array insert at bottom players()
players().name = "Romeo"
players().number = 8888
PrintPlayers()
wait key
do
loop
end

function PrintPlayers()
    local i as integer

    if array count(players()) > 0
        for i = 0 to array count(players())
            print players(i).name + " : " + str$(players(i).number)
        next i
    endif
endfunction


EDIT: I just realized that when I added "OOPS", I set its value using the current index and not an absolute one! Very interesting, indeed.

In fact, these cannot work like dynamic arrays of predefined types; it has to work differently. The compiler must be using pointers, and that means my suspicion about its similarity to STL is bolstered. Redimming them is a mistake if that is the case.