Totally did not think about last comma and with no validating or error handling should have been more along the lines of
I could send you some code files I wrote to make you feel better that your not having to fix them and make the ones you have not look so bad.Totally did not think about last comma and with no validating or error handling should have been more along the lines of
I've been going through this project I have inherited and there are so many assumptions and glaring errors that I am beginning to think it was something I wrote ;)
Now that I have started to find lots of places in code where variables are assumed to have a specifically formatted value or in arrays where the element occasionally contains nothing .. I've started to see inherent problems in everything.
... and just think ... I have over 4000 code files written in VB.Net, C#.Net, Javascript, ASP.Net, HTML, XML, and SQL ... I've only started to scratch the surface .. and the boss wants it done next week ;)
Go figure
My 2 c
John
Public Sub foo()
Dim numbers() As Integer = New Integer() {0, 1, 2, 3, 4, 5, 6, 7}
Dim csv As String = ""
Dim x As Integer = -1
For Each n In numbers
csv = csv + Increment(x, x + 1).ToString & ","
Next
MsgBox(csv.TrimEnd(","))
End Sub
Private Function Increment(Of T)(ByRef target As T, ByVal value As T) As T
target = value
Return value
End Function
Code - Visual Basic: [Select]
Dim result As String While (gettingValues = True) Dim pointidnumber(0) As Object pointidnumber(0) = myPoint.PointNumber result &= CStr(pointidnumber(0)) & "," Wend
Notice in the above code that the string result is Dimmed outside the loop where it is being set. This ensures you do not redim the variable and lose its previous value.
This is a good way to handle creating your strings.
int[] array = // given input assigned to an array of int
// String.Join uses a StringBuilder internally:
string CDFString = string.Join( ", ", array.Select( i => i.ToString() ) );
You might want to look at the StringBuilder class, which was intended to avoid the issue the code you show above has, which is that every time you concatenate two strings, it requires a reallocation of memory and copying of the existing string, which is precisely the same as using 'Redim Preserve' to add items to an array, one at a time.
One of the very first best-practices often taught in .NET programming classes, is to never iteratively construct strings the way the above code does, because it is horribly slow (compared to using StringBuilder).
In the CDFPointString class you show, it is pretty much the same problem. In that case, you update the string each time an item is added, which requires a memory allocation and copying of the entire contents of the existing string. That's a lot of work to do each time an item is added, even though the only time the string result is needed, is (presumably) after having added many items.
Lastly, a single line of LINQ would produce the desired result the OP needs, given a sequence of points as input:
This is C#, using an array of int's as input.Code: [Select]
int[] array = // given input assigned to an array of int
// String.Join uses a StringBuilder internally:
string CDFString = string.Join( ", ", array.Select( i => i.ToString() ) );
Slower? yes, Significantly? perhaps, it is all predicated on the number of times the string is modified. In my tests, when six or less modifications are made in sequence, there is no performance boost by using StringBuilder ... in a loop with dozens or perhaps hundreds of changes, then the performance boost is significant.
I've done some research on string concatenation and stringbuilder and found that some compilers build the binaries exactly the same, eliminating the performance hit for string concatenations, although Visual Studios does not.
Lastly, a single line of LINQ would produce the desired result the OP needs, given a sequence of points as input:
This is C#, using an array of int's as input.Code: [Select]
int[] array = // given input assigned to an array of int
// String.Join uses a StringBuilder internally:
string CDFString = string.Join( ", ", array.Select( i => i.ToString() ) );
Join(String, Object[]) is a convenience method that lets you concatenate each element in an object array without explicitly converting its elements to strings. The string representation of each object in the array is derived by calling that object's ToString method.
... doing it incorrectly, so the fact that the performance hit is not significant with a relatively-tiny amount of data/iterations doesn't justify doing it incorrectly, when it can just as easily be done correctly ...
If you do the research you will find that using StringBuilder in preference to iterative string concatenation is a universally-accepted best practice. If you posted that example on StackOverflow or codeproject, you would get a flood of responses, all basically reiterating the same thing I just pointed out.
< .. >
Yep, the internet is full of flamers .. I thought we were above that here .. guess not
... doing it incorrectly, so the fact that the performance hit is not significant with a relatively-tiny amount of data/iterations doesn't justify doing it incorrectly, when it can just as easily be done correctly ...
I have to believe that you can't see how one sided your opinion happens to be. I don't think you would be that obtuse on purpose.
I submit that there are certainly better ways to do something, however, just because something is not done in the best way possible, certainly does not mean it is done incorrectly.
If your assertion is correct, and I don't necessarily believe it to be, all compilers should only support the more efficient method.
If you do the research you will find that using StringBuilder in preference to iterative string concatenation is a universally-accepted best practice. If you posted that example on StackOverflow or codeproject, you would get a flood of responses, all basically reiterating the same thing I just pointed out.
Yep, the internet is full of flamers .. I thought we were above that here .. guess not
Lastly, a single line of LINQ would produce the desired result the OP needs, given a sequence of points as input:
This is C#, using an array of int's as input.Code: [Select]
int[] array = // given input assigned to an array of int
// String.Join uses a StringBuilder internally:
string CDFString = string.Join( ", ", array.Select( i => i.ToString() ) );
Do you really need the LINQ code? The Join<T>(String, IEnumerable<T>) and Join(String, Object[]) methods should take care of it by calling the ToString method internally.
// given a sequence of CogoPoint objects:
IEnumerable<CogoPoint> points = //...
string CDFPointNumbers =
string.Join( ",", points.Select( p => p.PointNumber.ToString() ));
Note that in earlier versions of .NET prior to (not sure if it is 3.5 or 4.0), you had to first convert the input sequence to an array, like this: string.Join( ",", points.Select( p => p.PointNumber.ToString() ).ToArray());
Hi,
Just my 2 cts, a generic extension method.Code - C#: [Select]
static class Extensions { public static string ToDelimitedString<T>(this IEnumerable<T> source, string delimiter) { return source .Skip(1) .Aggregate(sb, (s, t) => s.Append(delimiter).Append(t)) .ToString(); } public static string ToCsv<T>(this IEnumerable<T> source) { return source.ToDelimitedString("'"); } }Code - vb.net: [Select]
Module extensions <System.Runtime.CompilerServices.Extension> _ Public Function ToDelimitedString(Of T)(source As IEnumerable(Of T), delimiter As String) As String Dim sb As New StringBuilder(source.First().ToString()) Return source _ .Skip(1) _ .Aggregate(sb, Function(s, x) s.Append(delimiter).Append(x)) _ .ToString() End Function <System.Runtime.CompilerServices.Extension> _ Public Function ToCsv(Of T)(source As IEnumerable(Of T)) Return source.ToDelimitedString(",") End Function End Module
Hi Gile.
Unless I'm missing something, wouldn't the following produce the same result?Code - C#: [Select]
public static string ToDelimitedString<T>( this IEnumerable<T> source, string separator ) { return string.Join<T>( separator, source ); }