I have one class called Class1.cs

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

namespace static2 
{ 
public class Automobile
{
    public static int NumberOfWheels = 4;
    public static int SizeOfGasTank
    {
        get
        {
            return 15;
        }
    }

    public  int compute(int a,int b)

    {
        int c = a*b;
        return c ;

    }

    public static void Drive() { }
    //public static event EventType RunOutOfGas;

    // Other non-static fields and properties...
}


}

I am trying to use the compute method from Class1 under Program.cs

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

using  static2 ;

namespace static1
{
    class Program
    {
        static void Main(string[] args)
        {

            Automobile.Drive();
            int i = Automobile.NumberOfWheels;



            Automobile car = new Automobile.compute(2,3);


            Console.Write(car);

            Console.WriteLine("sdfsdfds");

        }
    }
}

it says Automobile.compute(2,3) is a method but it is used like a type ?

Recommended Answers

I beleive your code should be changed to:

 Automobile car = new Automobile();
 int x = car.compute(2,3);
Jump to Post

All 2 Replies

I beleive your code should be changed to:

 Automobile car = new Automobile();
 int x = car.compute(2,3);

As JorgeM said, the class needs instantiating first.

You could also make the method static and then it would function how you are trying to do at present.

Static methods do not require class instantiation in order to be used.

    public static int compute(int a,int b)
    {
        return a*b;
    }

and

    int answer = Automobile.compute(2,3);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.19 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.