|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
What am I missing??property of those objects. Here is the code to know IF I need to swap 2 elements... if cdbl(cCol(i).Balance) < cdbl(ccol(i + 1).Balance) then SwapColItems(i, i+1, cCol) The SwapColItems Sub looks like this (pared down) Public Sub SwapColItems(ByVal i1 As Integer, ByVal i2 As Integer, ByRef cCol As clsLiabilitiesCol) Dim sTemp As String sTemp = cCol(i1).Balance cCol(i1).Balance = cCol(i2).Balance cCol(i2).Balance = sTemp End Sub When I run this, and step through, I see the values being swapped. I am using the Command Window to verify the collection(1).Balance = "3000" and collection(2).Balance = "5000", then I step into the swap routine, and at the end I see that cCol(i1).Balance = "5000" and cCol(i2).Balance = "3000", which is good. Then, right after it exits out of this sub, those values have been reversed again!?!?! I know I'm doing something stupid, but I can't for the life of my think of what it is! Can anyone spot what I'm doing wrong? I know I'm forgetting something stupid, but I've checked the obvious things I can think of (byval vs. byref, which shouldn't matter in this case because the only values that need to be preserved are in the cCol class variable, and as I understand things it doesn't matter if you pass it byval or byref since it points to an object pointer on the stack, and the data on the heap, or whatever -- stacks and heaps make my ass twitch). Any and all help is appreciated. Matt Byref vs Byval is my first guess
Native Data types like Integer, String, Boolean and so on are by default referenced by value Classes are refrenced byref. Byref means that VS passes a pointer to the original objekt when you call a function. when you use Byval a copy of the Data Type's value is passed to the function. If the two elements you are swapping are integers like in the example then change from Byval to Byref. If it still isnt working then try using the System.Integer class you can read more on byref in VS help byref vs byval: http://www.developerfusion.co.uk/show/1763/3/ Show quoteHide quote "YYZ" <matt.da***@gmail.com> wrote in message news:1138288958.035221.148180@g49g2000cwa.googlegroups.com... > Short version: I've got to a collection of objects based on a certain > property of those objects. Here is the code to know IF I need to swap > 2 elements... > > if cdbl(cCol(i).Balance) < cdbl(ccol(i + 1).Balance) then > SwapColItems(i, i+1, cCol) > > The SwapColItems Sub looks like this (pared down) > Public Sub SwapColItems(ByVal i1 As Integer, ByVal i2 As Integer, > ByRef cCol As clsLiabilitiesCol) > Dim sTemp As String > sTemp = cCol(i1).Balance > cCol(i1).Balance = cCol(i2).Balance > cCol(i2).Balance = sTemp > End Sub > > When I run this, and step through, I see the values being swapped. I > am using the Command Window to verify the collection(1).Balance = > "3000" and collection(2).Balance = "5000", then I step into the swap > routine, and at the end I see that cCol(i1).Balance = "5000" and > cCol(i2).Balance = "3000", which is good. > > Then, right after it exits out of this sub, those values have been > reversed again!?!?! I know I'm doing something stupid, but I can't for > the life of my think of what it is! > > Can anyone spot what I'm doing wrong? I know I'm forgetting something > stupid, but I've checked the obvious things I can think of (byval vs. > byref, which shouldn't matter in this case because the only values that > need to be preserved are in the cCol class variable, and as I > understand things it doesn't matter if you pass it byval or byref since > it points to an object pointer on the stack, and the data on the heap, > or whatever -- stacks and heaps make my ass twitch). > > Any and all help is appreciated. > > Matt > > Byref vs Byval is my first guess That was mine, too, but in this case, the integers that I'm passingaren't the values I'm swapping. Think of it as an array (kinda) -- I'm passing the indexes of the array, say items 1 and 2, and telling the function to swap the values of array(1) and array(2) -- so the 1 and 2 aren't going to change...so byref/byval shouldn't matter. Assume the array is made up of strings. arr(1) = "First" and arr(2) = "Second". I want to make arr(1) = "Second" and arr(2) = "First". Did that make more sense? Matt My bad -- it wasn't anything obvious. The classes that I'm changing
are actually pass-throughs to an SDK that actually controls the data, and while swapping all the values of the class, I was swapping the item number, which means that my data was being moved, then moved back. In actuality, the way to switch all the values is to just switch the item number, which I didn't realize. Much easier than I was making it. Sorry to post when the problem was with me and not a question of programming. Matt
ANN: Free PDF books for registered users of Visual Studio 2005
Batch update to table from datagrid Create Access ODBC DSN ? Syntax not accepted on my version of VS why ?. need ideas - caching strategy RS 2005: Use a Custom Class as DataSource? beta 3 New 2 dotnet but Im a vb6 programmer remoting in net Keydown on load |
|||||||||||||||||||||||