Create a c# project, put the below method into a class: private static void ShowBarData() { string afile = Path.GetDirectoryName(Assembly.GetExecutingAssembly().FullName)+"\\\\great.txt"; FileStream fs = File.Create(afile); BinaryWriter g = new BinaryWriter(fs); try {
g.Write("Normal 1"); g.Write(" Nos of Steel Bar = " + " unit"); g.Write("");
g.Write("Table Title 1"); g.Write("\\tTable 1 : Reinforcement coordinate, diameter and area");
g.Write("Table Text 1");
int BarNo = 1; int rowno = 1; for (int i = 0; i < 10; i++) { for (int j = 0; j < 20; j++) { g.Write("Bar " + i.ToString()+j.ToString()); g.Write("Bar k" + rowno.ToString()); g.Write(BarNo.ToString()); rowno++; BarNo++; } } } finally { g.Close(); fs.Close(); } }
Highlight the line int BarNo=1 to the end of the for loop
Select "Extract Method", choose the default name. A method with the name "MyMethod" will appear as follows: private void MyMethod( ref BinaryWriter g ) { int BarNo = 1; int rowno = 1; for (int i = 0; i < 10; i++) { for (int j = 0; j < 20; j++) { g.Write("Bar " + i.ToString()+j.ToString()); g.Write("Bar k" + rowno.ToString()); g.Write(BarNo.ToString()); rowno++; BarNo++; } } }
Here are a couple of observations:
The method is not correctly indented
Although the method correctly identifies the BinaryWriter as the argument, but there is no need for the ref keyword, putting in ref causes a compilation error.