tags:

views:

105

answers:

4

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
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
+1  A: 

I am guessing you didn't put public in front of the method in question.

Moo-Juice
I can't believe this would be true unless their definition of decent Java developer is a bit flawed.
ChaosPandion
@ChaosPandion: Whilst you have a valid point, I ceased to be surprised by anything these days.
Moo-Juice
@Moo - +1 for *"whilst"*.
ChaosPandion
public static void myMethod(string sentence)
nuclearpro
wow! lots of moody DEVs out there... didnt mean to bring you guys down
nuclearpro
@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
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
+1  A: 

Or maybe you didn't mark the method as static?

stuartsmithuk
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
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