Howdy, Stranger!

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

Sign In with Facebook Sign In with Google Sign In with OpenID

Categories

We have migrated to a new platform! Please note that you will need to reset your password to log in (your credentials are still in-tact though). Please contact lee@programmersheaven.com if you have questions.
Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

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.