Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Difficult Question

I'm almost done writing my program, but I'm stuck at one point. It works with TYPE commands. Some variables are grouped together using subscripts, like



type playerinfo

hitpoints as integer

mana as integer

end type



To actually use this, I need to do



dim shared myvariable (amount) as playerinfo



Then I can access it

myvariable(subscript).hitpoints = value



Right? I have that down, but that's not my problem. The question I have relates to the variable above, amount. With each and every call to my subprocedure, the amount variable changes. One call, it could be 20, another one, it could be 300. I need some way to redimension the memory that the variables uses. At sub call, one variable is passed, a number, in long format. My math inside the sub takes over, and based on what it does, it returns a value in the variable 'total'. This variable total is how many 'things' can be done with this number. Once this has been computed, I need to somehow run the command "DIM myarray(0 to total) as mytype". The DIM statement can't be run from a sub/function, so I need to know how to run it, and how to allocate the needed memory (using DIM) once the variable "total" has had it's new value computed.



I think that's it. If this is unclear, please post, and I will try to make it clearer. Please submit any ideas on how to do this, no matter how stupid/outrageous you think they may be. At this point, I am willing to try almost anything. I do have Qbasic 4.5 on my computer, so if what I need is inside the QB 4.5 language, I can use it. I can also get my hands on Qbasic 7.1 PDS, if need be.



Thank you very much.



mercman2000


Comments

  • Greetings, Mercman2000.



    Your question is rather difficult, but you don't need to stress over it. While a re-write in C++ would be faster, and you can allocate memory on the spot, I'm going to give you an idea that's just crazy enough to work. I don't have your code, so I wouldn't be able to help you program this, I can only give you general ideas.



    First off, run some tests, and figure out what the highest value will be when you call your SUB. Take this value, and dimension your array referring to your TYPE structure to this value, such as 3000. It seems like you only have two integers, so it shouldn't take up too much memory. Once you have this max value, DIM your array to it, then go back into your sub routine. Before the value is configured, wipe the slate clean, with a FOR statement.

    for x=1 to maxarraysize

    myarray(x).variable = -1

    next x



    Now, compute your total,



    Stuff the variables computed to their respective places, then have QB take a look at your array. With another for statement, check each of your values, comparing them to -1. If the value is anything but -1, then you know that slot has been filled. Upon reaching -1, you know that's where your number of things to do with the number stops. Take this new value, and use it in the next part of your program to do whatever the rest of the code is doing.



    Good Luck

    VyroIngo


  • First in the main module use

    REDIM shared myvariable (amount) as playerinfo



    then in the SUB or FUNCTION you can

    REDIM myvariable (amount) as playerinfo



    note in the sub don't use the shared keyword again

    it's not needed and qb will give an error if you do.



    QB71 also has REDIM preserve if you need to retain the values that are allready in the array



    hope this helps. If not email me I will help if I can





    : I'm almost done writing my program, but I'm stuck at one point. It works with TYPE commands. Some variables are grouped together using subscripts, like

    :

    : type playerinfo

    : hitpoints as integer

    : mana as integer

    : end type

    :

    : To actually use this, I need to do

    :

    : dim shared myvariable (amount) as playerinfo

    :

    : Then I can access it

    : myvariable(subscript).hitpoints = value

    :

    : Right? I have that down, but that's not my problem. The question I have relates to the variable above, amount. With each and every call to my subprocedure, the amount variable changes. One call, it could be 20, another one, it could be 300. I need some way to redimension the memory that the variables uses. At sub call, one variable is passed, a number, in long format. My math inside the sub takes over, and based on what it does, it returns a value in the variable 'total'. This variable total is how many 'things' can be done with this number. Once this has been computed, I need to somehow run the command "DIM myarray(0 to total) as mytype". The DIM statement can't be run from a sub/function, so I need to know how to run it, and how to allocate the needed memory (using DIM) once the variable "total" has had it's new value computed.

    :

    : I think that's it. If this is unclear, please post, and I will try to make it clearer. Please submit any ideas on how to do this, no matter how stupid/outrageous you think they may be. At this point, I am willing to try almost anything. I do have Qbasic 4.5 on my computer, so if what I need is inside the QB 4.5 language, I can use it. I can also get my hands on Qbasic 7.1 PDS, if need be.

    :

    : Thank you very much.

    :

    : mercman2000

    :






Sign In or Register to comment.