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 / What are Types?

Author
Message
OrzeL
14
Years of Service
User Offline
Joined: 20th Oct 2009
Location:
Posted: 31st Dec 2009 07:57
Okay so whenever i load up codesurge it has functions, labels and types. I understand what labels and functions are but what is Types ? When would i use Types? and what are they useful for?
Veron
17
Years of Service
User Offline
Joined: 22nd Nov 2006
Location:
Posted: 31st Dec 2009 10:54 Edited at: 31st Dec 2009 10:55
Search next time - http://forum.thegamecreators.com/?m=forum_view&t=87212&b=7

Not such a good tutorial, I remember there being a better one around here. Search around and see what you find.

Cliff Mellangard 3DEGS
Developer
18
Years of Service
User Offline
Joined: 20th Feb 2006
Location: Sweden
Posted: 31st Dec 2009 15:07
Quote: "Okay so whenever i load up codesurge it has functions, labels and types. I understand what labels and functions are but what is Types ? When would i use Types? and what are they useful for?"

Its very usefull when you nead to simplifi your code for merging sections with other projects etc

iam using it like this in my current code.

type AI_DATA TILE_ID as integer SEARCH as integer X_ORIGIN as integer Y_ORIGIN as integer endtype
type AI_DATA2 START_X as integer START_Y as integer END_X as integer END_Y as integer EXIST as integer endtype
type AI_DATA3 SIZE_X as integer SIZE_Y as integer endtype
AI_PATH as AI_DATA2
AI_GRID as AI_DATA3

AI_GRID.SIZE_X = 16
AI_GRID.SIZE_Y = 10
undim AI_GRID(AI_GRID.SIZE_X,AI_GRID.SIZE_Y)
dim AI_GRID(AI_GRID.SIZE_X,AI_GRID.SIZE_Y) as AI_DATA

from dbps help file.......
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.
Van B
Moderator
21
Years of Service
User Offline
Joined: 8th Oct 2002
Location: Sunnyvale
Posted: 31st Dec 2009 16:17
Just to add to the excellent explanations already posted, I find that typed arrays are much neater, and easier to work with when you want to take more control over data.
For instance, it's easier to add to an array that is typed because you only have to handle 1 array. So inserting into and deleting from a typed array cuts down the workload and makes that sort of data management straightforward.


Health, Ammo, and bacon and eggs!
OrzeL
14
Years of Service
User Offline
Joined: 20th Oct 2009
Location:
Posted: 31st Dec 2009 19:21
Wow thank you, I understand it now,it seems very useful.
Cliff Mellangard 3DEGS
Developer
18
Years of Service
User Offline
Joined: 20th Feb 2006
Location: Sweden
Posted: 1st Jan 2010 16:46 Edited at: 1st Jan 2010 16:47
Quote: "Wow thank you, I understand it now,it seems very useful. "

Always nice to help when i actually can do so
I constantly see my self as an newbie and pretty stupid when it comes to coding
But thats why we have the tgc community to help each other .
NeX the Fairly Fast Ferret
19
Years of Service
User Offline
Joined: 10th Apr 2005
Location: The Fifth Plane of Oblivion
Posted: 1st Jan 2010 20:00
How well handled are typed arrays under DB? Is it storing each item as a pointer or literally as a huge block of data?

Athlon64 2.7gHz->OC 3.9gHz, 31C, MSi 9500GT->OC 1gHz core/2gHz memory, 48C, 4Gb DDR2 667, 500Gb Seagate + 80Gb Maxtor + 40Gb Maxtor = 620Gb, XP Home
Air cooled, total cost £160
Zotoaster
19
Years of Service
User Offline
Joined: 20th Dec 2004
Location: Scotland
Posted: 1st Jan 2010 21:27
NeX,

Huge block of data. Well, that's what I'm guessing anyway. If it used pointers, then at least it would allow you to pass objects by reference, which of course you can't do.

"everyone forgets a semi-colon sometimes." - Phaelax

Login to post a reply

Server time is: 2024-09-28 14:19:15
Your offset time is: 2024-09-28 14:19:15