Click here to Skip to main content
15,886,919 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I have C# coding ...I want to change string to stringbuilder for this coding..

I have sample coding

C#
public string name
       {
           get
           {
               return this.txtresname.Text;
           }

           set
           {
               this.txtresname.Text = value;
           }
       }



When i change string to stringbuilder it shows error in "this.txtresname.Text" as "Cannot convert string to stringbuilder";
Posted
Comments
Sergey Alexandrovich Kryukov 5-Jun-12 0:34am    
You did not even show the code which causes the error. What you show is not it.
--SA

Try this:
C#
public StringBuilder name
{
  get
  {
    return new StringBuilder(this.txtresname.Text);
  }

  set
  {
    this.txtresname.Text = value.ToString();
  }
}
 
Share this answer
 
Actually you can not convert it, since StringBuilder is not a scalar type.
You can create a stringbuilder instance that is initiated with your string. (MSDN[^])
C#
Stringbuilder sb = new StringBuilder(YourStringVariable)


Supposing that txtresname is the field you want to be a stringbuilder, see following:

C#
class myclass
{
    private StringBuilder txtresname = new StringBuilder();

    public string name
       {
           get
           {
               return this.txtresname.ToString();
           }

           set
           {
               this.txtresname.Clear;
               this.txtresname.Append(value);
           }
       }
}
 
Share this answer
 
v3
Please see my comment to the question. Of course one will not "convert" to another. How about just reading MSDN on the two classes? There two classes are related in the following way:
C#
string someString = //...
System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder(someString);

//...

string resultOfBuild = stringBuilder.ToString(); //isn't this obvious? :-)


As to the references, this one would be the most useful:
Microsoft Q209354. :-)

Good luck,
—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900