A
A
Alexey Smirnov2017-11-06 15:57:11
.NET
Alexey Smirnov, 2017-11-06 15:57:11

How to create a "database" in a separate class?

Hello.
I have some database. For example, the weight, length and material of each product.

Data

Изделие "Брусок" имеет следующие параметры:
int weightOfProduct = 2; // Вес изделия в килограммах.
int lengthOfProduct = 50; // Длина изделия в сантиметрах.
string materialOfProduct = "Дерево"; // Материал изделия.

Изделие "Арматура" имеет параметры:
int weightOfProduct = 4;
int lengthOfProduct = 200;
string materialOfProduct = "Сталь";

Изделие "Гвоздь" имеет параметры:
int weightOfProduct = 1;
int lengthOfProduct = 25;
string materialOfProduct = "Сталь";


I need to store this database in a separate "Products" class, so that when accessed from a method of any other class, I can pass the name of one product as a string ( ), and get back the values ​​​​of the parameters of this product (weightOfProduct, lengthOfProduct, materialOfProduct). How can this be implemented in C# ? PS Perhaps I shouldn't have put the complexity of the question simple. Maybe it's Middle. P.S. I did it this way:"Брусок" или "Арматура" или "Гвоздь"
Spoiler
class Products
    {
        public string weightOfProduct { get; set; } // Вес изделия в килограммах.
        public int lengthOfProduct { get; set; } // Длина изделия в сантиметрах.
        public int materialOfProduct { get; set; } // Материал изделия.
    }
    class Block : Products
    {
        public int weightOfProduct = 2;
        public int lengthOfProduct = 50;
        public string materialOfProduct = "Дерево";
    }
    class Armature : Products
    {
        public int weightOfProduct = 4;
        public int lengthOfProduct = 200;
        public string materialOfProduct = "Сталь";
    }
    class Nail : Products
    {
        public int weightOfProduct = 1;
        public int lengthOfProduct = 25;
        public string materialOfProduct = "Сталь";
    }


But I don't know how to pass a string variable further and get back the values ​​of the variables corresponding to this string variable. PPS I have nothing to do with construction. A fictional example. "Брусок" или "Арматура" или "Гвоздь"

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
Johnny Gat, 2017-11-06
@ERAFY

Product class (if you want to use get and set, do it right):

/// <summary>
    /// Изделие.
    /// </summary>
    class Product
    {
        private int _weight; // Вес изделия в килограммах.
        private int _length; // Длина изделия в сантиметрах.
        private string _material; // Материал изделия.

        /// <summary>
        /// Вес изделия в килограммах.
        /// </summary>
        public int Weight
        {
            get
            {
                return _weight;
            }
            set
            {
                _weight = value;
            }
        }

        /// <summary>
        /// Длина изделия в сантиметрах.
        /// </summary>
        public int Length
        {
            get
            {
                return _length;
            }
            set
            {
                _length = value;
            }
        }

        /// <summary>
        /// Длина изделия в сантиметрах.
        /// </summary>
        public string Material
        {
            get
            {
                return _material;
            }
            set
            {
                _material = value;
            }
        }

        /// <summary>
        /// Конструктор класса.
        /// </summary>
        public Product(int weight, int length, string material)
        {
            _weight = weight;
            _length = length;
            _material = material;
        }
    }

Completing the product list:
Dictionary<string, Product> products = new Dictionary<string, Product>();
products.Add("Брусок", new Product(2, 50, "Дерево"));
products.Add("Арматура", new Product(4, 200, "Сталь"));
products.Add("Гвоздь", new Product(1, 25, "Сталь"));

Receiving product parameters:
string materialArmature = products["Арматура"].Material;
int blockLength = products["Брусок"].Length;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question