0

pretty straight-forward question, I get an error I don't know how to solve, in Visual C# Express, when I call the Romberg function in my main (I provided the error message at the appropriate line). I tried using this.Romberg too but it didn't help. What should I do differently?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Romberg2
{
    class test
    {
        // the function
        double f(double x)
        {
            double f;
            f=8*(Math.Sqrt(1-x*x)-x);
            return f;
        }

        // The Integration function
        void Romberg(double a, double b, int n, double[,] R)
        {
            int i, j, k;
            double h, sum;

            h = b - a;
            R[0,0]=(h/2)*(f(a)+f(b));

            for (i=1; i<=n; i++)
            {
                h=h/2;
                sum=0;

                for (k=1; k <= (Math.Pow(2.0,i)-1); k +=2)
                    sum +=f(a+k*h);

                R[i,0]=R[i-1,0]/2+sum*h;

                for(j=1; j<=i; j++)
                    R[i,j]=R[i,j-1]+(R[i,j-1]-R[i-1,j-1])/(Math.Pow(4.0,j)-1);
            }
        }

        static void main(int argc, char [] argv)
        {
            double[,] R = new double [10,10];
            int n=5, digits=13;
            double a=0, b=1/Math.Sqrt(2.0);

            // Call the integrating function
            Romberg(a, b, n, R);//ERROR: An object reference is required for the non-static field, method, or property

            // Printout the diagram
            for(int i=0; i<n; i++)
            {
                for(int j=0; j<=i; j++)
                {
                    Console.WriteLine(R[i,j] + " ");
                }
                Console.WriteLine("\n");
            }

        }
    }
}
2
  • add static to void Romberg -> static void Romberg Commented May 7, 2012 at 21:32
  • @Marc Finally something I knew the answer too :( Commented May 7, 2012 at 21:40

8 Answers 8

2

Romberg is not a static method ,so you should call it via an instance using the new keyword

new test().Romberg(a, b, n, R);

or simply make it a static function

static void Romberg(double a, double b, int n, double[,] R)
Sign up to request clarification or add additional context in comments.

Comments

2

main is static, so it can't call instance methods. Declare Romberg as static, or create an instance of test and use that instance:

var inst = new test();
inst.Romberg(a, b, n, R);

For more information, please check out the MSDN article on Static Classes and Static Class Members.

Comments

2

Main is a static method, Roomberg is an instance method. Either create a new instance of the class inside Main and call it through the instance, or make Roomberg static also.

Comments

1

Three problems:

1) Main is not the correct format. It needs to be in one of these formats:

static void Main() {...} 
static void Main(string[] args) {... } 
static int Main() {...} 
static int Main(string [] args) {...}

Just change it to 'static void Main()'

2) Make Romberg method static

static void Romberg(double a, double b, int n, double[,] R)

3) Make f method static:

static double f(double x)

Comments

1

You need to make your Romberg function static, or you could wrap it in it's own class, instantiate that and execute it.

Comments

1

You need to call that method on an instance. That method belongs to instance of the class.

var inst = new test();
inst.Romberg(q, w, e, R);

Comments

1

Try this. To call Romberg(a, b, n, R); you have to create test class object first. Romberg is a instance method.

test obj=new test();
obj.Romberg(a, b, n, R);

Comments

0

you are tryin to call method from class that is not initializedt.

 static void Romberg(double a, double b, int n, double[,] R)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.