Solved

One new error

Posted on 2008-09-30
4
200 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
  • 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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

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

9 Experts available now in Live!

Get 1:1 Help Now