D
D
djolil2021-09-12 22:49:04
C++ / C#
djolil, 2021-09-12 22:49:04

How to create a custom data type?

How to create your own data type?
The Equipment class, which describes the properties of the weapon (int damage, string name and its methods). How to write an array of instances of this class to the database? I used json:

JSON_FILE

{
  "player": [
    [
      {
        "name": "sword",
        "attack_bonus": 2,
        "defence_bonus": 0,
        "cost": 10
      },
      {
        "name": "tornado",
        "attack_bonus": 5,
        "defence_bonus": 0,
        "cost": 25
      }
    ]
}


Equipment class

public class Equipment
    {
        public string name;
        public int attack_bonus;
        public int defence_bonus;
        public int cost;

        public Equipment(string name, int attack_bonus, int defence_bonus, int cost)
        {
            this.name = name;
            this.attack_bonus = attack_bonus;
            this.defence_bonus = defence_bonus;
            this.cost = cost;
        }

        public int get_attack_bonus()
        {
            return this.attack_bonus;
        }
        public int get_defence_bonus()
        {
            return this.defence_bonus;
        }
        public int get_cost()
        {
            return this.cost;
        }
        public string get_name()
        {
            return this.name;
        }

        public string get_descr()
        {
            string descr = get_name() + "  :";

            if (get_attack_bonus() != 0)
            {
                descr += "  attack +" + get_attack_bonus();
            }
            if (get_defence_bonus() != 0)
            {
                descr += "  defence +" + get_defence_bonus();
            }

            descr += "  cost $" + get_cost();

            return descr;
        }
    }

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Developer, 2021-09-12
@samodum

Create multiple tables and establish relationships between them

K
Konstantin Tsvetkov, 2021-09-13
@tsklab

JSON data in SQL Server .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question