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.

2D All the way! / Stupid Question About DataStructures

Author
Message
LuciferX
20
Years of Service
User Offline
Joined: 16th May 2003
Location: United States
Posted: 18th Jul 2003 22:17
I have a question about how to arrange the data in my program,
I know it is not classbased, but i need to have a setup like this::

Class AnimationDataType

String Name
array of 10 ints
int loopstart
int numberOfFrames

then i need to declair a variable of this type called WalkingAnimation

Can anyone show me some pointers on this and how to acess the data later?

Sorry for the stupid question, i cannot afford the book
Do or do not, there is no try. -Yoda
zenassem
20
Years of Service
User Offline
Joined: 10th Mar 2003
Location: Long Island, NY
Posted: 18th Jul 2003 23:25 Edited at: 18th Jul 2003 23:26
Luciferx,

It depends on if you have DBpro or Dbclassic v. 1.13. I can'tr remember which you are using.

Under DBpro you can use the "type" command.

This is exactly like a struct command in c++.

Unforutnately you do not get the encapsulation that you would get in a class under C++.

I usually put the type outside of all functions and make it global. I know that this is a "no!no!" under C++ object oriented design, but i believe that in darkbasic it is the best way to handle it.

I have included the the documentation from the help file of dbpro. It explains types better or as good as any example i could make up.



USER DEFINED TYPES

If the current set of datatypes is inadequate for your needs, you can can create your own data types
using user-defined-type. User defined types are useful for storing data using logical fields rather than the
unfriendly list of subscripts used by arrays.

To create a user defined type, you must first declare it at the top of your program. To do so, you would
give your type a name and a list of fields it contains:

TYPE MyType
Fieldname1
Fieldname2
Fieldname3
ENDTYPE
The above code creates a type called MyType with three fields contained within it. As the fields have no
declaration, they are assumed to be integers. The same code could also be truncated to a single line like
so:

TYPE MyType Fieldname1 Fieldname2 Fieldname3 ENDTYPE
To use your type, you simply create a variable and declare it with your new type. To declare a variable as
a specific type, you would use the AS statement:

MyVariable AS MyType
You can then assign data to your variable as normal, with the added bonus of the fields you have given
your variable like so:

MyVariable.Fieldname1 = 41
MyVariable.Fieldname2 = 42
MyVariable.Fieldname3 = 43
At the moment, the type is assuming our fields are integers. We may wish to declare our fields as a real
number, string or other datatype. We can do so using the same AS statement within the type definition. So
the following code makes more sense we shall give our type and fields sensible names:

TYPE AccountEntryType
Number AS INTEGER
Name AS STRING
Amount AS FLOAT
ENDTYPE
You can use a type like any other, so creating and using an array of the above is simply a case of declaring
the array with your new type:

DIM Accounts(100) AS AccountEntryType
Accounts(1).Number=12345
Accounts(1).Name="Lee"
Accounts(1).Amount=0.42
As you will eventually discover you can have types within types for more complex data structures so we
can imagine one of the fields contains more than one value. We would define two user defined types, and
then use one of them in the declaration of one of the fields of the section user defined type, as follows:

TYPE AmountsType
CurrentBalance AS FLOAT
SavingsBalance AS FLOAT
CreditCardBalance AS FLOAT
ENDTYPE
TYPE AccountEntryType
Number AS INTEGER
Name AS STRING
Amount AS AmountsType
ENDTYPE
DIM Accounts(100) AS AccountEntryType
Accounts(1).Number=12345
Accounts(1).Name="Lee"
Accounts(1).Amount.CurrentBalance=0.42
Accounts(1).Amount.SavingsBalance=100.0
Accounts(1).Amount.CreditCardBalance=-5000.0
As you can see, user defined types are not only powerful, they make the readability of your programs far
easier. Using named fields instead of a subscript value within an array, you can save yourself many hours
all for the sake of an incorrect subscript value throwing out your program results.

Login to post a reply

Server time is: 2024-02-27 20:05:18
Your offset time is: 2024-02-27 20:05:18