Solved

Error when overriding base class constructor in subclass

Posted on 2008-10-05
7
1,084 Views
Last Modified: 2013-12-17
I receive the following error when I try to override a base class's constructor in a subclass.

An object reference is required for the non-static field, method, or property 'Chapter_6_project_3.Bee.weight'

attached is the relevant code, with a comment showing where the error is received.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Chapter_6_project_3;
 
namespace chapter_6_project_3
{
    class Worker : Bee
    {
        public Worker(string[] jobsICanDo) : base(weight) //ERROR HERE!!!! it's for the base's parameter weight
        {
            this.jobsICanDo = jobsICanDo;
        }
 
        public override int ShiftsLeft
        {
            get
            {
                return shiftsToWork - shiftsWorked;
            }
        }
 
        private string currentJob = "";
        private string[] jobsICanDo;
        private int shiftsToWork;
        private int shiftsWorked;
 
        public string CurrentJob
        { 
            get
            {
                return currentJob;
            }
        }
 
        public bool DoThisJob(String potentialJob, int potentialShiftsToWork)
        {
            if (!String.IsNullOrEmpty(CurrentJob))
                return false;
 
            for (int i = 0; i < jobsICanDo.Length; i++)
            {
                if (potentialJob == jobsICanDo[i])
                {
                    currentJob = potentialJob;
                    shiftsToWork = potentialShiftsToWork;
                    return true;
                }
            }
 
            return false;
        }
 
        public bool WorkOneShift()
        {
            if (String.IsNullOrEmpty(currentJob))
                return false;
            shiftsWorked++;
 
            if (shiftsWorked > shiftsToWork)
            {
                shiftsWorked = 0;
                shiftsToWork = 0;
                currentJob = "";
                return true;
            }
            else
                return false;
        }
    }
}
 
 
//following ith base class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace Chapter_6_project_3
{
    class Bee
    {
        public Bee(double weight)
        {
            this.weight = weight;
        }
 
        public virtual int ShiftsLeft
        {
            get
            {
                return 0;
            }
        }
 
        private double weight;
 
        public virtual double GetHoneyConsumption()
        {
            double consumption;
            if (ShiftsLeft == 0)
                consumption = 7.5;
            else
                consumption = 9 + ShiftsLeft;
 
            if (weight > 150)
                consumption *= 1.35;
 
            return consumption;
        }
    }
}

Open in new window

0
Comment
Question by:jabrthel
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 6

Expert Comment

by:RishadanPort
ID: 22647549
yes there is no varible defined "weight"
0
 
LVL 6

Accepted Solution

by:
RishadanPort earned 125 total points
ID: 22647558
You need to declare your Worked constructor differently in order for it to work, like this:

public Worker(string[] jobsICanDo, double weight) : base(weight) <-- this will work
0
 

Author Comment

by:jabrthel
ID: 22647563
Yeah... but it's a parameter passed into the base class.  Isn't the structure as follows...
public class Class1 : base (baseParam1, baseParam2, etc.)
{
}

Open in new window

0
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 

Author Comment

by:jabrthel
ID: 22647566
Oh... ok, I see
0
 

Author Comment

by:jabrthel
ID: 22647573
Yup, that's it.
0
 

Author Closing Comment

by:jabrthel
ID: 31503314
Well, that was a beginner's mistake.
0
 
LVL 6

Expert Comment

by:RishadanPort
ID: 22647583
using the keyword "base" means that you are dealing with the parent object.

If you do this:

base()   or base(  ... parameters ... )

You are constructing your parent object in a certain way.


You can also do this:

base.someMethod() or base.someVariable    as well
0

Featured Post

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
collection output issue 9 67
Easy filter aspnet 2 35
Why use this lambda? 12 66
I need help changing the default value for an @HTML.LabelFor control 4 38
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question