Calling a function in another file - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Calling a function in another file

LoneIguanaLoneIguana Posts: 5Member
I'm trying to call a function in another file, I tried using filename.FunctionName() but it returns the following error when I try to build it:
An object reference is required for the nonstatic field, method, or property


Comments

  • BrutesBrutes Posts: 162Member
    Hi

    Well are you calling a function with in another class?

    public class myClass
    {
    public string myFunction()
    {
    return "You called me";
    }
    }

    public class myCallerClass
    {
    public static void main(string[] args)
    {
    myClass myObject = new myClass();
    Console.Write(myObject.myFunction());
    }
    }

    obviusly the top class can be in another file in the project.

  • yuvalyuval Posts: 13Member

    Hi

    Suppose you have a project with a class that computes the average of 10 numbers. In a different file you already written a class "Add_numbers" that adds up numbers. What you can do here, and is, in my opinion, the logical thing to do, is to incorporate the class
    "Add_numbers" into your project. Go to File->Add Existing Item and select the project that contains
    the class you want. Then select the class itself.
    This class in added in full to your project. Now you can access any method of this class by calling "Add_numbers.method_name". Voila.(do not forget to
    make an object of this class first).

    regards
    yuval
  • yuvalyuval Posts: 13Member

    Hi

    Suppose you have a project with a class that computes the average of 10 numbers. In a different file you already written a class "Add_numbers" that adds up numbers. What you can do here, and is, in my opinion, the logical thing to do, is to incorporate the class
    "Add_numbers" into your project. Go to File->Add Existing Item and select the project that contains
    the class you want. Then select the class itself.
    This class in added in full to your project. Now you can access any method of this class by calling "Add_numbers.method_name". Voila.(do not forget to
    make an object of this class first).

    regards
    yuval
  • LoneIguanaLoneIguana Posts: 5Member
    ok, Thanks, I forgot to instantiate the class.

    :
    : Hi
    :
    : Suppose you have a project with a class that computes the average of 10 numbers. In a different file you already written a class "Add_numbers" that adds up numbers. What you can do here, and is, in my opinion, the logical thing to do, is to incorporate the class
    : "Add_numbers" into your project. Go to File->Add Existing Item and select the project that contains
    : the class you want. Then select the class itself.
    : This class in added in full to your project. Now you can access any method of this class by calling "Add_numbers.method_name". Voila.(do not forget to
    : make an object of this class first).
    :
    : regards
    : yuval
    :

Sign In or Register to comment.