[RESULVED] Tied up in Strings...
The code:
<code>
Const NumStrings As Integer = 10
Dim A(NumStrings) As String
Dim i As Integer, j As Integer, T As String
FillArrayFromFile(A, NumStrings, "FILE.EXT")
For i = 1 to NumStrings - 1
For J = i + 1 to NumStrings
If A(j) < A(i) Then
T = A(i)
A(i) = A(j)
A(j) = T
End If
Next j
Next i
</code>
results in some strings in A being duplicated at the expense of others.
I presume that expressions such as T = A(i) and A(i) = A(j) are implementd by moving a pointer to the actual string, because if I replace them with something like T = Mid(A(i), 1, Len(A(i)) the problem goes away. It is very inelegant, however. Is there a simpler, more direct way to accomplish this?
Why did MS do this to us programmers? They went to great lengths to extend Object Oriented concepts throughout the language, add great exception handling, fix a large number of problems from VB5/6, and then muck about with simple substitution. Sigh. :cry:
RussCanada
Re: Tied up in Strings...
Just out of curiosity, why are you startin at i=1? .NET is always 0 based, so it seems like you never compare the first string with anything. Is that intentional?
Also, Mid is a VB6 function, and is discouraged in these parts. Substring is the .NET variant.
Look up String.Copy, that't the member function you want.
You are right that string holds a pointer to an object. All strings are pointers to character arrays, I think, so this isn't really a surprise. You would expect that T=A(i) would create a new copy of the string, but that isn't the case. Lots of little things like that in here. Most copying in .NET is shallow, and you need to specifically implement deep copies for any most objects. That has been done with the Copy() method, for the string class.
Re: Tied up in Strings...
Quote:
Originally Posted by Shaggy Hiker
Just out of curiosity, why are you startin at i=1? .NET is always 0 based, so it seems like you never compare the first string with anything. Is that intentional?
Also, Mid is a VB6 function, and is discouraged in these parts. Substring is the .NET variant.
Look up String.Copy, that't the member function you want.
You are right that string holds a pointer to an object. All strings are pointers to character arrays, I think, so this isn't really a surprise. You would expect that T=A(i) would create a new copy of the string, but that isn't the case. Lots of little things like that in here. Most copying in .NET is shallow, and you need to specifically implement deep copies for any most objects. That has been done with the Copy() method, for the string class.
Sorry. I made up the code on the fly.
As for Mid and other "grandfathered" routines, it would be useful and instructive if MS would put such suggestions into the Help. But perhaps they did and I'm simply blind to it. But I really think that T = A(i) ought to produce a copy, what you imply to be a "deep copy" and make the programmer have to do something extra to get a "shallow" copy. Why turn fifty years of experience into mush? :rolleyes:
Thanks for the help!
RussCanada
Re: [RESULVED] Tied up in Strings...
I agree about the deep copy issue. I was pretty annoyed by that when I first found out about it. C++ gives you the syntax for a copy constructor that allows you to make a deep copy, but .NET does not (though there is an interface that helps with it). Copy constructors have been around for so long I don't see why they couldn't have added one.
I do remember seeing a page somewhere in the help file about legacy functions and their replacements. Naturally, this doesn't seem to be directly accessible through the legacy function itself (which would be convenient), but it is in there somewhere.....and it isn't all that valuable once you find it.
Re: [RESULVED] Tied up in Strings...
All it would take would be something along the lines of:
"It is suggested that you use String.Whatever() instead of LegacyFunction()."
In addition, a link could be provided for the function String.Whatever()
RussCanada
Re: [RESULVED] Tied up in Strings...
Come on, the documentation is FAR worse than that. When I first got into .NET, I thought the documentation was the worst I had ever seen. There were several index items for which there was no information at all. You find the PERFECT topic and....oops....blank sheet. It has gotten better over time, but it still exists in some places.