Solved

One new error

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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

Suggested Solutions

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

739 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