Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

One new error

Posted on 2008-09-30
4
Medium Priority
?
228 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 2000 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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Suggested Courses

618 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