Why can't I call a public method in another class?
Asked Answered
L

4

11

I have got these two classes interacting and I am 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]'"


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.

Liability answered 1/12, 2008 at 6:12 Comment(2)
Please add more clarification like how you are invoking the method, is it a static method, or an instance method etc. Otherwise this question will be left for interpretation.. All the best.Giustino
well, I learned something from your homework...Kandi
O
39

You're trying to call an instance method on the class. 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());
}
Overdone answered 1/12, 2008 at 6:16 Comment(6)
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.Liability
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.Nicknack
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.Kandi
@discorax, Yes. Perfectly legal to call static methods without every creating an instance of a classOverdone
"Have you walked the dog this morning, dear?" / "No, but I have walked the concept of dog."Horan
Is there a way to do this without using static?Saltatorial
K
12

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.

Kandi answered 1/12, 2008 at 6:16 Comment(2)
+1 because it looks like a correct answer. Whoever downvoted this, I wish they would comment here.Ung
At first I thought this worked, but it's since created a recursion error in the IDE and is crashing it on the solution load. It's a great error, but I have to now find an alternative. Form1 myClass = new VisualCues.Form1(); myClass.ReadTheAPETag(myClass.ex);Gunthar
B
4

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]
Behoof answered 1/12, 2008 at 6:16 Comment(2)
My fault, I misread something and was on stackoverflow when I was ovewrly tired. I can;t undo my downvote now however;-(Disillusion
You can click on the down vote arrow again to undo a down vote.Impersonal
L
2

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();
Lastditch answered 1/12, 2008 at 8:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.