0

I am trying to call the method "BeräknaLön" which is located in another class than the click event method. The click event method resides in the partial class "PersonalRegister" and the method which I intend to call is located in the class "Säljare". Thanks in advance //Thorin

Form1.cs:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PersonalRegister
{
    public partial class Personalregister : Form
    {
        //Deklarering av lista över anställda
        List<Anställd> anställda = new List<Anställd>();
        Anställd medarbetare;

        public Personalregister()
        {
            InitializeComponent();
        }
        private void Btn_reg_försäljare_Click(object sender, EventArgs e)
        {
            //Deklarera variabler för namn, provision samt försäljning och tilldela värden
            //från textrutor
            string n = tbx_säljare_namn.Text;
            double p = Convert.ToDouble(tbx_säljare_provision.Text);
            double f = Convert.ToDouble(tbx_säljare_försäljning.Text);

            tbxml_register.AppendText(" " + n + " (Säljare) \r\n");

            Säljare.BeräknaLön(); //Error message suggests an "Object reference" is required here

        }
        private void Btn_reg_arbetspass_Click(object sender, EventArgs e)
        {
            string n = tbx_Konsult_namn.Text;
            double t = Convert.ToDouble(tbx_Konsult_timlön.Text);
            double a = Convert.ToDouble(tbx_Konsult_timmar.Text);

            tbxml_register.AppendText(" " + n + " (Konsult) \r\n");
        }
        private void Btn_reg_mLön_Click(object sender, EventArgs e)
        {
            string n = tbx_kontorist_namn.Text;
            double m = Convert.ToDouble(tbx_kontorist_mLön.Text);

            tbxml_register.AppendText(" " + n + " (Kontorist) \r\n");
        }
        private void Btn_beräkna_löner_Click(object sender, EventArgs e)
        {

        }
    }
}

The class "Säljare"

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PersonalRegister
{
    class Säljare : Anställd
    {
        double provision;
        double försäljning;

        public Säljare(string n, double p, double f)
        {
            this.namn = n;
            this.provision = p;
            this.försäljning = f;
        }
        public override double BeräknaLön()
        {
            return försäljning * (provision / 100);
        }
    }
}
1
  • Säljare is a non static class, therefore you must first create an object of it like so: Säljare saljare = new Säljare();. Commented Oct 10, 2019 at 18:44

1 Answer 1

1

Since BeräknaLön is an instance method (as opposed to a static method), you need to create an instance of the Säljare class before you can call it, which is exactly what the error message is telling you.

For example:

private void Btn_reg_försäljare_Click(object sender, EventArgs e)
{
    string name = tbx_säljare_namn.Text;
    double provision = Convert.ToDouble(tbx_säljare_provision.Text);
    double sales = Convert.ToDouble(tbx_säljare_försäljning.Text);

    tbxml_register.AppendText(" " + n + " (Säljare) \r\n");

    // Create a new instance of our class
    Säljare seller = new Säljare(name, provision, sales);

    // Now we can call the method on the instance we just created
    double salary = seller.BeräknaLön();
}
Sign up to request clarification or add additional context in comments.

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.