?
Solved

One new error

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

Technology Partners: 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

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses
Course of the Month14 days, 20 hours left to enroll

840 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