Figured it out, but another question

 DevX Home Today's Headlines   Articles Archive   Tip Bank   Forums

# Thread: Figured it out, but another question

1. Victoria miwok742hotmail.com Guest

## Figured it out, but another question

Ok I figured it out, with help, I have another question(listed after the code)

Dim numbers(1 To 5)

Private Sub Form_Load()

numbers(1) = "1/1/2000"
numbers(2) = "1/1/2001"
numbers(3) = "1/1/2002"
numbers(4) = "1/1/2003"
numbers(5) = "1/1/2020"

End Sub

Private Sub Command1_Click()

For i = 1 To UBound(numbers) - 1
For j = 1 To UBound(numbers) - 1
If numbers(j) < numbers(j + 1) Then
temp = numbers(j)
numbers(j) = numbers(j + 1)
numbers(j + 1) = temp
End If
Next
Next

End Sub

Private Sub Form_Load()

numbers(1) = "1/1/2000"
numbers(2) = "1/1/2001"
numbers(3) = "1/1/2002"
numbers(4) = "1/1/2003"
numbers(5) = "1/1/2020"

End Sub

My question is what if I had a 2 Dimension array:

Dim numbers(1 To 3, 1 To 2)

Private Sub Form_Load()

numbers(1,1) = "1/1/2001"
numbers(1,2) = "50"
numbers(2,1) = "1/1/2005"
numbers(2,2) = "60"
numbers(3,1) = "1/1/2002"
numbers(3,2) = "70"

End Sub

How can I perform a bubble sort on that array just using the date portions
(the first part)?

Thanks,

Vic

2. Arthur Wood Guest

## 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

then

numbers(1) = #1/1/2000#
numbers(2) = #1/1/2001#
numbers(3) = #1/1/2002#
numbers(4) = #1/1/2003#
numbers(5) = #1/1/2020# ( by the way, these are already sorted, so you aren't
really making sure that your method REALLY wordk correctly...)

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)

Option Explicit
Private Type MyType
DatePart As Date
NumberPart As Integer
End Type

Private Sub cmdTest_Click()

Dim Values(5) As MyType

Values(1).DatePart = #1/1/2000#
Values(1).NumberPart = 10
Values(2).DatePart = #1/1/2010#
Values(2).NumberPart = 22
Values(3).DatePart = #1/1/2004#
Values(3).NumberPart = 9
Values(4).DatePart = #1/1/2003#
Values(4).NumberPart = 43
Values(5).DatePart = #1/1/2002#
Values(5).NumberPart = 18

'Now you can do a Bubble Osrt on the array Values:

Dim Switched As Boolean
Dim iVal As Integer
Dim Temp As MyType

Switched = False

Do
Switched = False
For iVal = 1 To UBound(Values) - 1

If Values(iVal + 1).DatePart < Values(iVal).DatePart Then
Temp = Values(iVal)
Values(iVal) = Values(iVal + 1)
Values(iVal + 1) = Temp
Switched = True
End If
Next
Loop Until Not Switched
End Sub

That is how you do a bubble sort with dates, and multiple value types...

Arthur Wood

"Victoria" miwok742hotmail.com wrote:
>
>Ok I figured it out, with help, I have another question(listed after the

code)
>
>Dim numbers(1 To 5)
>
>Private Sub Form_Load()
>
>numbers(1) = "1/1/2000"
>numbers(2) = "1/1/2001"
>numbers(3) = "1/1/2002"
>numbers(4) = "1/1/2003"
>numbers(5) = "1/1/2020"
>
>End Sub
>
>Private Sub Command1_Click()
>
>For i = 1 To UBound(numbers) - 1
> For j = 1 To UBound(numbers) - 1
> If numbers(j) < numbers(j + 1) Then
> temp = numbers(j)
> numbers(j) = numbers(j + 1)
> numbers(j + 1) = temp
> End If
> Next
>Next
>
>End Sub
>
>Private Sub Form_Load()
>
>numbers(1) = "1/1/2000"
>numbers(2) = "1/1/2001"
>numbers(3) = "1/1/2002"
>numbers(4) = "1/1/2003"
>numbers(5) = "1/1/2020"
>
>End Sub
>
>My question is what if I had a 2 Dimension array:
>
>Dim numbers(1 To 3, 1 To 2)
>
>Private Sub Form_Load()
>
>numbers(1,1) = "1/1/2001"
>numbers(1,2) = "50"
>numbers(2,1) = "1/1/2005"
>numbers(2,2) = "60"
>numbers(3,1) = "1/1/2002"
>numbers(3,2) = "70"
>
>End Sub
>
>How can I perform a bubble sort on that array just using the date portions
>(the first part)?
>
>Thanks,
>
>Vic

3. Rick Rothstein Guest

## 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

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

4. Arthur Wood Guest

## 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
>

5. Patrick Ireland Guest

## 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.

Pat

#### Posting Permissions

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

 FAQ Latest Articles Java .NET XML Database Enterprise
 Questions? Contact us. C++ Web Development Wireless Latest Tips Open Source