So I am trying to learn how to use the StringBuilder
Class. I read up about it and it seems amazing compared to string
!
I am trying to create the StringBuilder
in the other button but it keeps throwing me the error:
; expected" and Use of unassigned local variable "strb"
on line 42 & 43.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication15
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string nrmlString = "C#";
nrmlString += " This";
nrmlString += " is";
nrmlString += " a";
nrmlString += " Test";
nrmlString += " Thisss";
MessageBox.Show(nrmlString);
}
private void button2_Click(object sender, EventArgs e)
{
StringBuilder strb new StringBuilder("something");
strb.Append("Something else");
MessageBox.Show(strb.ToString());
}
}
}
@VargaDev, Your error was a simple missing of the = in your code.
StringBuilder strb = new StringBuilder("something");
And it has already been answered. I just chimed in for a suggestion. When trying this type of codes, using VS and creating a form is cumbersome. Have a check at the wonderful (and free) utility LinqPad. You can use that as a code scratch pad. ie: For testing your code above, you would simply do this:
-Choose C# statements (or C# program) from the combo -Type
StringBuilder strb = new StringBuilder("something");
strb.Append("Something else");
strb.ToString().Dump();
and hit F5. That is it! Dump() is on streoids. You can dump almost anything, it shows the result in a suitable way (a datagrid for example if it is a list like thing).
PS: I don't have any affiliation with the author of the utility (Joseph Albahari), just a lover of it. It is (and he is) worth to be appraised.