How to condense a multiple line declaration?

Currently, if I define a group of different rows, I do it like this:

string a = "A";
string b = "B";
string c = "C";
string d = "D";
string e = "E";

      

Is there an easier way to define all these lines on one line?

+3


source to share


4 answers


You can do

string a = "A", b = "B", c = "C", d = "D", e = "E";

      

even if I prefer one ad per line.



or maybe you need an array?

string strings[5] = {"A", "B", "C", "D", "E"};

      

and then use strings[0]

to strings[4]

.

+8


source


You can also do:

string a = "A",
       b = "B",
       c = "C",
       d = "D",
       e = "E";

      



Technically this is one liner, but it's easier for me to read than:

string a = "A", b = "B", c = "C", d = "D", e = "E";

      

+4


source


An easier way to do this would be

String letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

      

Then calling the corresponding letter with

letters[0]; //"A"
letters[1]; //"B"
//etc.

      

Alternatively, you can use the char value to get the correct index of the string containing letters.

+4


source


You can use Array, Vector, or List if you have the number of related strings as you defined.,.

+3


source







All Articles