Add a variable to a variable.

Started by Borislav, September 13, 2018, 17:29:51

Previous topic - Next topic

Borislav

Basically to make a variable variable.
Here is some pseudo-code

number = 54
var+number = 43 #var54

I want to get number from a for loop and I want to make loads of variables with their own specific names generated.


Holzchopf

Not possible that way because BMX is compiled and not interpreted, therefore the exact variable name has to be known at compile-time and can't be generated at runtime.

BUT..! You can use arrays instead:Local var:Int[100] ' var with 100 fields (indexed from 0 to 99)
number = 54
var[number] = 43 ' var at index 54

Borislav

Quote from: Holzchopf on September 13, 2018, 17:45:22
Not possible that way because BMX is compiled and not interpreted, therefore the exact variable name has to be known at compile-time and can't be generated at runtime.

BUT..! You can use arrays instead:Local var:Int[100] ' var with 100 fields (indexed from 0 to 99)
number = 54
var[number] = 43 ' var at index 54

You can't make an object in B3D using an array right?
I am actually trying to make an object using these variables.
I am basically making a value of objects and they are going to be numerated so I could specifically modify each one of them at any time.

Amanda Dearheart

Do you understand types Borisslav?

If I understand what you're asking, you might try something like this!

type MyObject
   Field ID : Int
   Field Another var  : Int
   Field Another var  : Int
end type


for Item = 1 to 100
    ThisObject.MyObject = New MyObject
    ThisObject.ID  = Item
next



In the rest of the code for your program you will reference the ID number of the type to as a handle to your object!
Prepare to be assimilated !  Resistance is futile!

Matty

Yes you can have an array of objects in blitz3d.

I believe declare the array as:

Dim myarray.mytype[n]

Although been so many years no idea of the exact syntax anymore or even whether b3d uses square brackets for arrays.

Holzchopf

Oh, is it B3D now..? :-\

IIRC either Dim myarray.mytype(n) or Local myarray.mytype[n] - the second one is called a blitzarray. They can be defined as local or global, be passed to or returned from functions but can't be multidimensional or be resized.

Borislav

#6
Thank you!

Borislav

Quote from: bsisko on September 14, 2018, 08:34:03
Do you understand types Borisslav?

If I understand what you're asking, you might try something like this!

type MyObject
   Field ID : Int
   Field Another var  : Int
   Field Another var  : Int
end type


for Item = 1 to 100
    ThisObject.MyObject = New MyObject
    ThisObject.ID  = Item
next



In the rest of the code for your program you will reference the ID number of the type to as a handle to your object!
But is that compatible with Blitz3D/MiniB3D objects?

fielder

#8
MiniB3d is for Blitzmax .. and this code is for Blitzmax
Blitz3D is not compatible with this code.. is a different language.

i suggest you to learn something about arrays... is the most simple thing to use for this kind of usages.

you can also do something like your pseudocode .. using files...

for example create a file called "var+number" and write into it an integer.... this is a VERY SLOW solution... but works.. i use this for example to add a thumbnail to a certain item...

i have an item called  652567256 taken from an array (or a barcode reader... or a lot of other things..)...  the software check if the file 652567256.jpg exist... if exist the image will be added to item description on main application :)

Quote from: Holzchopf on September 14, 2018, 09:23:42
Oh, is it B3D now..? :-\
Quote from: Matty on September 14, 2018, 09:03:11
Yes you can have an array of objects in blitz3d.
this forum is BlitzMax / BlitzMax NG i think is better to NOT publish B3d code on it.