Problem to ASCII

HuaMin Chen
HuaMin Chen used Ask the Experts™
on
Hi,
Any advice to these
namespace ClassLibrary1
{
    public class Class1
    {
        bool allowappend = true;
        string File1="c:/dp2/oup.txt";
        string s = "9quali52ty3";
        byte[] ASCIIValues = Encoding.ASCII.GetBytes(s);
        foreach(byte b in ASCIIValues) {
                using (StreamWriter sw = new StreamWriter(@File1, allowappend, Encoding.Unicode))
                {
                    sw.WriteLine(b);
                    sw.WriteLine("");
                }
}
}
}

Open in new window


due to these
Error	1	Invalid token 'foreach' in class, struct, or interface member declaration	C:\dp11\ClassLibrary1\ClassLibrary1\Class1.cs	38	9	ClassLibrary1
Error	2	; expected	C:\dp11\ClassLibrary1\ClassLibrary1\Class1.cs	38	24	ClassLibrary1
Error	3	Invalid token ')' in class, struct, or interface member declaration	C:\dp11\ClassLibrary1\ClassLibrary1\Class1.cs	38	38	ClassLibrary1
Error	4	; expected	C:\dp11\ClassLibrary1\ClassLibrary1\Class1.cs	39	97	ClassLibrary1
Error	5	Invalid token '(' in class, struct, or interface member declaration	C:\dp11\ClassLibrary1\ClassLibrary1\Class1.cs	41	33	ClassLibrary1
Error	6	Invalid token ')' in class, struct, or interface member declaration	C:\dp11\ClassLibrary1\ClassLibrary1\Class1.cs	41	35	ClassLibrary1
Error	7	Invalid token '(' in class, struct, or interface member declaration	C:\dp11\ClassLibrary1\ClassLibrary1\Class1.cs	42	33	ClassLibrary1
Error	8	Type or namespace definition, or end-of-file expected	C:\dp11\ClassLibrary1\ClassLibrary1\Class1.cs	45	1	ClassLibrary1
Error	9	Type or namespace definition, or end-of-file expected	C:\dp11\ClassLibrary1\ClassLibrary1\Class1.cs	46	1	ClassLibrary1
Error	10	A field initializer cannot reference the non-static field, method, or property 'ClassLibrary1.Class1.s'	C:\dp11\ClassLibrary1\ClassLibrary1\Class1.cs	37	54	ClassLibrary1
Error	11	'ClassLibrary1.Class1.ASCIIValues' is a 'field' but is used like a 'type'	C:\dp11\ClassLibrary1\ClassLibrary1\Class1.cs	38	27	ClassLibrary1
Error	12	A field initializer cannot reference the non-static field, method, or property 'ClassLibrary1.Class1.File1'	C:\dp11\ClassLibrary1\ClassLibrary1\Class1.cs	39	59	ClassLibrary1
Error	13	A field initializer cannot reference the non-static field, method, or property 'ClassLibrary1.Class1.allowappend'	C:\dp11\ClassLibrary1\ClassLibrary1\Class1.cs	39	67	ClassLibrary1
Error	14	'ClassLibrary1.Class1.sw' is a 'field' but is used like a 'type'	C:\dp11\ClassLibrary1\ClassLibrary1\Class1.cs	41	21	ClassLibrary1
Error	15	'ClassLibrary1.Class1.b' is a 'field' but is used like a 'type'	C:\dp11\ClassLibrary1\ClassLibrary1\Class1.cs	41	34	ClassLibrary1
Error	16	'ClassLibrary1.Class1.sw' is a 'field' but is used like a 'type'	C:\dp11\ClassLibrary1\ClassLibrary1\Class1.cs	42	21	ClassLibrary1

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Billing Engineer
Most Valuable Expert 2014
Top Expert 2009
Commented:
I agree with the error message.
inside the class, you cannot write code outside of functions...

you may want to put this code into the class constructur, for example

namespace ClassLibrary1
{
    public class Class1
    {
       public Class1()
      {
        bool allowappend = true;
        string File1="c:/dp2/oup.txt";
        string s = "9quali52ty3";
        byte[] ASCIIValues = Encoding.ASCII.GetBytes(s);
        foreach(byte b in ASCIIValues) {
                using (StreamWriter sw = new StreamWriter(@File1, allowappend, Encoding.Unicode))
                {
                    sw.WriteLine(b);
                    sw.WriteLine("");
                } // using streamwriter
       } // foreach
   } // constructor
} // class
} // namespace

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial