Solved

Error when overriding base class constructor in subclass

Posted on 2008-10-05
7
1,080 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
  • 4
  • 3
7 Comments
 
LVL 6

Expert Comment

by:RishadanPort
Comment Utility
yes there is no varible defined "weight"
0
 
LVL 6

Accepted Solution

by:
RishadanPort earned 125 total points
Comment Utility
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
Comment Utility
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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:jabrthel
Comment Utility
Oh... ok, I see
0
 

Author Comment

by:jabrthel
Comment Utility
Yup, that's it.
0
 

Author Closing Comment

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

Expert Comment

by:RishadanPort
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
DataGridView 2 29
PHP question(s) about order of output 9 39
crm development 2 35
Spacing between controls 4 15
There is an easy way, in .NET, to centralize the treatment of all unexpected errors. First of all, instead of launching the application directly in a Form, you need first to write a Sub called Main, in a module. Then, set the Startup Object to th…
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

743 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now