Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Object required? Compiler error CS0120 1

Status
Not open for further replies.

Bujitsu

MIS
Oct 23, 2003
67
AU
Hi,

I am trying to understand why the compiler wants an object?
Any help would be great.

Thank you



static void Main(string[] args)
{
int counter = 0;
string sDir = "c:\\";
DirSearch(sDir);// <-------Here.
}

void DirSearch(string sDir)
{
try
{
foreach (string d in Directory.GetDirectories(sDir))
{
foreach (string f in Directory.GetFiles(d,"test.txt"))
{
Console.WriteLine(f);
}
DirSearch(d);
}
}
catch (System.Exception excpt)
{
Console.WriteLine(excpt.Message);
}
}
 
You need to change DirSearch to a static void or instantiate your class like so:

Code:
static void Main(string[] args)
 {
   int counter = 0;//what the heck is this for?
   string sDir = "c:\\";
   MyClass mc = new MyClass();//here you instantiate your class
   mc.DirSearch(sDir);// now you can execute its' method(s)
   mc = null;//get rid of your object
 }


Hope this helps,

Alex



Ignorance of certain subjects is a great part of wisdom
 
Bujitsu - I think this may be a worthwhile read for you too:


I know that it is about java, but the principle still applies. It doesn't look right for me (IE6) and I have to scroll way the heck down to see anything, but it appears to be a good explaination of the differences between static and instance methods.

Hope it helps,

Alex

Ignorance of certain subjects is a great part of wisdom
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top