0

i made a garagemanager program which contain vehicle class and classes which heritage from this class. while in the UI manager call to a methods which ask the user for details so i can construct a new vehicle object. that method give me the error :

An Object reference is required for non-static field, methods, or property.

the method i call is :

 //takeing a vehicle license number and creating a new vehicleStatusInfo.
    public VehicleStatusInfo GetNewVehicleStatusInfo(string i_LicenseNumber)
    {
        eVehicleType vehicleType = GetVehicleType();
        string vehicleOwnerName = GetVehicleOwnerName();
        string vehicleOwnerPhone = GetVehicleOwnerPhone();
        string modelName = GetVehicleModleName();
        eEnergyType energyType = getEnergyInput();
        float maxEnergy = ChooseNumOf("max energy of the vehicle (float)");
        float energyLeft = ChooseNumOf("energy Left in the vehicle (float)", 0, maxEnergy);
        List<Wheel> wheelList = GetWheelList();
        Vehicle newVehicle;
        switch (vehicleType)
        {
            case eVehicleType.MotoricCar:
                eColor motorCarcolor = GetColor();
                int motorCarNumOfDoors = GetNumOfDoors();
                newVehicle = new Car(modelName, i_LicenseNumber, energyLeft, wheelList, energyType, maxEnergy, motorCarcolor, motorCarNumOfDoors);
                break;
            case eVehicleType.ElectricCar:
                eColor elctCarColor = GetColor();
                int elctCarNumOfDoors = GetNumOfDoors();
                newVehicle = new Car(modelName, i_LicenseNumber, energyLeft, wheelList, energyType, maxEnergy, elctCarColor, elctCarNumOfDoors);
                break;
            case eVehicleType.MotoricBike:
                eLisenceSize licenseSize = GetLicenseSize();
                int engineCapacity = GetEngineCapacity();
                newVehicle = new Motorcycle(modelName, i_LicenseNumber, energyLeft, wheelList, energyType, maxEnergy, licenseSize, engineCapacity);
                break;
            case eVehicleType.ElecticBike:
                eLisenceSize elctLicenseSize = GetLicenseSize();
                int elctEngineCapacity = GetEngineCapacity();
                newVehicle = new Motorcycle(modelName, i_LicenseNumber, energyLeft, wheelList, energyType, maxEnergy, elctLicenseSize, elctEngineCapacity);
                break;
            case eVehicleType.Truck:
                bool hasDangerouseMaterial = IsDangerouse();
                float maxWeight = ChooseNumOf("max weight of the truck");
                newVehicle = new Truck(modelName, i_LicenseNumber, energyLeft, wheelList, energyType, maxEnergy,hasDangerouseMaterial, maxWeight);
                break;
            default:
                newVehicle = new Vehicle(modelName, i_LicenseNumber, energyLeft, wheelList, energyType, maxEnergy);
                break;
        }

and here is how i call the method :

newGarageManager.VehicleList.Add(GetNewVehicleStatusInfo(VehicleLicenseNum))

i can't make the method static since she use other get-from-user-methods.

  • The error is *very* clear. You are trying to call a method as if it were a static method. Are you trying to call it from inside another static method perhaps? In which case you should post at least the signature of the method that contains the calling line – Panagiotis Kanavos May 26 '16 at 08:33
  • Duplicate of http://stackoverflow.com/questions/498400/an-object-reference-is-required-for-the-nonstatic-field-method-or-property-on you will need to make the class non static or make your method static (and safe to be static). – Murray Foxcroft May 26 '16 at 08:34
  • I think you are class is not static. So you cant call like this... You should create a instance of that class to call it... – Nifal Nizar May 26 '16 at 08:37

0 Answers0