-2

Who ya gonna call? Unrelated code to pose the problem. Not the best editor tool so being terse. Thanks.

A new method that is part of the derived class cannot be accessed by the new object. All the Intellisense sees are the abstract parts of the base class. Typing them in and running them gets an error. If methods and fields can't be added what is the point of base to derived and on down. I have searched all examples and come up empty.

public class SalesEmployee : Employee
{
    public decimal salesbonus; // Additional field

    public SalesEmployee(string name, decimal basepay, decimal salesbonus)
    {
        this.salesbonus = salesbonus; // Create new field
    }

    public override decimal CalculatePay() // Override abstract
    {
        return basepay + salesbonus;
    }

    public decimal CalculateExtraBonus() // Not an override 
    {
        return basepay + (0.5 * salesbonus); // Belongs to this class only
    }

}

static void Main()
{
    // Create new employee.
    SalesEmployee employee1 = new SalesEmployee("Alice", 1000, 500);

    decimal = employee1.CalculateExtraBonus(); // Can't see the new method
            // Derived class cannot get to new method.
}

I'm thinking of trying the following. Typing out questions really helps.

{  SalesEmployee salesEmpInstance = employee1 

    decimal = salesEmpInstance.CalculateExtraBonus() 
             // Maybe this could see the method.
Elliot
  • 101
  • Code looks fine just add proper semicolons. Have you restarted your IDE (VisualStudio)? – jegtugado Jul 21 '16 at 03:38
  • 1
    you need to give a name to your decimal variable at the bottom. – Mike Marynowski Jul 21 '16 at 03:40
  • 2
    I think you forgot variable name : `decimal bonus = employee1.CalculateExtraBonus();` –  Jul 21 '16 at 03:41
  • I tried out what I suggested and it did actually work. Would still like an answer to explain what I have been missing. – Elliot Jul 22 '16 at 01:49
  • As I say I typed it out into the only text I could get going so it isn't perfect. Any idea of the answer t my question? Thanks. – Elliot Jul 22 '16 at 01:51

2 Answers2

0

I'll ignore the syntax errors and assume you have it right in your actual code. However it looks like you forgot to call the parents constructor ("base") on your derived class constructor and then tried to access a variable only instantiated by the parent. Also you need to cast the literal "0.5" as a decimal.

You can read more about "base" on msdn

Working code is below. Output is 1250.

using System;

public abstract class Employee
{
    public string name;
    public decimal basepay;
    public Employee(string name, decimal basepay)
    {
        this.name = name;
        this.basepay = basepay;
    }
    public abstract decimal CalculatePay();
}

public class SalesEmployee : Employee
{
    public decimal salesbonus; // Additional field

    // -->ERROR HERE, You forgot to call the base and instantiate
    // the fields of the parent.
    public SalesEmployee(string name, decimal basepay, decimal salesbonus): base(name, basepay)
    {
        this.salesbonus = salesbonus; // Create new field
    }

    public override decimal CalculatePay() // Override abstract
    {
        return basepay + salesbonus;
    }

    public decimal CalculateExtraBonus() // Not an override 
    {
        return basepay + ((decimal)0.5 * salesbonus); // Belongs to this class only
    }
}

class Program
{
    static void Main(string[] args)
    {
        SalesEmployee employee1 = new SalesEmployee("Alice", 1000, 500);
        decimal aliceBonus = employee1.CalculateExtraBonus();
        Console.WriteLine(aliceBonus);
    }
}
Brennen Sprimont
  • 1,564
  • 15
  • 28
0

Your code looks fine, I recommend you:

  1. Remove the reference to the first class library from your new project.
  2. Rebuild your first class library
  3. Reference again the first class library dll file in your new project, maybe you referenced to an older version of the dll. You should reference to the last created dll of first class library
  4. Correct your code like this

.

{
    SalesEmployee employee1 = new SalesEmployee("Alice", 1000, 500);
    SalesEmployee salesEmpInstance = employee1 ;
    decimal result = salesEmpInstance.CalculateExtraBonus(); 
}

Anyway if you have not any reference in this case. Compair the following codes with your code. I tested it. It works...

enter image description here

Note1: You should use base in your constructor to pass name and basepay to their corresponding fields in base class.

Note2: Now Rebuild your project, have you any error? I have not! Have you VS Intellisense problem yet?

enter image description here

Ramin Bateni
  • 16,499
  • 9
  • 69
  • 98
  • The question did not involve any Library references. All were part of the same class plus an additional file for Main. You have repeated the code as it was shown not to work. – Elliot Jul 24 '16 at 22:17
  • @user2863749, Yes, you did not write about it but it could be possible when i don't know your project structure correctly (you did not provide enough information about it). Also you did not provide Employee class in your question. I edited my answer please check it again. – Ramin Bateni Jul 25 '16 at 06:13