How to put information of a class in an array?

0

To save lines of code, I do the following:

PictureBox[] pic = { pictureBox1, pictureBox2, pictureBox3, pictureBox4, pictureBox5, pictureBox6, pictureBox7, pictureBox8 };

And I have a class called InformationLetters , whose same has several variables, I use it like this:

InformationLetters.Carta1 = "";, etc.

In order not to use IL.Carta1, IL.Carta2 ...., I wanted a way to put it in an array . For example:

Array[] Nome = { InformationLetter.Carta1, InformationLetter.Carta2 };

And use it like this:

Nome[0] = ""; Nome[1] = "";

How can I do this?

Follow the current code and how you are occupying unneeded lines:

    
asked by anonymous 11.10.2017 / 07:59

1 answer

0

Programming is about conceptualizing well, it's about writing meaningful codes. It's not about typing less. What you are trying to do is make the code unreadable and create maintenance problems. Do not do this. Make it readable.

But you may have an engineering problem there. I can not give details because the question does not give details.

If you have variables with the same prefix in the name and then a sequential number, it might be the case to change this. You could use a

pictureBox[i]

And one

Carta[i]

Even if you want to insist on doing this is to function as you are thinking of doing, as long as those Cartax are of a type by reference, because in array you will have a reference for the equal data has InformationLetters.Cartax (where x there is only one convention to show that is the number of each). They are accessing the same object, so changing it in one place changes the other.

If the type is by value only if a C # day allows you to use ref in the array element. Today I would have to create an infrastructure that would deal with this, I do not think it's worth the effort.

If the problem is the number of lines, each if there is occupying 4 lines, but it is possible to occupy 1, and if bobear becomes more readable.

I do not know the whole code, nor does it have a clear context, but I wonder if the loop of the j variable is necessary. If there is an individual treatment for each value of j , it is redundant.

It's all a matter of concept, write the code to show what you want, not to have fewer lines. If you need to handle items individually then do so, if you need to treat items as a sequence ( array ), then set them to use so later.

    
11.10.2017 / 13:17