can anybody tell me how to create a string builder in a new class (say v.cs) which is going to call into main winform(form1.cs) project using c#
I have implemented below codes but it has working properly. I have browsed for whole day but found nothing. So your single threads meant me lots
// codes for my new class say v.cs
StringBuilder _sb= new StringBuilder();
public void v(StringBuilder sb)
{
sb.Replace("s", "A");
sb.Replace("hi","hello"); so on ....
}
// codes for form1.cs
private void button3_Click(object sender, EventArgs e)
{
StringBuilder sb= new StringBuilder(c1Editor1.Text);
v sb = new Jam(sb);
sb.display();
}
Thanks
3 Replies
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.

Wim SturkenboomPosted Oct 8, 2014, 1:28 PM
class vv
{
StringBuilder _sb;
public vv()
{
_sb = new StringBuilder();
}
public vv(StringBuilder sb)
{
_sb = sb;
}
public StringBuilder replace(string strWhat, string strByWhat)
{
_sb.Replace(strWhat, strByWhat);
return _sb;
}
public StringBuilder append(string strValue)
{
_sb = _sb.Append(strValue);
return _sb;
}
}
This class contains two constructors; public vv() will simply create a new StringBuilder _sb while public vv(StringBuilder sb) will take an argument and assign that to _sb.
Next there are two methods that you can use; one to replace content and one to append content.
A console application example that uses both
static void Main(string[] args)
{
vv vbuilder1 = new vv();
vv vbuilder2 = new vv(new StringBuilder("hi, how are you"));
vbuilder1.append("here we go");
Console.WriteLine(vbuilder1.replace("hi", "hello VerenaJam").ToString());
Console.WriteLine(vbuilder2.replace("hi", "hello VerenaJam").ToString());
Console.ReadLine();
}
This creates two independent instances of the class vv. vbuilder 1 will contain an empty StringBuilder object and vbuilder2 will contain a StringBuilder object with some content.
Next we append something to the StringBuilder object in vbuilder1 using the append method.
The two WriteLines call the replace methods and display the result.
The ReadLine is just there to wait for user input before the program ends.
I hope this helps
VerenaJamPosted Oct 8, 2014, 12:21 AM
I do realized that I have made many mistake . Therefore can you please share me your full codes for this if you dont mind because i have try lots to resolve the problem but couldnt make out.
Thanks
Wim SturkenboomPosted Oct 7, 2014, 8:46 AM
Next you will get an error on the below two lines
StringBuilder sb = new StringBuilder(c1Editor1.Text);
v sb = new Jam(sb);
In the first of those, you declare sb to be an object of type 'StringBuilder'. In the second one you declare that sb should be an object of type 'v'. That does not work; you can only declare sb once and it's either of type 'StringBuilder' or of type 'v'. The third problem is that you try to assign an object of type 'Jam' to an object that is declared to be of type 'v' (or 'StringBuilder'); that also does not work
Your class v
class v
{
StringBuilder _sb = new StringBuilder();
public v(StringBuilder sb)
{
sb.Replace("s", "A");
sb.Replace("hi", "hello");
}
}
And your button code
StringBuilder sb = new StringBuilder(c1Editor1.Text);
v vobject = new v(sb);
It will not do much, but at least compile.