he he
-
string currentdate = "Current date and time is " + DateTime.Now.ToString(); StringBuilder sbcurrentdate = new StringBuilder(); sbcurrentdate.Append(currentdate); return sbcurrentdate;
Regards, Sylvester G sylvester_g_m@yahoo.com
-
string currentdate = "Current date and time is " + DateTime.Now.ToString(); StringBuilder sbcurrentdate = new StringBuilder(); sbcurrentdate.Append(currentdate); return sbcurrentdate;
Regards, Sylvester G sylvester_g_m@yahoo.com
Marvelous, a real beauty. Let me guess what comes after the method call... .ToString()?!? :laugh:
-
string currentdate = "Current date and time is " + DateTime.Now.ToString(); StringBuilder sbcurrentdate = new StringBuilder(); sbcurrentdate.Append(currentdate); return sbcurrentdate;
Regards, Sylvester G sylvester_g_m@yahoo.com
sylvesterg wrote:
return sbcurrentdate**.ToString()**;
I assume, or does it really returns StringBuilder? Either way... heh.
"Throughout human history, we have been dependent on machines to survive. Fate, it seems, is not without a sense of irony. " - Morpheus "Real men use mspaint for writing code and notepad for designing graphics." - Anna-Jayne Metcalfe
-
string currentdate = "Current date and time is " + DateTime.Now.ToString(); StringBuilder sbcurrentdate = new StringBuilder(); sbcurrentdate.Append(currentdate); return sbcurrentdate;
Regards, Sylvester G sylvester_g_m@yahoo.com
-
The whole point of using StringBuilder is that it's far more efficient than using "+" to "add" two strings together - the programmer was probably told to use StringBuilder for efficiency, and that was what they came up with
you caught the code nicely
Regards, Sylvester G sylvester_g_m@yahoo.com
-
The whole point of using StringBuilder is that it's far more efficient than using "+" to "add" two strings together - the programmer was probably told to use StringBuilder for efficiency, and that was what they came up with
Oh...:~ Well, that kind of makes a bit more sense now, but I still don't see what the big deal is. Other than the uselessness of StringBuilder, everything seems fine to me. Maybe if there were a lot more "+" concatenations before the StringBuilder usage, it would be a little funnier. Also, I never trust an object to be better than another or than simple operations just because the documentation says so. There are a lot of ways you can concatenate two strings but the simplest and fastest way should always be the obvious way... However, if you can prove that StringBuilder is better than simple string concatenation, you get an A+. :D
-
Oh...:~ Well, that kind of makes a bit more sense now, but I still don't see what the big deal is. Other than the uselessness of StringBuilder, everything seems fine to me. Maybe if there were a lot more "+" concatenations before the StringBuilder usage, it would be a little funnier. Also, I never trust an object to be better than another or than simple operations just because the documentation says so. There are a lot of ways you can concatenate two strings but the simplest and fastest way should always be the obvious way... However, if you can prove that StringBuilder is better than simple string concatenation, you get an A+. :D
It should read like so
return new StringBuilder("Current date and time is " + DateTime.Now.ToString());
Lots of useless code to return a string as a StringBuilder. Or, shouldn't have used StringBuilder at all. The object of the StringBuilder class is to save memory when appending strings together. The original code misses that concept and uses more memory than if he would have just added the strings together.
Darroll Not one person lives in the present.
-
Oh...:~ Well, that kind of makes a bit more sense now, but I still don't see what the big deal is. Other than the uselessness of StringBuilder, everything seems fine to me. Maybe if there were a lot more "+" concatenations before the StringBuilder usage, it would be a little funnier. Also, I never trust an object to be better than another or than simple operations just because the documentation says so. There are a lot of ways you can concatenate two strings but the simplest and fastest way should always be the obvious way... However, if you can prove that StringBuilder is better than simple string concatenation, you get an A+. :D
sk8er_boy287 wrote:
However, if you can prove that StringBuilder is better than simple string concatenation, you get an A+.
If you need to build a string in a loop the stringbuilder is FAR more efficient then normal string concatenation. I've run into the problem before and learned the hard way. Actually I learned about it a while back using VB6, but VB6 didn't have a nice StringBuilder so I figured out another way around it. A simple example will show the difference. Do I get an A+? :)
Public Class Form1
Private Sub TestStringConcatenation(ByVal loops As Integer, ByVal myText As String)
Dim s As String = ""
For i As Integer = 0 To loops
s &= myText
Next
End SubPrivate Sub TestStringBuilder(ByVal loops As Integer, ByVal myText As String) Dim sb As New System.Text.StringBuilder For i As Integer = 0 To loops sb.Append(myText) Next End Sub Private Sub Button1\_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Const LOOPS As Integer = 1000 Dim myText As String = New String("A"c, 200) Dim sw As New Stopwatch sw.Start() TestStringConcatenation(LOOPS, myText) sw.Stop() Console.WriteLine("Normal string concatenation took {0} ticks", sw.ElapsedTicks) sw.Reset() sw.Start() TestStringBuilder(LOOPS, myText) sw.Stop() Console.WriteLine("String builder took {0} ticks", sw.ElapsedTicks) End Sub
End Class
As for the code the OP posted, what's funny about it is how much extra useless code the person used to return a string. All that was needed was a simple line
Return "Current date and time is " + DateTime.Now.ToString()
As someone already mentioned the original code writer probably learned that is was more efficient to use the stringbuilder if you are going to append text to a string. Unfortunatly they didn't use it correctly and didn't really grasp when it's appropriate to use it. I ran a test to see what method performed best. In this case normal string concatenation seemed to out perform stringbuilder. Here is my code I used to see the performance of each method.
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Const TRIALS As Integer = 1000
Const LOOPS As Integer = 1000 -
sk8er_boy287 wrote:
However, if you can prove that StringBuilder is better than simple string concatenation, you get an A+.
If you need to build a string in a loop the stringbuilder is FAR more efficient then normal string concatenation. I've run into the problem before and learned the hard way. Actually I learned about it a while back using VB6, but VB6 didn't have a nice StringBuilder so I figured out another way around it. A simple example will show the difference. Do I get an A+? :)
Public Class Form1
Private Sub TestStringConcatenation(ByVal loops As Integer, ByVal myText As String)
Dim s As String = ""
For i As Integer = 0 To loops
s &= myText
Next
End SubPrivate Sub TestStringBuilder(ByVal loops As Integer, ByVal myText As String) Dim sb As New System.Text.StringBuilder For i As Integer = 0 To loops sb.Append(myText) Next End Sub Private Sub Button1\_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Const LOOPS As Integer = 1000 Dim myText As String = New String("A"c, 200) Dim sw As New Stopwatch sw.Start() TestStringConcatenation(LOOPS, myText) sw.Stop() Console.WriteLine("Normal string concatenation took {0} ticks", sw.ElapsedTicks) sw.Reset() sw.Start() TestStringBuilder(LOOPS, myText) sw.Stop() Console.WriteLine("String builder took {0} ticks", sw.ElapsedTicks) End Sub
End Class
As for the code the OP posted, what's funny about it is how much extra useless code the person used to return a string. All that was needed was a simple line
Return "Current date and time is " + DateTime.Now.ToString()
As someone already mentioned the original code writer probably learned that is was more efficient to use the stringbuilder if you are going to append text to a string. Unfortunatly they didn't use it correctly and didn't really grasp when it's appropriate to use it. I ran a test to see what method performed best. In this case normal string concatenation seemed to out perform stringbuilder. Here is my code I used to see the performance of each method.
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Const TRIALS As Integer = 1000
Const LOOPS As Integer = 1000 -
sk8er_boy287 wrote:
However, if you can prove that StringBuilder is better than simple string concatenation, you get an A+.
If you need to build a string in a loop the stringbuilder is FAR more efficient then normal string concatenation. I've run into the problem before and learned the hard way. Actually I learned about it a while back using VB6, but VB6 didn't have a nice StringBuilder so I figured out another way around it. A simple example will show the difference. Do I get an A+? :)
Public Class Form1
Private Sub TestStringConcatenation(ByVal loops As Integer, ByVal myText As String)
Dim s As String = ""
For i As Integer = 0 To loops
s &= myText
Next
End SubPrivate Sub TestStringBuilder(ByVal loops As Integer, ByVal myText As String) Dim sb As New System.Text.StringBuilder For i As Integer = 0 To loops sb.Append(myText) Next End Sub Private Sub Button1\_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Const LOOPS As Integer = 1000 Dim myText As String = New String("A"c, 200) Dim sw As New Stopwatch sw.Start() TestStringConcatenation(LOOPS, myText) sw.Stop() Console.WriteLine("Normal string concatenation took {0} ticks", sw.ElapsedTicks) sw.Reset() sw.Start() TestStringBuilder(LOOPS, myText) sw.Stop() Console.WriteLine("String builder took {0} ticks", sw.ElapsedTicks) End Sub
End Class
As for the code the OP posted, what's funny about it is how much extra useless code the person used to return a string. All that was needed was a simple line
Return "Current date and time is " + DateTime.Now.ToString()
As someone already mentioned the original code writer probably learned that is was more efficient to use the stringbuilder if you are going to append text to a string. Unfortunatly they didn't use it correctly and didn't really grasp when it's appropriate to use it. I ran a test to see what method performed best. In this case normal string concatenation seemed to out perform stringbuilder. Here is my code I used to see the performance of each method.
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Const TRIALS As Integer = 1000
Const LOOPS As Integer = 1000 -
sk8er_boy287 wrote:
However, if you can prove that StringBuilder is better than simple string concatenation, you get an A+.
If you need to build a string in a loop the stringbuilder is FAR more efficient then normal string concatenation. I've run into the problem before and learned the hard way. Actually I learned about it a while back using VB6, but VB6 didn't have a nice StringBuilder so I figured out another way around it. A simple example will show the difference. Do I get an A+? :)
Public Class Form1
Private Sub TestStringConcatenation(ByVal loops As Integer, ByVal myText As String)
Dim s As String = ""
For i As Integer = 0 To loops
s &= myText
Next
End SubPrivate Sub TestStringBuilder(ByVal loops As Integer, ByVal myText As String) Dim sb As New System.Text.StringBuilder For i As Integer = 0 To loops sb.Append(myText) Next End Sub Private Sub Button1\_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Const LOOPS As Integer = 1000 Dim myText As String = New String("A"c, 200) Dim sw As New Stopwatch sw.Start() TestStringConcatenation(LOOPS, myText) sw.Stop() Console.WriteLine("Normal string concatenation took {0} ticks", sw.ElapsedTicks) sw.Reset() sw.Start() TestStringBuilder(LOOPS, myText) sw.Stop() Console.WriteLine("String builder took {0} ticks", sw.ElapsedTicks) End Sub
End Class
As for the code the OP posted, what's funny about it is how much extra useless code the person used to return a string. All that was needed was a simple line
Return "Current date and time is " + DateTime.Now.ToString()
As someone already mentioned the original code writer probably learned that is was more efficient to use the stringbuilder if you are going to append text to a string. Unfortunatly they didn't use it correctly and didn't really grasp when it's appropriate to use it. I ran a test to see what method performed best. In this case normal string concatenation seemed to out perform stringbuilder. Here is my code I used to see the performance of each method.
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Const TRIALS As Integer = 1000
Const LOOPS As Integer = 1000Horring me :confused::confused::confused::confused::confused::confused::rolleyes::mad:
Regards, Sylvester G sylvester_g_m@yahoo.com
-
string currentdate = "Current date and time is " + DateTime.Now.ToString(); StringBuilder sbcurrentdate = new StringBuilder(); sbcurrentdate.Append(currentdate); return sbcurrentdate;
Regards, Sylvester G sylvester_g_m@yahoo.com
:laugh::laugh::laugh:
Regards, Satips.:rose: Don't walk in front of me, I may not follow; Don't walk behind me, I may not lead; Walk beside me, and just be my friend. - Albert Camus