views:

1668

answers:

5

I've got these two classes interacting and I'm trying to call four different classes from class one for use in class two.

The methods are public and they do return values but for some reason there is not a connection being made. The error I get when I try is: "An object reference is required for the nonstatic field, method, or property 'GradeBook.[method I want called]'"

+2  A: 

You have to create a variable of the type of the class, and set it equal to a new instance of the object first.

GradeBook myGradeBook = new GradeBook();

Then call the method on the obect you just created.

myGradeBook.[method you want called]
Jeffrey L Whitledge
Why does this have a down vote?
Nathan W
My fault, I misread something and was on stackoverflow when I was ovewrly tired. I can;t undo my downvote now however;-(
JoshBerke
You can click on the down vote arrow again to undo a down vote.
Eric Ness
+17  A: 

You're trying to call an instance method on the class. For to call an instance method on a class you must create an instance on which to call the method. If you want to call the method on non-instances add the static keyword. For example

class Example {
  public static string NonInstanceMethod() {
    return "static";
  }
  public string InstanceMethod() { 
    return "non-static";
  }
}

static void SomeMethod() {
  Console.WriteLine(Example.NonInstanceMethod());
  Console.WriteLine(Example.InstanceMethod());  // Does not compile
  Example v1 = new Example();
  Console.WriteLine(v1.InstanceMethod());
}
JaredPar
When I change the method I want to call to say static then variables inside that method go crazy and I get more errors. I can't win.
Harris
Yes you can - if you want to call an instance method, call it on an instance. It's the only way which makes sense. Otherwise it's like asking "dog" to run instead of asking a *particular* dog to run.
Jon Skeet
follow-up question for my benefit. Can you call the Static Method without EVER creating an instance of the class, since it appears that's what you're doing in the example. I'll be sure to try this out on Monday. Thanks for sharing.
discorax
@discorax, Yes. Perfectly legal to call static methods without every creating an instance of a class
JaredPar
"Have you walked the dog this morning, dear?" / "No, but I have walked the concept of dog."
Daniel Earwicker
+2  A: 

It sounds like you're not instantiating your class. That's the primary reason I get the "an object reference is required" error.

MyClass myClass = new MyClass();

once you've added that line you can then call your method

myClass.myMethod();

Also, are all of your classes in the same namespace? When I was first learning c# this was a common tripping point for me.

discorax
+1 because it looks like a correct answer. Whoever downvoted this, I wish they would comment here.
Karl
A: 

I have everything initialized. I don't want to create the methods as static. I read over the specifics of my assignment again and I'm not even supposed to but I can't seem to get this to work anyway I word it.

myGradeBook.[method] GraceBook.[method]

It all seems to create errors.

The current errors: The best overloaded method match or 'System.Console.WriteLine(string, object)' has some invalid arguments.

Arugment '2': cannot convert from 'method group' to 'object'

I'm not even sur what those mean....

EDIT: I just fixed that problem thanks to the Step Into feature of Visual Studio. I don't know why it took me so long to use it.

Harris
A: 

For example 1 and 2 you need to create static methods:

public static string InstanceMethod() {return "Hello World";}

Then for example 3 you need an instance of your object to invoke the method:

object o = new object();
string s = o.InstanceMethod();