unable to access classs in VS2005

C

csharpquestion

Hi all. I have a real simple question that is driving me nuts. I
have the a console app with 3 classess.
I am trying to access c2.doit() from Class1 - but I can't. I can only
access from the Program class. I am using
Visual Stuido 2005. It works in VS2003. What am I doing wrong?
Thanks

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Class2 c2 = new Class2();
// THIS WORKS
c2.doit();
}
}
}


using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
class Class1
{
// COMPILES WITH THE FOLLOWING LINE
Class2 c2 = new Class2();
// DOES NOT COMPILE - c2 CANNOT BE FOUND
c2.doit();
}
}

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
public class Class2
{
public void doit()
{
string f = "";
}
}
}
 
C

csharpquestion

Hi all. I have a real simple question that is driving me nuts. I
have the a console app with 3 classess.
I am trying to access c2.doit() from Class1 - but I can't. I can only
access from the Program class. I am using
Visual Stuido 2005. It works in VS2003. What am I doing wrong?
Thanks

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Class2 c2 = new Class2();
// THIS WORKS
c2.doit();
}
}

}

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
class Class1
{
// COMPILES WITH THE FOLLOWING LINE
Class2 c2 = new Class2();
// DOES NOT COMPILE - c2 CANNOT BE FOUND
c2.doit();
}

}

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
public class Class2
{
public void doit()
{
string f = "";
}
}



}- Hide quoted text -

- Show quoted text -

never mind, as usual stupid mistake. this works:

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
public class Class1
{
public void test()
{
Class2 c2 = new Class2();
c2.doit();
}
}
}
 
I

Irfan

You need to keep the calls to methods inside methods. You are directly calling class2.

//Modifed Class1
class Class1
{
void CallingClass2()
{
class2 c2 = new class2();
c2.doit();
}
}



Hope this helps






Hi all. I have a real simple question that is driving me nuts. I
have the a console app with 3 classess.
I am trying to access c2.doit() from Class1 - but I can't. I can only
access from the Program class. I am using
Visual Stuido 2005. It works in VS2003. What am I doing wrong?
Thanks

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Class2 c2 = new Class2();
// THIS WORKS
c2.doit();
}
}

}

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
class Class1
{
// COMPILES WITH THE FOLLOWING LINE
Class2 c2 = new Class2();
// DOES NOT COMPILE - c2 CANNOT BE FOUND
c2.doit();
}

}

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
public class Class2
{
public void doit()
{
string f = "";
}
}



}- Hide quoted text -

- Show quoted text -

never mind, as usual stupid mistake. this works:

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
public class Class1
{
public void test()
{
Class2 c2 = new Class2();
c2.doit();
}
}
}
 
H

Hardono Arifanto

I think you forgot to put the c2.doit(); inside the main() method.

Accessing an object method is only permitted inside method of the
current class.

Regards,
Hardono Arifanto
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top