none
C# how to call a method from another class?

    General discussion

  • Hi I have a 2 classes and one class contains the method:

      public double getAngleBetweenVector(Vector2D otherVector)
            {
                double aMagnitude = getMagnitude();
                double bMagnitude = otherVector.getMagnitude();
                double theta = Math.Acos(getDotProduct(otherVector) /(aMagnitude * bMagnitude));
                theta = theta * (180 / Math.PI);
                return theta;

            }

    and I want to call the above method from another class how do I do that? do i create an object for otherVector?  Pleaseeee helpppp!!!

    • Changed type omfgcheesecake Thursday, November 07, 2013 11:45 PM Help
    Thursday, November 07, 2013 11:33 PM

All replies

  • If 'getAngleWetweenVector' was in a class called 'vector', you would call it like this:

    vector MyVector = new vector();
    vector.getAngleBetweenVector(new Vector2D(0,0));

    Don't include the first line if you already have a vector declared that you want to use. Here is the documentation on methods: http://msdn.microsoft.com/en-us/library/vstudio/ms173114.aspx


    Wasabi Fan

    Friday, November 08, 2013 12:05 AM
  • You will need to create an instance of the class that this method is in, say it is called 'manipulator'.  You will need to create an object for the parameter, say it is called 'magnitudeDirection'.  Then you call the method with

    double angle = manipulator.getAngleBetweenVector(magnitudeDirection);


    Paul Linton

    Friday, November 08, 2013 12:08 AM
  • Hi I have a 2 classes and one class contains the method:

      public double getAngleBetweenVector(Vector2D otherVector)
            {
                double aMagnitude = getMagnitude();
                double bMagnitude = otherVector.getMagnitude();
                double theta = Math.Acos(getDotProduct(otherVector) /(aMagnitude * bMagnitude));
                theta = theta * (180 / Math.PI);
                return theta;

            }

    and I want to call the above method from another class how do I do that? do i create an object for otherVector?  Pleaseeee helpppp!!!

    do whatevet you're already doing with the method getMagnitude - think about this...

    Cap'n

    Friday, November 08, 2013 3:37 AM