|
-
Aug 2nd, 2000, 10:24 AM
#1
Thread Starter
Fanatic Member
Hey everyone. I would like to know if the following is possible.
I have a User Defined Type. This type has three strings in it. "Name", "Address", "Tel"
I then have an array of this type. The size is how many elements i am retrieving from a file.
What i want to do is pass an array of just the Names to a sub procedure.
Maybe some code will help to explain.
Code:
Private Type recordType
Name As String
Address As String
Tel As String
End Type
Dim custArray() As recordType
Private Sub cmdSort_Click()
Call sortArray(custArray().Name)
End Sub
Private Sub sortArray(sortFiled() As String)
'sort the array
End Sub
Any help is much appreciated. Thanks in advance
Regards,
Iain.
Iain, thats with an i by the way!
-
Aug 2nd, 2000, 10:36 AM
#2
Frenzied Member
Fraid not, you're gonna harve to redefine your function to cope with UDTs
-
Aug 2nd, 2000, 10:41 AM
#3
Thread Starter
Fanatic Member
Sorry Sam, i think i mislead you. I meant any help that i wanted to hear was much appreciated. 
Just kidding. Thanks for clarifying that. That's what i was afraid of though Sam. It is just it would have been easier that way. Back to the drawing board then.
Iain, thats with an i by the way!
-
Aug 2nd, 2000, 10:41 AM
#4
Lively Member
Maybe...
Prior to the call to the sort procedure create a string array and fill it with the names in the recType array.
Code:
Dim nameArray() as string
For i = 0 to UBound(custArray)
Redim Preserve nameArray(i) = custArray(i).Name
i = i + 1
Next i
Call SortArray(nameArray)
Good Luck.
-
Aug 2nd, 2000, 10:46 AM
#5
Thread Starter
Fanatic Member
Yeah i thought of that, but then the thought of transfering anywhere between 5,000, and 50,000 names told me not to.
Iain, thats with an i by the way!
-
Aug 2nd, 2000, 12:42 PM
#6
Lively Member
-
Aug 2nd, 2000, 01:29 PM
#7
Frenzied Member
The other point about it is that whan you put the contents of your UDT array into the Sting array and have it sorted by the function, how are you going to use it to sort your UDT, you'll need a function just as complicated as the original function.
so you're in fact nearly tripling the time it would take to sort the array, with 5000 odd records this isn't a good Idea.
-
Aug 2nd, 2000, 03:00 PM
#8
Frenzied Member
I'm sorry but I don't see what the problem is.
Dim custArray() is in scope for all procudures in the form so there is no need to pass any part of it as a parameter.
-
Aug 2nd, 2000, 03:11 PM
#9
transcendental analytic
I don't see the problem either, i think it's that you can't pass an array of UDT's without having a procedure for that UDT. It could be like
Code:
Private Sub sortArray(sortFiled() As recordType)
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Aug 2nd, 2000, 03:34 PM
#10
Thread Starter
Fanatic Member
I think you are all missing the point of what i am trying to do.
I wish to be able to pass any one of the three strings in the user defined type to the sorting function. The function will then sort on the array of strings that i have passed, thus saving me writing 3 seperate sorting algorithims, or using If statements.
Example code.
Code:
Private Type recordType
Name As String
Address As String
Tel As String
End Type
Dim custArray() As recordType
Private Sub cmdSort_Click()
Call sortArray(custArray().Name)
End Sub
Private Sub sortArray(sortFiled() As String)
'simple bubble sort to demonstarte
Dim i As Integer
Dim blSwap As Boolean
Dim tempRec As recordType
Do
blSwap = False
For i = LBound(custArray) To UBound(custArray) - 1
If sortFiled(i) > sortFiled(i + 1) Then
blSwap = True
tempRec = custArray(i)
custArray(i) = custArray(i + 1)
custArray(i + 1) = tempRec
End If
Next i
Loop Until blSwap = False
End Sub
Iain, thats with an i by the way!
-
Aug 2nd, 2000, 03:44 PM
#11
Frenzied Member
If you REALLY are going to have an array with 50000 elements in it I reckon you should use a database.
You can sort it however you want then and the database engine's sorting is a damn sight quicker than using VB!
Just a suggestion... 
-
Aug 2nd, 2000, 04:15 PM
#12
transcendental analytic
Ah, i got ya Iain, youre passing the string array; you have to pass it as a variant:
Code:
Private Sub sortArray(sortFiled as Variant)
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Aug 2nd, 2000, 04:33 PM
#13
Thread Starter
Fanatic Member
Nice idea Ked, but as it stands i get an error in the cmdSortBy_click() event.
Iain, thats with an i by the way!
-
Aug 2nd, 2000, 05:05 PM
#14
transcendental analytic
Hmmm, when i look closer, i think youre trying to accomplish the same i tried a few days ago, there's probably just no way out but coding a procedure for each UDT
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Aug 20th, 2000, 02:22 PM
#15
New Member
What you are doing is not impossible - but the way you are coding it is. You are misunderstanding the basic structures you have created!!
In memory your array of UDT will be stored as:
UDT(0) UDT(1) UDT(2)
Name:Address:Tel:Name:Address:Tel:Name:Address:Tel:
So as such the array of names doesn't exist separately.
Passing UDT().Name is nonesense.
If you want to store and pass each array separately then you should declare this structure:
Private Type recordType
Name() As String
Address() As String
Tel() As String
End Type
Then you can pass recordType.Name() which will work.
However I don't think this structure fits in with your problem.
Your original structure fits the problem - but defeats the objective of sorting. Because of the way the data is arranged you can have the 3 different elements sorted at the same time.
You can only have the recordType array sorted by Name, OR by Address, OR by Tel at any one time. You can't have these 3 states existing at the same time.
You can only have sorting if:
a. You split this structure up.
b. Have a heirarchy of sort keys - such as how Access deals with sorting using mulitple fields. Eg. You could sort by name first, then by address, then by Tel.
If you are happy at only having the UDT sorted by one of its elements at any one time then you would have to seek thought the original UDT destroying it as you go while recreating it in a sorted form by name for example.
Private Type recordType
Name() As String
Address() As String
Tel() As String
End Type
Dim custArray() As recordType
Private Sub cmdSort_Click()
Call sortArray(custArray(), 1)
End Sub
Private Sub sortArray(var() As Variant, intSortKey As Integer)
Dim custSortedArray() As recordType
Dim originalUDT() As recordType
Select Case intSortKey
Case 1
'sort orignalUDT by Name
Case 2
'sort orignalUDT by Address
Case 3
'sort orignalUDT by Tel
End Select
End Sub
If you have any problems in passing the UDT around then the CopyMemory API will solve it - but in which case you will have to make your strings fixed.
I have written apps. using this method extensively, so just shout if you have problems.
I can't be bothered typing the sort code but if you you do this bit I can help with using CopyMemory on the UDT.
holgrave.
-
Aug 20th, 2000, 03:12 PM
#16
Frenzied Member
holgrave
I don't quite get what you're saying.
By the looks of it you're trying to sort the udt by storing it as 3 seperate arrays and then sorting the name one.
How are you coping with the other 2? won't they be maisaligned?
I reckon it would be far easier to alter the function, you only need to alter it slightly if you have it as 2 seperate functions, just copy and paste it then change the name and a few lines of code of one of the functions.
-
Aug 20th, 2000, 03:35 PM
#17
New Member
Sam you've just made my point for me.
I was trying to say that Iian17 has two options.
1. Change the existing structure to have separate arrays for each element. Or
2. Pass the existing UDT whole sale to the sort function identifying which element he wanted the UDT by.
My code demonstrated 2 as I thought it was more in line with what Iian17 wants.
My way avoids misalignment probs - what Iian17 was trying to do before WOULD.
No alignment probs would currently occur as at the moment it shouldn't be necessary to resort to using CopyMemory or change the fundamental structure of the UDT.
hol.
-
Aug 20th, 2000, 08:01 PM
#18
Fanatic Member
Hi,
The problem from the first post is that you can't pass just one part of the udt. but then you're only passing a refference anyway so just pass the whole thing...
Code:
Private Type recordType
Name As String
Address As String
Tel As String
End Type
Private Enum Rec
TheName = 1
TheAddress = 2
TheTel = 3
End Enum
Dim custArray() As recordType
Private Sub cmdSort_Click()
Call sortArray(custArray, TheName)
End Sub
Private Sub sortArray(sortFiled() As recordType, Column as Rec)
select Case Column
case TheName
' sort by name
case TheAddress
'sort by address
end select
End Sub
You have to do this because "YOU NEED THE WHOLE ARRAY IN ORDER TO SORT IT" unless you're using some sorting algorithm I don't know.
What you should do is create another array as an index and rather than swap the orders of the actual records write the UDT array counter of whatever cloumn you sorted into the index then to loop the list in order (or binary search it)
Code:
For x = 1 to 50000
custArray(index(x)).name
next
Paul Dwyer 
Network Engineer
Aussie In Tokyo
Using Powerbasic 6 & VB6 SP4 (Please also add your VB Version to your signature!)
-
Aug 21st, 2000, 01:42 AM
#19
transcendental analytic
Hehehe, just admit it guys, you can't make a sortfunction to which you pass any UDT.
There's always a way around but as Iain said,
I wish to be able to pass any one of the three strings in the user defined type to the sorting function. The function will then sort on the array of strings that i have passed, thus saving me writing 3 seperate sorting algorithims, or using If statements.
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Aug 21st, 2000, 03:19 AM
#20
Fanatic Member
Well, you could always use a 2D array and enum the fields so it looked sort of like a UDT in code...
"Better Lame than Never!"
Or write your own object model that works sort of like a recordset (but then, your objective is to have less code so... maybe not) 
Paul Dwyer 
Network Engineer
Aussie In Tokyo
Using Powerbasic 6 & VB6 SP4 (Please also add your VB Version to your signature!)
-
Aug 21st, 2000, 09:43 AM
#21
New Member
I think we have wasted are brains on this long enough.
Next question:
Please can you tell me how to write a routine that loops through some data but I don't want to use For, Next, Do or Loop statements? And while your at it please can you change the laws of gravity.
hol.
-
Aug 21st, 2000, 10:10 AM
#22
Thread Starter
Fanatic Member
Sorry for not posting earlier, i have only just noticed that this old thread got dug up again.
Holgrave, nice ideas mate, but i think you have completely missed the point.
Paul, i know you cant sort a UDT with out the whole UDT, but the passing of one bit of the UDT as an array was just an idea i had. I now accept that it wont work.
I now unfortunately have many sorting functions.
Iain, thats with an i by the way!
-
Aug 21st, 2000, 01:23 PM
#23
transcendental analytic
HEhe, i knew Iain would reply that sooner or later 
But i think you could do some sort of sorting with copymemory, if you just specify which parts to be sorted
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Aug 21st, 2000, 02:05 PM
#24
New Member
-
Apr 4th, 2001, 08:52 PM
#25
Addicted Member
Y dont you just put the udt into a listview (set to report mode) then use a function tha sorts the list view by the column header that you clicked.
Then once you have sorted the listview just stick all the stuff back into the UDT and your done 
-|- Hurgh -|-
Email: [email protected]
Website: http://www.hurgh.org/
Unix, Linux, FreeBSD, OpenBSD, Solaris, Windows
C, C++, PHP, VB6, ASP, VBScript, JavaScript
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|