1
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.

2
  • Is this C#? You should tag your question with the programming language you need help with. Commented Nov 21, 2012 at 12:05
  • Sorry about that. The laungauge is C#. Commented Nov 21, 2012 at 12:09

2 Answers 2

2

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; }
}
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.