L
L
Lord Drous2017-11-13 14:43:30
OOP
Lord Drous, 2017-11-13 14:43:30

Who can help with the OOP problem?

Hello everyone, we started to study OOP, this is the task, I wrote it myself according to the way I know, using an example, but the
code errors

namespace OOP4
{
    class PatchCord
    {
        private string name;
        private string type;
        private int length;

        public PatchCord(string name, string type, int length)
        {
            this.name = name;
            this.type = type;
            this.length = length;
        }
    }

    class PatchCords
    {
        PatchCord[] patchCords = new PatchCord[5];

        public PatchCord this[int length]
        {
            set { patchCords[length] = value; }
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            PatchCords A = new PatchCords();
            A[0] = new PatchCord("sds","dsd", 2);
            for (int i = 0; i < 2; i++)
            {
                Console.Write(A[i].length);
            }
        }
    }
}

Task:
Create objects of the class <class name> (the class and its fields are defined in accordance with the selected option in lab 1), and the class objects must store arrays of data about the subject area associated with the properties of the subject of the task by option. Moreover, the number of array elements must be set programmatically by the user in an interactive mode.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Eremin, 2017-11-13
@EreminD

Well, look
While, of course, it’s not clear what kind of errors you are getting
And if you wrote them, things would go faster
For now, I can say that it’s not very clear here

class PatchCords
    {
        PatchCord[] patchCords = new PatchCord[5];

        public PatchCord this[int length]
        {
            set { patchCords[length] = value; }
        }
    }

I see that in PatchCords you create a patchCords array with a dimension of 5.
You need to understand that an array is an array for that, that you will not change its dimension later (in fact, you can, but this is not what you need now )
Start small - put your array creation in your class constructor
class PatchCords
    {
        PatchCord[] patchCords;

        public PatchCords(int size){
           patchCords = new PatchCord[size];
        }
    }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question