dcsimg
Results 1 to 1 of 1

Thread: Classic VB - What's wrong with Dim x, y, z As Long ?

  1. #1

    Thread Starter
    Lurker
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,651

    Arrow Classic VB - What's wrong with Dim x, y, z As Long ?

    If you have used C-style languages, you may be used to this notation:
    int x, y, z;
    which declares 3 variables x, y, and z of the type int.

    You may have also used this in VB.NET:
    Dim x, y, z As Long
    which declares the three variables of type Long.

    So it's only natural that you would expect to do the same in VB6 (or earlier). Unfortunately, pre VB.NET, the syntax does not mean the same thing. If you declare a variable without specifying a type, you are effectively declaring it as a Variant.
    Dim x ' x is a Variant

    Declaring multiple variables in the shorthand manner seen above will result in x and y typed as Variant, and only z as a Long, when you intended all three to be Long's.

    In Visual Basic.NET Variants were removed from the language altogether as they did not fit with the .NET type system. The statement Dim x in VB.NET actually declares x as an Object, but the behaviour of the shorthand declaration was changed so that all untyped variables are declared as the next type keyword that appears in the declaration statement. Just remember that this is NOT the case in Classic VB
    Last edited by penagate; Feb 17th, 2006 at 03:38 AM.

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width