How to put class information in an array?

Asked

Viewed 171 times

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 it has several variables, I use it so:

InformationLetters.Carta1 = "";, etc.

To keep from using IL.Cart1, IL.Cart2...., I wanted a way to put her in a array. For example:

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

And use it like this:

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

How can I do that?

Follow the current code and how it is occupying unnecessary lines:

Código

  • fors with ifs for all indices are always wrong at the design level. I suggest you do not put the code as picture. See here why. The InformationLetter was set where and how? The same question to the CartasInformacao

1 answer

0


Programming is about conceptualizing well, it’s about writing significant code. It’s not about typing less. What you’re trying to do is make the code unreadable and create maintenance issues. Don’t do it. Make it readable.

But you may have an engineering problem there. I can’t give details because the question doesn’t give details.

If you have variables with the same prefix in the name and then a sequential number, maybe you should change this. You could use a

pictureBox[i]

And a

Carta[i]

I put in the Github for future reference.

Even if you want to insist on doing it this way it’s to work as you’re thinking of doing, as long as these CartaX are of a type by reference because array shall have a reference to the equal data in InformationLetters.CartaX (where X there is only a convention to show that is the number of each). They are accessing the same object, therefore changing in one place changes in the other.

If type is by value only if day C# allows use ref in the element of array. Today I would have to create an infrastructure that would handle it, I don’t 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 don’t know the whole code, nor does it have a clear context, but I wonder if the loop of the variable j is necessary. If there is an individual treatment for each value of j, he is redundant.

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.