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 ?

I beleive your code should be changed to:

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

Edited 2 Years Ago by JorgeM

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);

Edited 2 Years Ago by Mike Askew

This article has been dead for over six months. Start a new discussion instead.