Nested Class Help in C#

232 Views Asked by At

Two-part question

Working on an MDI program designed to keep track of any number of generic stores' inventories utilizing a snippet of code our teacher provided us. My thought process was "A store has a name and a record of items," so the class definition below represents the extent of what I have defined a store to be.

Part 1) How do I create an array of unknown quantity of class Record within class Store? The idea being that a store won't be limited to, say, 100 different items. For each item, there's one Record, and this should be able to account for adding a new one.

Part 2) How would I construct the class outside of this one? Basically, I'm going to have a window where it asks for information about the item (name, ID Num, etc.). How would I create a new Record to place within Store?

Thanks for the help. Class definition is below.

using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Inventory
{
    class Store
    {
        public Store() { }
        public Store(string name) { }
        public string name { get; set; }

        [Serializable]
        class Record
        {
            public Record() { }
            public Record(int ID, int Quantity, double Price, string Name) { }
            public int id { get; set; }
            public int quantity { get; set; }
            public double price { get; set; }
            public string name { get; set; }
        }
    }
}
1

There are 1 best solutions below

3
On BEST ANSWER

Just define the classes separately and define a collection of one inside the other.

I used a private setter so you can only initialize it inside the class, then add and remove items from outside the class.

namespace Inventory
{
    class Store
    {
        public Store() : this(null) { }
        public Store(string name) {
             Records = new List<Record>();
        }
        public string name { get; set; }

        public List<Record> Records { get; private set; }
    }

    class Record
    {
        public Record() { }
        public Record(int ID, int Quantity, double Price, string Name) { }
        public int id { get; set; }
        public int quantity { get; set; }
        public double price { get; set; }
        public string name { get; set; }
    }
}