Cameron Kloot

i am new to string builder class, but i thought this would work. i doesn't. :
public string [] typeA = { "meshbox", "pyramid" };
public string [] prop = { "name", "postition", "rotation", "size", "end" };
public double [] subProp = { 1, 2, 3 };
public string mBFilePath;
public string nl = Environment.NewLine;
public StringBuilder fullMB = typeA[1] + nl + prop[0] + nl + prop[1] + nl + prop[2];

it says this about the last line: Error 1 A field initializer cannot reference the nonstatic field, method, or property 'BMM.Form1.typeA'

thanks





Re: Visual C# Express Edition string builder

Traps

I believe you need to define your array like this........

public string [] typeA = new string [2] { "meshbox", "pyramid" };





Re: Visual C# Express Edition string builder

Cameron Kloot

no, my array is not the problem. it is the string builder that i am having trouble with. the array works fine. i need to know how to use the string builder class. cause i am obviously doing something wrong.






Re: Visual C# Express Edition string builder

Traps

string temp = typeA[1] + nl + prop[0] + nl + prop[1] + nl + prop[2];

StringBuilder fullMB = new StringBuilder(temp,25);

25 is the capacity of the string builder, set its value appropriately.

http://msdn2.microsoft.com/en-us/library/2839d5h5(vs.71).aspx





Re: Visual C# Express Edition string builder

nobugz

Section 10.4.5.2 of the C# Language Specification forbids using another field's value in a field initializer. Just initialize "fullMB" in the constructor.





Re: Visual C# Express Edition string builder

Traps

nobugz wrote:
Section 10.4.5.2 of the C# Language Specification forbids using another field's value in a field initializer. Just initialize "fullMB" in the constructor.

I stand corrected sir! (not suprised Smile)





Re: Visual C# Express Edition string builder

Cameron Kloot

well, that was sorta it, i had to do this

StringBuilder fullMB = new StringBuilder(temp, 25);
temp = typeA[1] + nl + prop[0] + nl + prop[1] + nl + prop[2];
string temp2 = fullMB.ToString();
so...
thanks
but is there a better way anyone






Re: Visual C# Express Edition string builder

Cameron Kloot

what do u mean nobugz can u exp[lain a little better. sry
thanks





Re: Visual C# Express Edition string builder

nobugz

public class WhatEver {
public string[] typeA = { "meshbox", "pyramid" };
public string[] prop = { "name", "postition", "rotation", "size", "end" };
public double[] subProp = { 1, 2, 3 };
public string mBFilePath;
public string nl = Environment.NewLine;
public StringBuilder fullMB;
public WhatEver() {
fullMB = new StringBuilder(typeA[1] + nl + prop[0] + nl + prop[1] + nl + prop[2]);
}
}





Re: Visual C# Express Edition string builder

windfinder

You are not using the string builder.
Here's an example:

StringBuilder sb = new StringBuilder();
sb.Append("foo")
sb.Append("foo2");

//or like this
StringBuilder sb2 = new StringBuilder();
sb2.Append("foo").Append("foo2");

/johan/
http://idstam.com/en






Re: Visual C# Express Edition string builder

Cameron Kloot

on this bit: public WhatEver() {
fullMB = new StringBuilder(typeA[1] + nl + prop[0] + nl + prop[1] + nl + prop[2]);
}
}

it gives this error for the "Whatever":
Error 1 Class, struct, or interface method must have a return type

thanks