Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8746367
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T12:10:58+00:00 2026-06-13T12:10:58+00:00

I am working with abstract classes and virtual methods. Currently I have a window

  • 0

I am working with abstract classes and virtual methods. Currently I have a window form composed of a button click and two multiline textboxes where I am displaying the results. The base class has a default constructor and a constructor that accepts all the necessary data the base needs. There are two classes Trees and Tomatoes that have a default constructor and a constructor that is passed all the data. I am able to display in textBox1 the item name, number in stock and price.

However I am not able to call and display for Trees the height variable named tree_height and the Tomatoes size and tomatoes per plat variables named tomatoes_perplat and tomatoes_size. It gives me the error does not contain a definition for those variables. How can I fix that?

CODE

namespace plant_shop
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        public abstract class Plants
        {
            public string the_name;
            public double num_stock;
            public double price_peritem;
            public double total_item_value;

            public Plants(string new_name, int new_stock, double new_price)
            {
                the_name = new_name;
                num_stock = new_stock;
                price_peritem = new_price;
            }

            public override string ToString()
            {
                return "";
            }

            public virtual double Get_Value()
            {
                double s = 0;
                return s;
            }


        }

        public class Trees : Plants
        {
            double tree_height;
            public Trees(string new_name, int new_stock, double new_price, double new_height)
                : base(new_name, new_stock, new_price)
            {
                tree_height = new_height;
                total_item_value = num_stock * price_peritem;
            }

            public override string ToString()
            {
                string s = "Tree" + "     " + num_stock + "      " +  the_name + "     " + price_peritem + "    " + tree_height ;
                return s;
            }

            public override double Get_Value()
            {
                total_item_value = num_stock * price_peritem;
                return total_item_value;
            }

        }

        public class Tomatoes : Plants
        {

            string sizeoftomato;
            int tomatoesinpat;

            public Tomatoes(string new_name, int new_stock, double new_price, int tomatoes_perplat, string tomatoes_size)
                : base(new_name, new_stock, new_price)
            {
                tomatoesinpat = tomatoes_perplat;
                sizeoftomato = tomatoes_size;  
                total_item_value = num_stock * price_peritem; 
            }

            public override string ToString()
            {
                string s = "Tomatoes" + "     " + num_stock + "      " + the_name + "     " + price_peritem;
                return s;
            }

            public override double Get_Value()
            {

                total_item_value = num_stock * price_peritem;
                return total_item_value;
            }
        }



        public void Report()
        {

             Trees trees1 = new Trees("Oak", 3, 14.40, 2);
             const string format = "{0,-26} {1,-25} {2,-25} {3,-25}";
             string trees1_result = String.Format(format, "Tree", trees1.the_name, trees1.num_stock, trees1.price_peritem, trees1.tree_height); //not showing `tree_height`            
             textBox1.AppendText(trees1_result + Environment.NewLine);
             textBox2.AppendText(trees1.Get_Value() + Environment.NewLine);            

             Tomatoes tomatoes1 = new Tomatoes("Big Boy", 30, 10, 12, "Large");
             const string format2 = "{0,-26} {1,-25} {2,-25} {3,-25}";
             string tomatoes1_result = String.Format(format2, "Tomatoe", tomatoes1.the_name, tomatoes1.num_stock, tomatoes1.price_peritem, tomatoes1.tomatoes_perplat, tomatoes1.tomatoes_size); //not showing `tomatoes_perplat` or `tomatoes_size`
             textBox1.AppendText(tomatoes1_result + Environment.NewLine);
             textBox2.AppendText(tomatoes1.Get_Value() + Environment.NewLine);

        }

        private void button1_Click(object sender, EventArgs e)
        {
            Report();
        }

    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T12:10:58+00:00Added an answer on June 13, 2026 at 12:10 pm

    Okay, now I’ve got the code somewhat compiling, it’s clearer…

    You can’t access trees1.tree_height because it’s private within the Trees class, and you’re trying to access it from a method in the Form1 class.

    You can’t access tomatos_perplat from the method because there’s no such variable in the Tomatoes class – there’s tomatoesinpat, but that’s private too.

    Ditto tomatoes_size – the field is called sizeoftomato, and is private.

    Keeping your fields private is a good idea – I don’t recommend making them public. Instead, you should expose properties to expose data between classes. You can use automatically implemented properties as an easy way to implement very simple properties.

    I’d strongly encourage you to work on your organization though:

    • Don’t use nested classes for everything; they’re handy occasionally, but it’s usually better to have separate top-level classes

    • Read the .NET naming conventions and follow them

    • Think about what your methods are trying to achieve – things like your Get_Value method are pointless at the moment

    • I would urge you to experiment with console applications. They’re less “exciting” than GUIs, but are much simpler to understand and experiment with.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on an app where I have two very similar model classes. I
I have a couple of pure virtual classes, Matrix and Vector . Throughout my
This is for .net MVC project i'm working on I have the following classes:
I've been working with some abstract classes and something feels off but I'm not
I'm working on an application where I have a plugin-style architecture with multiple abstract
I am working on creating abstract activity classes for my application that I will
I have a base class DockedToolWindow : Form, and many classes that derive from
In a proof-of-concept project I'm working on, I have a generic abstract class that
I am working in AS3. We have an abstract class, call it MyAbstractClass (I
I have been working on a program that has 3 classes of which 2

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.