0

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 ?

3
Contributors
2
Replies
26
Views
3 Years
Discussion Span
Last Post by Mike Askew
0

I beleive your code should be changed to:

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

Edited by JorgeM

0

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 by Mike Askew

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.