dcsimg
Results 1 to 8 of 8

Thread: Silly Question (But I'll Ask Anyway)

  1. #1

    Thread Starter
    Member
    Join Date
    Nov 2018
    Posts
    59

    Silly Question (But I'll Ask Anyway)

    Why does VB not throw a "Data Type mismatch" error in the following?
    Code:
    Option Explicit
    
    Private Sub Command1_Click()
        
        Dim myLong As Long
        Dim myString As String
        
        myLong = 5
        myString = myLong
        
    End Sub

  2. #2
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    18,002

    Re: Silly Question (But I'll Ask Anyway)

    VB does vartype conversions for us. If the conversion fails, an error will be generated. Most things can be converted to a string value, but not everything.

    for example:
    Code:
    Dim myVariant As Variant
    Dim myString As String
    
    myVariant = Null
    myString = myVariant
    Conversions can cause other types of errors besides type-mismatch, i.e., Overflow if converting a value > 32767 to Integer
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  3. #3

    Thread Starter
    Member
    Join Date
    Nov 2018
    Posts
    59

    Re: Silly Question (But I'll Ask Anyway)

    OK thanks LaVolpe

    I turned test code around, and got the error I expected (VB can't convert "abc" to a Long).
    Code:
    Option Explicit
    
    Private Sub Command1_Click()
        
        Dim myLong As Long
        Dim myString As String
    
        myString = "abc"
        myLong = myString
        
    End Sub
    I was trying to demonstrate one of the important reasons of using Option Explicit and declaring all variables explicitly
    to a young person here in the office, and my demonstration code did not generate the error I expected.

    He looked at me like "Ya, you really don't know what your talking about, do you?"
    That's probably true!
    Last edited by mms_; Mar 17th, 2019 at 09:38 AM.

  4. #4
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    18,002

    Re: Silly Question (But I'll Ask Anyway)

    Here are some examples...

    This example shows unexpected results when variables not type-declared (whether Option Explicit used or not)
    Code:
     Dim v, s
       s = "1.5"
       v = 2
       MsgBox v & " > " & s & " = " & (v > s) & vbCrLf & _
            s & " < " &  v & " = " & (s < v)
    Now if v were declared with a numeric vartype, you wouldn't get the incorrect results. Try it, declare v As Long

    Here's a gotcha with not using Option Explicit
    Code:
     ' since don't have to, we won't declare variables
        For iTemp = 1 to 1000
          If iTemp = 250 Then iTmp = 500
        Next
    No errors in above code, but whatever that loop's intent was, failed. Since no Option Explicit, the typo of using iTmp instead of iTemp defeated the "If" statement
    Last edited by LaVolpe; Mar 17th, 2019 at 11:35 AM.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  5. #5
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    33,314

    Re: Silly Question (But I'll Ask Anyway)

    One bit of advice: NEVER show anything to a programming neophyte without testing it ahead of time. The code gods will inflict confusion upon your over-confident attempt, such that the simplest example will go awry to your great discomfort.

    My brother-in-law tried to show me a simple two or three line snippet in C++....and it failed for a totally trivial reason. Simple examples are dangerous that way.
    My usual boring signature: Nothing

  6. #6
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    12,743

    Re: Silly Question (But I'll Ask Anyway)

    In VB.Net they have added an option to set Option Strict when turned on it will throw an error when you attempt to assign miss matched variable types without an explicit conversion statement.

  7. #7

    Thread Starter
    Member
    Join Date
    Nov 2018
    Posts
    59

    Re: Silly Question (But I'll Ask Anyway)

    LaVolpe
    I tried declaring v as Long in your first example... still get wrong results (although both show TRUE)

    I ran second example both ways (using Option Explicit and NOT using Option Explicit)... get same results both ways (500); the expected result
    Code:
    Private Sub Command1_Click()
     ' since don't have too, we won't declare variables
        For iTemp = 1 To 1000
          If iTemp = 250 Then iTmp = 500
        Next
        Debug.Print iTmp
    End Sub
    Code:
    Option Explicit
    
    Private Sub Command1_Click()
     ' DECLARE variables because we have to
     Dim iTemp As Long
     Dim iTmp As Long
        For iTemp = 1 To 1000
          If iTemp = 250 Then iTmp = 500
        Next
        Debug.Print iTmp
    End Sub
    Shaggy's point? , or did I do something wrong?
    Last edited by mms_; Mar 17th, 2019 at 11:21 AM.

  8. #8
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    18,002

    Re: Silly Question (But I'll Ask Anyway)

    mms_ oops, didn't format the 2nd line of the msgbox correctly
    Should be:
    Code:
       MsgBox v & " > " & s & " = " & (v > s) & vbCrLf & _
            s & " < " & v & " = " & (s < v)
    The booleans were correct, just forgot to swap the string characters s & v. Fixed in original post

    Edited: for the 2nd example, I was just trying to highlight that without Option Explicit, typos may not be caught. In that example, iTmp should have been iTemp (was missing letter e). Also, I said that we were not going to declare variables, i.e., Option Explicit is not used. If iTmp were iTemp as intended, then the loop would go from 1 to 250, skip to 500+1 and continue. iTemp would have never had values between 251 & 500 inclusively if no typo.

    When typos are not caught, unexpected results and/or errors are common, at run-time, not necessarily during compilation.
    Last edited by LaVolpe; Mar 17th, 2019 at 11:43 AM.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

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