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.

Code Snippets / [DBP+Matrix1Utils] Dark Variants

Author
Message
RiiDii
19
Years of Service
User Offline
Joined: 20th Jan 2005
Location: Inatincan
Posted: 22nd Jul 2009 09:07
Store almost any data in almost any variable type!

Numbers and String values can be passed around and through each other. The only one restriction is that numeric data cannot be stored in a string variable.


===================================================================================

Command:
Return Value = _Write <value> As_Variant
Stores the value provided, regardless of data type, to the Return Value, regardless of data type. See Return Value for restriction.

Parameters:
<value> can be any value or variable of any data type (excluding Doubles). See Return Value for restriction.

Return Value:
Can be any variable of any data type. Restriction: Cannot be a String data type variable if the <value> provided is numeric.

===================================================================================


Demo code:



Now, you may look at that and wonder - is this a DLL? Nope! Other than using IanM's Matrix1Utils, this is coded in Dark Basic Pro.

Download the .rar file and open up the demo. You will see the code from above. Run the demo to see that it does work. Feel free to play with the command (there is only one).

After you try to wrap your brain around how this was done, go ahead and open up the Dark Variants.dba file to be amazed.

For those that do not have IanM's Matrix1Utils installed, and don't want to, I have included a compiled executable.


Open MMORPG: It's your game!

Attachments

Login to view attachments
tiresius
21
Years of Service
User Offline
Joined: 13th Nov 2002
Location: MA USA
Posted: 22nd Jul 2009 17:59
This is cool on a geeky level, but is it a good idea to try to make Darkbasic an untyped language?

I'm not a real programmer but I play one with DBPro!
RiiDii
19
Years of Service
User Offline
Joined: 20th Jan 2005
Location: Inatincan
Posted: 22nd Jul 2009 19:49
Perhaps don't think of it as removing other types, but rather adding a universal type. For example: Arrays can store any data type without having to declare UDT's. It adds flexibility. It doesn't remove anything.

I am working on a more complete version of this (without the restrictions).


Open MMORPG: It's your game!
RiiDii
19
Years of Service
User Offline
Joined: 20th Jan 2005
Location: Inatincan
Posted: 23rd Jul 2009 01:16 Edited at: 23rd Jul 2009 01:19
Here is a more complete version (I still have ideas on improving this).


================================================================================================
New Data Type
Variant
Usage: Use when declaring a variant variable.

Example: Dim VariantList(10) as Variant
Example: TempVariant as Variant

Note: The Variant data type is a DWord data type and the two data types are interchangeable. However, for good programming practices, using the Variant data type when declaring a variant will help in understanding the intention of the variant variable.

================================================================================================
New Command:
DV_Cast, DV_[DataType], To_[DataType]

Usage:
Return Value = DV_Cast DV_[DataType] <value> To_[DataType]

Transforms the <value> provided based on the input [DataType] and Output [DataType], and stores the result in the Return Value.

Command structure:
DV_Cast
Use the DV_Cast command to begin the command sentence.

DV_[DataType]
Use the DV_DataType to indicate the type of input value that follows. Valid DataType Commands are: DV_Variant, DV_DWord, DV_Integer, DV_Float, and DV_String.

<value>
This can be any value of any data type (excluding doubles). The data type of this value must match the preceding DV_[DataType] command. Using a data type other than that specified by the DV_DataType command may cause errors or a crash.

To_[DataType] This command indicates the data type for the return value. This ensures the output value matches the data type of the Return Value. Valid DataType Commands are: To_Variant, To_DWord, To_Integer, To_Float, and To_String.

Return Value:
This can be any data type variable, but it must match the To_[DataType] command. The value returned will be appropriate for the To_[DataType]. Using other data types will likely cause errors or a crash.

Example 1:
MyVariant as Variant
MyFloat as Float
MyString as String
MyInteger as Integer

MyFloat = 1.23
MyVariant = DV_Cast DV_Float MyFloat To_Variant
MyString = DV_Cast DV_Variant MyVariant To_String
MyInteger = DV_Cast DV_String MyString To_Integer

================================================================================================

New Function:
Return Integer = _DV_Get_Variant_Type(Value as Variant)
Return String = _DV_Get_Variant_Type$(Value as Variant)

The first form of this function returns a numeric value indicating the Variant data type; 1=DWord, 2=Integer, 3=Float, 4=String, 0=Unknown

The second form of this function returns a string indicating the Variant data type.

In both functions, the provided variant must be a valid variant or the results will be 0 or "Unknown"

================================================================================================

New Function:
_DV_Clean_Variant(Value as Variant)

This function frees the memory locations of the variant in memory. When a Variant is re-assigned a value, regardless of the data type, a new memory location is created. The only way to prevent this from becoming a memory leak is to clean up variants using this function prior to assigning a variant a new value. This function can be used safely with an unassigned variant (a null variant).

================================================================================================

Code snippet.



Open MMORPG: It's your game!

Login to post a reply

Server time is: 2024-05-04 23:08:25
Your offset time is: 2024-05-04 23:08:25