very simple loop with strange output

  • Thread starter Thread starter bouy78
  • Start date Start date
B

bouy78

I now know that I know nothing...

using System;
using System.IO;

namespace ADODotNet
{ public class Class1
{ public Random r = new Random();
string[] nodes = new string[] { "192.168.0.0", "192.168.0.1",
"192.168.0.2", "192.168.0.3", "192.168.0.4", "192.168.0.5",
"192.168.0.6", "192.168.0.7", "192.168.0.8", "192.168.0.9" };
public TextWriter tw = new StreamWriter("c:\\populate.txt");
[STAThread]
public static void Main()
{ Class1 mc = new Class1();
for (int i = 0; i <= 299; i++) //mornin
mc.putIP();
}
public void putIP()
{ tw.Write(nodes[r.Next(10)]);
tw.Write(" , ");
tw.Write(tw.NewLine);
}
}
}

I thought I'd get 300 lines but instead I get 256...why
 
Try a tw.Flush() after the for(). Does that help?

--
William Stacey [MVP]

|I now know that I know nothing...
|
| using System;
| using System.IO;
|
| namespace ADODotNet
| { public class Class1
| { public Random r = new Random();
| string[] nodes = new string[] { "192.168.0.0", "192.168.0.1",
| "192.168.0.2", "192.168.0.3", "192.168.0.4", "192.168.0.5",
| "192.168.0.6", "192.168.0.7", "192.168.0.8", "192.168.0.9" };
| public TextWriter tw = new StreamWriter("c:\\populate.txt");
| [STAThread]
| public static void Main()
| { Class1 mc = new Class1();
| for (int i = 0; i <= 299; i++) //mornin
| mc.putIP();
| }
| public void putIP()
| { tw.Write(nodes[r.Next(10)]);
| tw.Write(" , ");
| tw.Write(tw.NewLine);
| }
| }
| }
|
| I thought I'd get 300 lines but instead I get 256...why
|
 
Error 1 An object reference is required for the nonstatic field,
method, or property 'ADODotNet.Class1.tw' C:\Documents and
Settings\me\Local Settings\Application Data\Temporary
Projects\Project1\CodeFile1.cs 18 13 Project1..........................wont
let me use tw in Main....im extremely new to c#
 
In Main:

mc.FlushMe();

then write the method

public void FlushMe()
{
this.tw.Flush();
}
 
Error 1 An object reference is required for the nonstatic field,
method, or property 'ADODotNet.Class1.tw' C:\Documents and
Settings\me\Local Settings\Application Data\Temporary
Projects\Project1\CodeFile1.cs 18 13
Project1..........................wont
let me use tw in Main....im extremely new to c#

Main is a static function so does not have access to any module variables in
your class. A static function is just like a global function but placed in
your class for readability.

Michael
 
Back
Top