简体   繁体   中英

Getters Setters Autoproperty issue

Having an issue with autoproperty. Here's snippet from my class:

 class Car
{
    string brand;
    string model;
    Engine CarEngine {get; set;}

Now the class Engine:

class Engine
{
    double FuelCap { get; }
    double fuelCapDefault = 50;
    double FuelCount { get; set;}
    double engineCap;

I created an object with constructor:

  public Car(string brand, string model, double engineCap, double FuelCount, double FuelCap)
    {
        this.brand = brand;
        this.model = model;
        this.CarEngine = new Engine(engineCap, FuelCount, FuelCap);

    }

and the constructor for Engine:

public Engine(double engineCap, double FuelCount, double FuelCap)
    {
        this.engineCap = engineCap;
        this.FuelCount = FuelCount;
        this.FuelCap = FuelCap;
    }

I've created an object with:

 Car car = new Car(x, y, z, a, b);

Now to the main point, finally:

I'm trying to access car.CarEngine.FuelCount in my Main Program to get the value, but Visual doesn't even suggest me the CarEngine. I'm new to the whole autoproperty stuff. What did I do wrong?

All the properties that you have declared doesn't have any modifier , so by default they are private . This means that they can only be accessed within the declaring class.

Mark them public will solve the problem.

All types and type members have an accessibility level, which controls whether they can be used from other code in your assembly or other assemblies. You can use the following access modifiers to specify the accessibility of a type or member when you declare it:

public The type or member can be accessed by any other code in the same assembly or another assembly that references it.

private The type or member can be accessed only by code in the same class or struct.

For the full list see Access Modifiers (C# Programming Guide)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM