Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Newcomers DBPro Corner / Referencing a TYPE data type variable with an unknown number in the name

Author
Message
roswell1329
14
Years of Service
User Offline
Joined: 12th Apr 2010
Location:
Posted: 23rd Sep 2014 01:11
If you create a TYPE data type with a variable that has a number in it, is there a way to reference that name later if the number is unknown?

Below is what I tried which didn't work:

WickedX
15
Years of Service
User Offline
Joined: 8th Feb 2009
Location: A Mile High
Posted: 23rd Sep 2014 01:44
Something like this?



roswell1329
14
Years of Service
User Offline
Joined: 12th Apr 2010
Location:
Posted: 23rd Sep 2014 02:10
Not quite. I'm looking for a way to call each of the name# variables without specifying the number at the end of the name like this:



The idea, is to print out all of the names for each "thingy" in the array.
WickedX
15
Years of Service
User Offline
Joined: 8th Feb 2009
Location: A Mile High
Posted: 23rd Sep 2014 02:19
Since you explicitly name the variables name1, name2 and name3, you cannot index them as such. This is the reason everybody wants arrays in UDT’s. Unless someone beats me to it, I’ll try to work something out.

WickedX
15
Years of Service
User Offline
Joined: 8th Feb 2009
Location: A Mile High
Posted: 23rd Sep 2014 03:33
The honey do’s, kind of eat up my time for tonight. I’m working on a completely dynamic system. Maybe tomorrow.

roswell1329
14
Years of Service
User Offline
Joined: 12th Apr 2010
Location:
Posted: 23rd Sep 2014 16:45
Please don't spend your time on this on my behalf. I was asking because I preferred using a UDT to an array for a specific function for the sake of clarity, but it works perfectly well with the array. You answered my question already -- you cannot dynamically create the name of a UDT variable on-the-fly, and I thank you very much for that!
Rick the Programmer
20
Years of Service
User Offline
Joined: 23rd Mar 2004
Location: Maryland
Posted: 23rd Sep 2014 16:46 Edited at: 23rd Sep 2014 17:18
I would do something like this with a Case/Select-



Here's the minimalist snippet of the same thing


There is no way, that I know of, to directly call Type data with a variable as you are describing. The compiler would assume that the "y" was a part of the variable's name and not a separate value to be appended to the "name" variable name. The compiler would look for the "namey" variable every time, and never look to substitute the value of the "y" variable in place of the y in "namey". The closest you'll find to this functionality is with arrays, i.e. name(y). As was mentioned earlier, DarkBASIC does not allow for arrays to be defined within User Defined Types(yet?- we all wish)

If we could use arrays in UDTs, you'd be able to get away with something like this, but alas, it is not possible



Your initial workaround was well-conceived, but you have to reference all UDT data verbatim as far as I know. Your code has the compiler looking for the "attribute$" data in your type, which doesn't exist. The element names of a UDT are separate from even Global Variables, in my experience. You could have a "Global ID as integer" declared but still be able to use a Level(x).ID variable since it is declared within your UDT.

Ask not for whom the bell tolls;
It tolls for ye!
Ortu
DBPro Master
17
Years of Service
User Offline
Joined: 21st Nov 2007
Location: Austin, TX
Posted: 23rd Sep 2014 17:16 Edited at: 23rd Sep 2014 17:31
This is somewhat messy, but it does give you the functionality you are looking for using a multidimensional typed array:



I am not sure that I would recommend this though, the whole purpose of being able to iterate over the types by numerical index is if you don't know the types. In the example given, the types name1, name2, name3 aren't really different types, they are just different items in a list of similar items, I am not sure why you would need to access them in the manner you are requesting and not just do something like:



I guess the question is, does it really matter that "Sam" is exactly "name1" and "Sally" is exactly "name2" or do you really just need to know that both Sam and Sally are names within level 1?

roswell1329
14
Years of Service
User Offline
Joined: 12th Apr 2010
Location:
Posted: 23rd Sep 2014 17:30
Thanks Rick and Ortu! Even if the way I was thinking isn't possible, it's cool to see several different answers to the same problem!
Rick the Programmer
20
Years of Service
User Offline
Joined: 23rd Mar 2004
Location: Maryland
Posted: 23rd Sep 2014 18:26
You're welcome

Programming is a lot like Yoshi Valley in Mariokart - There are many ways, some better than others, to achieve your goal... and if you're reckless you fall off the sides XD

Ask not for whom the bell tolls;
It tolls for ye!

Login to post a reply

Server time is: 2024-11-23 14:07:45
Your offset time is: 2024-11-23 14:07:45