Yes I'm new to C# but I'm a decent Java Dev. OK Ive got a project in visual studio with a program.cs file and a Class.cs file. All I'm trying to do is make a call to the method in Class.cs in Program.cs.... I have 1 frustrating error. The name 'mymethod' does not exist in the current context. All the other code builds fine if I comment out the method call mymethod(parameter); but I cant get rid of that bug.... I would greatly appreciate any help someone could give me.
+4
A:
This doesn't work?
public class Class
{
public void myMethod()
{
}
}
class Program
{
static void Main(string[] args)
{
Class c = new Class();
c.myMethod();
}
}
ChaosPandion
2010-10-29 13:55:23
sorry guys i will post my code from now on. The answer does work I just have to have all code in one file. Thats how I did it in Java. So I spranged my ankle on the "walk in the park" because of the use of two different files. ( the class file and the program/main() file.
nuclearpro
2010-10-29 15:32:02
+1
A:
I am guessing you didn't put public in front of the method in question.
Moo-Juice
2010-10-29 13:58:34
I can't believe this would be true unless their definition of decent Java developer is a bit flawed.
ChaosPandion
2010-10-29 13:59:40
@ChaosPandion: Whilst you have a valid point, I ceased to be surprised by anything these days.
Moo-Juice
2010-10-29 14:03:17
wow! lots of moody DEVs out there... didnt mean to bring you guys down
nuclearpro
2010-10-29 15:40:49
@nuclearpro - Wasn't being moody mate :) It was a genuine thought, had you made it publically accessible? With regard to your comments on other answers, you shouldn't need to throw all your code in to a single file for it to work. Unless a class is small and utilitarian, it gets its own file.
Moo-Juice
2010-10-29 15:49:43
yes it seems that everything is in order except main won't recognize my method name from another class. Every things public I'm not trying to encapsulate just build. Thanks for help!
nuclearpro
2010-10-29 15:58:16
The answer does work I just have to have all code in one file. Thats how I did it in Java. So I spranged my ankle on the "walk in the park" because of the use of two different files. ( the class file and the program/main() file. Good god my first question and I'm getting pounded!
nuclearpro
2010-10-29 15:35:33
A:
You are probably calling the method without creating an object first:
public class MyClass
{
public void MyMethod()
{
}
}
MyClass.MyMethod();
You should create an instance first:
var obj = new MyClass();
obj.MyMethod();
jgauffin
2010-10-29 14:55:35