Results 1 to 1 of 1

Thread: Classic VB - What is datatype coercion?

  1. #1

    Thread Starter
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    Classic VB - What is datatype coercion?

    Datatype coercion is a feature of Visual Basic that can't be found in many other programming languages. It allows you to use different datatypes against each other without you bothering much about making sure the datatypes can actually be compared against each other.

    For example, you can do this:

    VB Code:
    1. Dim strTemp As String
    2.  
    3.     ' coercion happens here: 500 is automatically converted to "500"
    4.     strTemp = 500
    5.  
    6.     Dim intTemp As Integer
    7.     intTemp = 500
    8.  
    9.     ' coercion happens here again: strTemp is now automatically converted to an Integer value
    10.     If strTemp = intTemp Then MsgBox "Datatype coercion occurred.", vbInformation

    This makes using of the programming language far easier, but it has it's downfalls as well. Sometimes there can be situatations when coercion doesn't know how to work and you get an error:

    VB Code:
    1. Dim strTemp As String
    2.     Dim intTemp As Integer
    3.     intTemp = 500
    4.     If strTemp = intTemp Then MsgBox "Datatype coercion occurred.", vbInformation

    If you try running this code, you get error 13: type mismatch, as an empty string cannot be converted into a number.


    What you should do is to make sure you are comparing same datatypes against each other. Using the same datatypes against each other is also much faster as VB doesn't need to do coercion in that case. The resulting compiled code will also be shorter and much more efficient as there will be no need for the extra conversion code (which is invisible to you).

    So, we are comparing numbers here. Lets make the string a number:

    VB Code:
    1. If Val(strTemp) = intTemp Then ...

    Please note that coercion still takes place here: Val() returns a double value. You could also add CInt() surrounding the Val(). You can't use CInt directly, because strTemp isn't counted as a number and thus CInt would give you the same type mismatch error when the string is empty.

    Although it would be the best if you didn't use a string in the first place. You could plan things like this:
    • visual layer: convert to strings when displaying something to the user and convert values given by user back to proper datatype when done with the visual side
    • "under the hood" layer: everything possible is handled as numbers using the best suited datatype for each case (except native string data, of course)
    • output layer: if you need to save, then convert to the format you wish to use for your save files


    This will prevent getting errors and weird bugs, and makes sure your code is efficient.


    A quick guide to the most used datatypes

    Integers
    Byte = 8 bits unsigned integer, 0 to 255
    Integer = 16 bits signed integer, -32768 to 32767
    Long = 32 bits signed integer, -2147483648 to 2147483647
    Currency = 64 bits hacked signed integer, can have four decimal numbers

    Integer numbers are always accurate.

    Floating point
    Single = 32 bits floating point
    Double = 64 bits floating point

    Floating point numbers aren't perfectly accurate, but give a possibility to have a lot of decimals. Never use for money calculations!

    Special
    Boolean = 16 bits, True or False
    Date = 64 bits floating point (actually a Double)
    String = 32 bits + byte length of string + null character
    Last edited by Merri; Nov 27th, 2005 at 06:53 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width