Serving the Quantitative Finance Community

 
User avatar
chocolatemoney
Topic Author
Posts: 322
Joined: October 8th, 2008, 6:50 am

VB/ VBA

June 4th, 2010, 7:05 am

HiLet's say I have a class "Position" with some properties "Name", "Currency", "Size(USD)", etc.I stick all the positions in a collection "Portfolio".Is there a way to write down a generic function that sorts the collection by different properties, for example first by name and then by currency?I am not interested in the sorting per se, but I'd like to know if there is a way to do something like that:Public Sub BlaBla () Call SortCollection (myPortfolio, "Name", "Currency")End SubPublic Sub SortCollection (ByVal MyCollection as Collection, ByVal PropertyIdLikeToSortBY as "Property", ...) bla bla bla bla Variable1 = MyCollection.Item(x).PropertyIdLikeToSortBYEnd sub In other words, is there a way to refer in an abstract way to an object property.I'm sorry if my explanation is not clear and if I did not use the proper terminology.Thanks a lot.
 
User avatar
daveangel
Posts: 17031
Joined: October 20th, 2003, 4:05 pm

VB/ VBA

June 4th, 2010, 7:07 am

the way I would do this in C or C++ is to pass a pointer to a comparator function to the sort function. In VBA I guess you could pass the name of the comparator and use the Run method ?
knowledge comes, wisdom lingers
 
User avatar
AlexesDad
Posts: 243
Joined: May 29th, 2009, 4:10 pm

VB/ VBA

June 25th, 2010, 12:29 pm

QuoteOriginally posted by: daveangelthe way I would do this in C or C++ is to pass a pointer to a comparator function to the sort function .... .. but you don't ACTUALY pass a pointer to a function when you're coding in C++ do you ?
 
User avatar
rmax
Posts: 6080
Joined: December 8th, 2005, 9:31 am

VB/ VBA

June 25th, 2010, 12:47 pm

I remember many years ago when dinosaurs walked the earth that I did do some code in VB6 using ObjPtr. It is an undocumented function, and had serious health warnings on it. There was a VarPtr function as well. I don't know if you could use that.Otherwise, I would store the properties in a collection and sort using that.
 
User avatar
Cuchulainn
Posts: 65000
Joined: July 16th, 2004, 7:38 am
Location: Ganymede

VB/ VBA

June 25th, 2010, 12:56 pm

QuoteOriginally posted by: rmaxI remember many years ago when dinosaurs walked the earth that I did do some code in VB6 using ObjPtr. It is an undocumented function, and had serious health warnings on it. There was a VarPtr function as well. I don't know if you could use that.Otherwise, I would store the properties in a collection and sort using that.6000 years ago of course.Do you remember the algae life forms in VB3?
65000
 
User avatar
quantmeh
Posts: 5974
Joined: April 6th, 2007, 1:39 pm

VB/ VBA

June 25th, 2010, 1:03 pm

what you're looking for is commonly called "reflection" in programming. so, google for something like "reflection VBA"
 
User avatar
rmax
Posts: 6080
Joined: December 8th, 2005, 9:31 am

VB/ VBA

June 25th, 2010, 1:04 pm

QuoteOriginally posted by: CuchulainnQuoteOriginally posted by: rmaxI remember many years ago when dinosaurs walked the earth that I did do some code in VB6 using ObjPtr. It is an undocumented function, and had serious health warnings on it. There was a VarPtr function as well. I don't know if you could use that.Otherwise, I would store the properties in a collection and sort using that.6000 years ago of course.Do you remember the algae life forms in VB3?Do I have to admit I do. But the coding I had done before then was all BBC Basic, 6502 and Fortran so I thought it was pretty cool this event based stuff......
 
User avatar
daveangel
Posts: 17031
Joined: October 20th, 2003, 4:05 pm

VB/ VBA

June 25th, 2010, 1:23 pm

QuoteOriginally posted by: AlexesDadQuoteOriginally posted by: daveangelthe way I would do this in C or C++ is to pass a pointer to a comparator function to the sort function .... .. but you don't ACTUALY pass a pointer to a function when you're coding in C++ do you ?what I do is entirely my own affair ... it harms no one else
knowledge comes, wisdom lingers
 
User avatar
AlexesDad
Posts: 243
Joined: May 29th, 2009, 4:10 pm

VB/ VBA

June 25th, 2010, 1:46 pm

QuoteOriginally posted by: daveangelQuoteOriginally posted by: AlexesDadQuoteOriginally posted by: daveangelthe way I would do this in C or C++ is to pass a pointer to a comparator function to the sort function .... .. but you don't ACTUALY pass a pointer to a function when you're coding in C++ do you ?what I do is entirely my own affair ... it harms no one elseI neither suggested that what yo do is not entirely your own affair or that what you do harms anyone. I simply asked for clarification of an assertion you made in a PUBLIC forum. However, given that you say you use function pointers in C++ AND what you do harms no one else, I can only assume you do not code for a living.
 
User avatar
chocolatemoney
Topic Author
Posts: 322
Joined: October 8th, 2008, 6:50 am

VB/ VBA

June 25th, 2010, 1:56 pm

Thank you guys for the feedback. Actually, my initial inquiry was rather simple: I have a class with several properties. I stick instances of such class in a collection.I have to sort the collection by (several) of those properties (ex class aMan, instances in the Employees collection, and I have to sort the collection by weight, wage, height, age, etc)I'd like to avoid writing a sub for each property I'd like to sort by; subs that would just differ by the if statement where the comparison is made: ex "if man1.age > man2.age then" vs "if man1.wage > man2.wage" etcI was just thinking about the most convenient solution and looking for hints...Thank you.
 
User avatar
daveangel
Posts: 17031
Joined: October 20th, 2003, 4:05 pm

VB/ VBA

June 25th, 2010, 1:58 pm

Quote I can only assume you do not code for a living. yes thank goodness. I leave that to chumps to like you.
knowledge comes, wisdom lingers
 
User avatar
AlexesDad
Posts: 243
Joined: May 29th, 2009, 4:10 pm

VB/ VBA

June 25th, 2010, 2:02 pm

QuoteOriginally posted by: daveangelQuote I can only assume you do not code for a living. yes thank goodness. I leave that to chumps to like you.I couldn't agree more, indeed thank goodness ! Now if only you left giving advice about coding to chumps too, the world would be a safer place yet still.
 
User avatar
daveangel
Posts: 17031
Joined: October 20th, 2003, 4:05 pm

VB/ VBA

June 25th, 2010, 2:07 pm

QuoteOriginally posted by: AlexesDadQuoteOriginally posted by: daveangelQuote I can only assume you do not code for a living. yes thank goodness. I leave that to chumps to like you.I couldn't agree more, indeed thank goodness ! Now if only you left giving advice about coding to chumps too, the world would be a safer place yet still.as you said its a free forum ... you dont need to follow my advice. so why don't you keep your nose out of my affairs ?
knowledge comes, wisdom lingers
 
User avatar
AlexesDad
Posts: 243
Joined: May 29th, 2009, 4:10 pm

VB/ VBA

June 25th, 2010, 2:19 pm

QuoteOriginally posted by: daveangelQuoteOriginally posted by: AlexesDadQuoteOriginally posted by: daveangelQuote I can only assume you do not code for a living. yes thank goodness. I leave that to chumps to like you.I couldn't agree more, indeed thank goodness ! Now if only you left giving advice about coding to chumps too, the world would be a safer place yet still.as you said its a free forum ... you dont need to follow my advice. so why don't you keep your nose out of my affairs ?"WHY ??" Are you serious ?? Because .... IT'S A PUBLIC FORUM .... your comments here cease to be protected from criticism and ridicule ! So it's my prerogative to put my nose into any affairs made public here. Moreover I feel morally compelled to warn against your HARMFUL advice you fool.
 
User avatar
daveangel
Posts: 17031
Joined: October 20th, 2003, 4:05 pm

VB/ VBA

June 25th, 2010, 2:28 pm

QuoteOriginally posted by: AlexesDadQuoteOriginally posted by: daveangelQuoteOriginally posted by: AlexesDadQuoteOriginally posted by: daveangelQuote I can only assume you do not code for a living. yes thank goodness. I leave that to chumps to like you.I couldn't agree more, indeed thank goodness ! Now if only you left giving advice about coding to chumps too, the world would be a safer place yet still.as you said its a free forum ... you dont need to follow my advice. so why don't you keep your nose out of my affairs ?"WHY ??" Are you serious ?? Because .... IT'S A PUBLIC FORUM .... your comments here cease to be protected from criticism and ridicule ! So it's my prerogative to put my nose into any affairs made public here. Moreover I feel morally compelled to warn against your HARMFUL advice you fool.I am not asking for my comments to be protected .. I see you still haven't come up with a suggested solution. I would suggest that you are simply a troll.so F O A D - and if you dont know what this means PM me and I will let you know.
knowledge comes, wisdom lingers