Page 2 of 2 FirstFirst 12
Results 16 to 29 of 29
  1. #16
    Gold Lounger
    Join Date
    Dec 2000
    Location
    New Hampshire, USA
    Posts
    3,386
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Microsoft- a pet peeve

    The problem is what some have called "the evil of type coercion". This is a serious problem in VB/VBA.

    If somebody gives you code that uses Variant types, then it is not always so simple to explicitly type all variables without breaking some code. Try the following:

    <pre>Sub EvilCoercion()
    Dim SomeVariable
    Dim strDate As String
    Dim lngDate As Long
    Dim dblDate As Double


    SomeVariable = Date
    strDate = Date
    lngDate = Date
    dblDate = Date

    MsgBox SomeVariable & vbCr & _
    strDate & vbCr & _
    lngDate & vbCr & _
    dblDate

    SomeVariable = Time
    strDate = Time
    lngDate = Time
    dblDate = Time

    MsgBox SomeVariable & vbCr & _
    strDate & vbCr & _
    lngDate & vbCr & _
    dblDate


    End Sub</pre>


  2. #17
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Microsoft- a pet peeve

    Oh, I always specify my variable types too. I was just pointing out that it wasn't completely necessary in a snippet of code.
    Charlotte

  3. #18
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Microsoft- a pet peeve

    Actually, in a lot of Access code, you need to define the variable as a variant rather than an object if you're working with some kinds of collection in the For...Each expression. If you see code like
    <hr>For Each element in MyRecordsetCollection<hr>
    , you have to define element as a variant in order for it to accept the recordset object stored in the collection. If you define it as a recordset it won't work.
    Charlotte

  4. #19
    knoway
    Guest

    Re: Microsoft- a pet peeve

    That's interesting. I didn't know that.

    It is my understanding that the Variant storage contains internal information that defines the sub-type (based, presumably, on where the data came from originally).

    I wonder what the sub-type would be for your example? Hmmm[img]/w3timages/icons/thinks.gif[/img]

  5. #20
    knoway
    Guest

    Re: Microsoft- a pet peeve

    Hi Howard,

    Nice demo, but I am not sure what it proves.

    I am not 100% sure how Basic does it, but it is my understanding that datetime is held as a real number that records the elapsed time from an arbitary point (1/1/1980?).

    In your example, the variant will know that it is a date, and will format it (according to the active datetime coding mask) on output or when moved to a string variable.

    There is no way of specifying a coding mask for datetime when held in floating point variables, so Basic justs moves the real number. Unfortunately, single precision is not big enough to handle the number of significant digits required for time, and the variable will overflow. The double precision variable can cope right enough.

    It is interesting that Basic just seems to ignore the overflow condition and continues with an invalid value. From my previous lives, I would have expected some sort of exception error to have been raised. Something else I have learnt today. [img]/w3timages/icons/thinks.gif[/img]

    The bottom line is, I think, that the problem you are demonstrating is the restrictions of allowing Basic to do type conversion. If you know the real data types (Geoff's point), then you can use the correct type conversion functions.

  6. #21
    Gold Lounger
    Join Date
    Dec 2000
    Location
    New Hampshire, USA
    Posts
    3,386
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Microsoft- a pet peeve

    I was demonstrating that if you have code that uses the default Variant and you explicitly type variables you can get unexpected results.

    The Time and Date functions return a Variant(Date).

    Oh well, this will be my last posting in the Lounge for quite some time.

  7. #22
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Microsoft- a pet peeve

    Couldn't begin to guess, but you can create a collection, populate it with a recordset like all the fields for a particular keyID, and then use a for each element construction to reference each collection member. It gets a little tricky, but not too much.
    Charlotte

  8. #23
    Super Moderator WebGenii's Avatar
    Join Date
    Jan 2001
    Location
    Redcliff, Alberta, Canada
    Posts
    4,066
    Thanks
    2
    Thanked 5 Times in 5 Posts

    Re: Microsoft- a pet peeve

    What is "Hungarian Notation"?
    [b]Catharine Richardson (WebGenii)
    WebGenii Home Page
    Moderator: Spreadsheets, Other MS Apps, Presentation Apps, Visual Basic for Apps, Windows Mobile

  9. #24
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 28 Times in 27 Posts

    Re: Microsoft- a pet peeve

    Have a look at the following link for references:

    http://search.microsoft.com/us/dev/default...&radP=2&chkK=on

  10. #25
    Platinum Lounger
    Join Date
    Dec 2000
    Location
    Queanbeyan, New South Wales, Australia
    Posts
    3,730
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Microsoft- a pet peeve

    Hey, I like the sig

    Hungarian notation is a naming convention for variables- they are prefixed by (generally) a 3-characrter prefix indicating the data type- eg

    dim strCounter as Integer
    dim strTemp as string

    See:
    http://support.microsoft.com/support/kb/ar...s/Q173/7/38.ASP
    Subway Belconnen- home of the Signboard to make you smile. Get (almost) daily updates- follow SubwayBelconnen on Twitter.

  11. #26
    2 Star Lounger
    Join Date
    Dec 2000
    Location
    eastern Connecticut, Connecticut, USA
    Posts
    113
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Microsoft- a pet peeve

    Not to be picky... but shouldn't the first line in your example be:

    DIM intCounter as Integer
    OR
    DIM iCounter as Integer

    [img]/w3timages/icons/smile.gif[/img]

  12. #27
    3 Star Lounger
    Join Date
    Dec 2000
    Location
    Vancouver, Br. Columbia, Canada
    Posts
    268
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Microsoft- a pet peeve

    Geoff:

    You wrote:

    "There's some sample code in Excel which goes:

    "for each cell in ..."

    "When I first came across that, I couldn't find a type which worked, so I ended up coding..."

    I ran into the same thing, although it came from some guy on the comp.apps.spreadsheets ng - he had written a routine to do custom formatting in excel 5. It was quite cool, but he did the same thing: "for each Cell in WorkRange..."

    I figured that Cell was a keyword, and couldn't figure out why I couldn't re-create his code (at least, not in a way that worked). He was very patient in explaining the problem, and I appreciated that, and he was just a "volunteer" after all.

    I agree that it is galling not to have explicit declarations in all the MS help file code examples (and I really hate "myfile," "myvar," "mybutton," etc - they make "mepuke") and the stuff on the knowledgebase. It doesn't take much extra time, it is good programming practice, and the whole point is to teach something -sometimes it is useful to know just what sort of value is expected (or allowed) in each variable.

    Why doesn't the lounge have a "recommend this post" feature - your rant deserves a recommendation!

  13. #28
    Platinum Lounger
    Join Date
    Dec 2000
    Location
    Queanbeyan, New South Wales, Australia
    Posts
    3,730
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Microsoft- a pet peeve

    Whoops.

    The typographically challenged in me comes out again.
    Subway Belconnen- home of the Signboard to make you smile. Get (almost) daily updates- follow SubwayBelconnen on Twitter.

  14. #29
    Platinum Lounger
    Join Date
    Dec 2000
    Location
    Queanbeyan, New South Wales, Australia
    Posts
    3,730
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Microsoft- a pet peeve

    Hi,

    Thanks to all for the responses- negative and positive. I've really appreciated this forum, and the feedback one gets.

    The thread has gone as little big now- and I think anybody who wanted to contribute has done so, so I'm closing the thread.

    If you want to abuse me for it, open a new thread to say so- I'm new at this moderating stuff!
    Subway Belconnen- home of the Signboard to make you smile. Get (almost) daily updates- follow SubwayBelconnen on Twitter.

Page 2 of 2 FirstFirst 12

Posting Permissions

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