Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Strange happenings with dates in collections

Status
Not open for further replies.

Mike Lewis

Programmer
Jan 10, 2003
17,490
10
38
Scotland
www.ml-consult.co.uk
Just came across something I can't understand.

Consider this code:

Code:
FOR lnI = 1 TO MyCollection.Count
  loObj = MyCollection.Item(lnI)
  ? VARTYPE(loObj.dStart)
    && dStart is a date property of the object, so
    && this correctly displays "D"
ENDFOR

That works as expected. But now consider this:

Code:
FOR EACH loObj IN MyCollection
   ? VARTYPE(loObj.dStart)
     && dStart is still a date property. But this
     && now displays "T"
ENDFOR

You would think both of these constructs would be identical. But in the first code, the date property is correctly recognised as a date, but in the second, VFP treats it as a datetime.

This is in VFP 9.0, SP1. The collection is a native VFP collection of objects, not a COM collection.

Very rum.

Mike



__________________________________
Mike Lewis (Edinburgh, Scotland)

My Visual FoxPro site: www.ml-consult.co.uk
 
I've solved it.

The answer is FOXOBJECT. This needs to be added to the FOR ... EACH. It convinces VFP that these are native Foxpro objects, not COM objects. Without that, VFP seems to want to treat all date peroperties as datetimes.

Now I think about it, I have come across this before, but it's not the easiest thing to remember.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

My Visual FoxPro site: www.ml-consult.co.uk
 
Yep. Wrote about this one, too, in the looping article (which should be in the second issue of FoxRockX).

The truth is you want FOXOBJECT not only because it makes things work (also a good choice <g>), but because it speeds things up, too.

Tamar
 
Thanks for that, Tamar.

In my present project, I'm making extensive use of VFP collections, and especially FOR .. EACH. I'll get into the habit of doing FOXOBJECT with all of them.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

My Visual FoxPro site: www.ml-consult.co.uk
 
Yeah, I've been working on a project with a lot of collections and I've had to work to train myself to always use FOXOBJECT, but it's worth it.

Hmm, probably should do something in IntelliSense for this.

Tamar
 
True, but I use native collections a lot more than COM objects, at least at the moment, so it might be worth having to delete FOXOBJECT when it's a COM object. Alternatively, I could give myself two different IntelliSense keys, once for native and one for COM.

Tamar
 
Tamar,

Good points. I'd go with the first of those two suggestions. In fact, I've tried to avoid using FOR EACH with COM objects, after the debacle I mentioned in thread1251-1321695 ("Looping through COM collections with FOR EACH") and which I still haven't sussed.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

My Visual FoxPro site: www.ml-consult.co.uk
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top