Leth
Leth

Reputation: 1059

Print object array

class Bil
{
    public string regnr
    {
        get { return regnr; }
        set { regnr = value; }
    }

    public string maerke
    {
        get { return maerke; }
        set { maerke = value; }
    }

    public int vaegt
    {
        get { return vaegt; }
        set { vaegt = value; }
    }

    public bool traek
    {
        get { return traek; }
        set { traek = value; }
    }

    public Bil(string regnr, string maerke, int vaegt, bool traek)
    {
        this.regnr = regnr;
        this.maerke = maerke;
        this.vaegt = vaegt;
        this.traek = traek;
    }

    public double StatsAfgift(int vaegt, bool traek)
    {
        this.vaegt = vaegt;
        this.traek = traek;
        double statsAfgift = 0;
        if (vaegt <= 800)
            statsAfgift = vaegt * 50;
        else if (vaegt > 800)
            statsAfgift = 800 * 50 + 75 * vaegt - 800;
        if (traek == true)
            statsAfgift = statsAfgift + 200;
        return statsAfgift;
    }
}

Bil Honda = new Bil("225689", "Honda", 500, true);
Console.WriteLine("Statsafgiften på bilen er afsat til: {0:C}", Honda.StatsAfgift(500, true));
Bil Citroen = new Bil("985632", "Citroen", 400, false);
Bil Peugeot = new Bil("125697", "Peugeot", 650, true);

Bil[] bilSamling = new Bil[3];
bilSamling[0] = Honda;
bilSamling[1] = Citroen;
bilSamling[2] = Peugeot;


foreach (Bil b in bilSamling)
{
    Console.WriteLine("Statsafgift på {0} er {1}", b.maerke, b.StatsAfgift(b.vaegt, b.traek))
}

Just to clarify, bil = car. Statsafgift = tax. Vaegt = weight, maerke = brand, regnr = registered number.

I need to print out the objects in the array along with each objects calculated tax. I am also getting some stack overflow errors when trying to compile the code.

Upvotes: 1

Views: 1326

Answers (2)

cirrus
cirrus

Reputation: 5662

Well your stackoverflow errors are coming from your properties. You need to define a local private variable to hold them (as they're currently calling themselves) or better still define them as auto-properties;

public bool traek { get; set; }

It doesn't look like you need to pass parameters through either, since they're already on the object;

public double StatsAfgift() // don't need params heere
{
    double statsAfgift = 0;
    if (vaegt <= 800)
        statsAfgift = vaegt * 50;
    else if (vaegt > 800)
        statsAfgift = 800 * 50 + 75 * vaegt - 800;
    if (traek == true)
        statsAfgift = statsAfgift + 200;
    return statsAfgift;
}

allowing you to just do;

Console.WriteLine("Statsafgift på {0} er {1}", b.maerke, b.StatsAfgift());

You could even do it on one like without the foreach loop;

Console.WriteLine(String.Join("\n", bilSamling.Select(b => String.Format("Statsafgift på {0} er {1}", b.maerke, b.StatsAfgift()))));

Upvotes: 1

Vladimirs
Vladimirs

Reputation: 8599

Problem is with

public string regnr 
            {
                get { return regnr; }
                set { regnr = value; }
            }

Try to use auto property instead

public string Regnr { get; set; }

Overflow raised when you try assign regnr = value; for that purpose you should use other variable like that:

private string _regnr;

public string Regnr
{
      get { return _regnr; }
      set { _regnr = value; }
}

Upvotes: 2

Related Questions