Solved

One new error

Posted on 2008-09-30
4
216 Views
Last Modified: 2012-05-05
Error is at base(fatgrams,8)

An object reference is required for the nonstatic field,method, or property 'ConsoleApplication2.FoodItem.fatgrams


using System;
using System.Runtime.InteropServices;

namespace ConsoleApplication2
{
      /// <summary>
      /// Summary description for FoodItem.
      /// </summary>
      public class FoodItem
      {
            private int  Calories_Per_Gram=9;
            protected  int fatgrams;
            protected int servings;            
      
            public FoodItem(int Fatgrams1,int servings1)
            {
                  fatgrams=Fatgrams1;
                  servings=servings1;
            }
            
            private int Calories()
            {
                  return fatgrams *Calories_Per_Gram ;
            }

            public int CaloriesPerSavings()
            {      
                  return(Calories ()/servings );
            }
}

      public class Pizza :  FoodItem
      {
            public Pizza (int FatGrams):base(fatgrams,8)
            {
                  
            }
            static  void Main(string[] args)
            {
                  Pizza special=new Pizza (275);
                  Console.WriteLine("Calories per serving: " + special.CaloriesPerSavings());
            }
      }
}
      

0
Comment
Question by:sarang8180
[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
  • 2
4 Comments
 
LVL 20

Expert Comment

by:informaniac
ID: 22604606
Here you'll have to declare a variable by name fatgrams...

public Pizza (int FatGrams):base(fatgrams,8)

public Pizza (int FatGrams):base(100,8) should work
0
 
LVL 4

Expert Comment

by:alaran
ID: 22604738
Seems you're trying to pass FatGrams from the constructor of class Pizza to it's base class constructor.
In that case the problem is case sesitivity. The parameter name in the Pizza constructor to pass to the base constuctor must be the same as you specify in the base constructor parameter list, like so:

public Pizza(int FatGrams) : base(FatGrams, 8)  { }

I would also suggest you start parameter and variable names with a lower case character, so you easily see the difference between a parameter/variable and a property or class. Also you avoid things like this: Pizza Pizza = new Pizza();

With this advise your constructor should look something like tis:

public Pizza(int fatGrams) : base(fatGrams, 8)  { }

A note on informaniac's "solution":
public Pizza (int FatGrams):base(100,8)

This will always pass 100 as the fatGrams parameter to the base class (FoodItem), whitch results in a Pizza always having a fatGrams of a houndred.

I.e. Pizza myPizza = new Pizza(500); will still have a fatgrams value of 100, since 500 is never passed to the base class.
0
 

Author Comment

by:sarang8180
ID: 22611681
to refer fatgrams(ie. a protected variable.) in inheriting class.how to create instance of base class(in the above example).because,it is not accepting below thing:


public Pizza(int fatGrams) : base(fatGrams, 8)  { }
0
 
LVL 4

Accepted Solution

by:
alaran earned 500 total points
ID: 22612950
To create an instance of the base class you do: FoodItem item = new FoodItem(10, 8);
There should be no problem accessing any of the protected variables from within the Pizza class, if you see one what is the error message?
If you want to access it like: Pizza pizza = new Pizza(100); int fat = pizza.fatGrams; this will fail. But if you need to you can make it a property (which seems natural in any case).

Make it read-only:

public int FatGrams
{
    get { return _fatGrams; }
}

Readable and writable:

public int FatGrams
{
    get { return _fatGrams; }
    set { _fatGrams = value; }
}
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

690 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