If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Re: Figured it out, but another question

Victoria,
YOu were EXTREMELY lucky in the choice of dataes that you used to test
you alogorithm, because this would fail, badly, with real dates...that is
dates other than 1/1/YYYY. YOur arrys (which you called numbers) is declared
using

Dim numbers(1 to 5)

is in fact an array of type VARIANT (those may look, to you, as if they are
dates, but to the computer they are actually STRINGS - they just happen to
look like dates) and they are actually being stored by Visual Basic as VARAINT
types - a VARIANT is a variable type which can hold ANY kind of value.

If you wanted this routine to act on REAL dates, you would need to do this:

Dim numbers(1 to 5) as Date ' this declares that you will be using the special
DATE data type

in the above the # symbols serve to inform Visual Baisc to translate the
1/1/2000 into its internal representation of a DATE

in VB, a Date variable is actually a FLOATING POINT number where the whole
part of the number (to the left of the decimal point) is the number of days
betweenDec 31, 1899 and the date you used, and the fractional part of the
number (to the right of the decimal point) is the fraction of a day (number
of milli-seconds /86400000 ) since midnight on the day indicated (a date
like 1/1/2000 is treated as if it were MIDNIGHT - the start of that date
- so the fractional part would therefore be .000000)

in the second example, yoy have a different problem, since the array does
NOT in fact contain the same kinds of values in the two dimensions. So the
approach that you have take will NEVER work properly, since you want DTAES
in the first dimension, and Numbers in the second...but an array is BY DEFINITION,
made up of the SAME KIND of values in ALL of its dimensions.

YOu cannot have an array where the values in one dimesuion are one kind of
thing (dares) and in another dimension, a different kind of value (numbers).

Form what you have shown, while this problem CAN be solved, it would appear
that you are something of a beginner, and the solution is very much different
than anything that you have tried.

The simplest approach is to declare YOUR OWN TYPE of value (called a User
DEfined Type or UDT) , which has two parts - a Date part, and a Number part
- and the declare an array of THAT User-Defined Type (single dimension)

Re: Figured it out, but another question

> is in fact an array of type VARIANT (those may look, to you, as if they are
> dates, but to the computer they are actually STRINGS - they just happen to
> look like dates) and they are actually being stored by Visual Basic as VARAINT
> types - a VARIANT is a variable type which can hold ANY kind of value.
>
> If you wanted this routine to act on REAL dates, you would need to do this:

Since Variants are being used, he could have done this

numbers(1) = CDate("1/1/2000")
numbers(2) = CDate("1/1/2001")
etc.

in order to have stored real dates in his original arrray.

> in the second example, yoy have a different problem, since the array does
> NOT in fact contain the same kinds of values in the two dimensions. So the
> approach that you have take will NEVER work properly, since you want DTAES
> in the first dimension, and Numbers in the second...but an array is BY DEFINITION,
> made up of the SAME KIND of values in ALL of its dimensions.

That's not true for arrays defined as Variants. Each element of a Variant array can be
different. Give this code a try

Re: Figured it out, but another question

Rick,
I was not aware of that, though when I think about it, what you say makes
sense (it was late, last night, when I answered Victoria's question). However,
that reall does not address her question, and I doubt that she was even aware
that she was unsing VARIANT types, any way. I avoid VARIANTS at all costs,
because of the performance hit, and that was the real reason for my saying
what I said. And by using variants, the sort algorithm for a multiple dimensioned
array becomes quite a bit more obtuse - having to keep the additional columns
in sync is not always obvious - where the use of a UDT makes everyting behave
as one would expect, and it becomes almost trivial to extend to many "columns"
(additional fields in the UDT), and even allows for the columns to be in
an arbitrary order - the suggested code would work, without change, if the
UDT were changed to:

Private Type MyType
NumberPart as Integer
DatePart as Date
End Type

where using the Multiple dimensioned array of variants (with the meanings
of the COLUMNS swithched) would require significant re-write of the sort
routine.

Arthur Wood

"Rick Rothstein" <rick_newsgroup@email.com> wrote:
>> is in fact an array of type VARIANT (those may look, to you, as if they
are
>> dates, but to the computer they are actually STRINGS - they just happen
to
>> look like dates) and they are actually being stored by Visual Basic as
VARAINT
>> types - a VARIANT is a variable type which can hold ANY kind of value.
>>
>> If you wanted this routine to act on REAL dates, you would need to do
this:
>
>Since Variants are being used, he could have done this
>
> numbers(1) = CDate("1/1/2000")
> numbers(2) = CDate("1/1/2001")
> etc.
>
>in order to have stored real dates in his original arrray.
>
>
>> in the second example, yoy have a different problem, since the array does
>> NOT in fact contain the same kinds of values in the two dimensions. So
the
>> approach that you have take will NEVER work properly, since you want DTAES
>> in the first dimension, and Numbers in the second...but an array is BY
DEFINITION,
>> made up of the SAME KIND of values in ALL of its dimensions.
>
>That's not true for arrays defined as Variants. Each element of a Variant
array can
>be
>different. Give this code a try
>
> Dim X As Integer
> Dim Val(1 To 4) As Variant
> Val(1) = "ABC" ' String
> Val(2) = 12345 ' Integer
> Val(3) = 67.89 ' Double
> Val(4) = CDate("1/23/2001") ' Date
> For X = 1 To 4
> Debug.Print TypeName(Val(X))
> Next
>
>
>Rick
>

Re: Figured it out, but another question

Arthur,

Your points are well made. But Victoria needs to be aware that dates
are not sorted by Month-Day-Year as her example showed. Rather they
are sorted by Year-Month-Day. This is automatically done if you use
a Date type, however, if you use a string time as in her original example
the data would not be sorted correctly. She must either convert the
string to a date via the mentioned CDate() or reorder the string so that
it is in Year-Month-Day order. Of course, the year must be fully specified
as a 4 digit number.