Smart grid 5x5 alphabets in C #

I need to print a 5x5 table with alphabets in them, for example:

<table>
 <tr> <td>A</td> <td>B</td> <td>C</td> <td>D</td> <td>D</td>

etc. the letters are actually a link, so they will be like this:

<td> <a href='/someplace'>A</a> </td>

These links tend to change frequently, and I don’t want to hardcode and replace them, as they will appear on several pages. So I decided to write a function to output the whole structure.

Yes, this is pretty straight forward, make the loop forbehave like this:

  StringBuilder alphabets = new StringBuilder("<table class='table'>");
  for(int i=65; i<=87; i++)
  {
      //Do stuff here to calculate if i mod 5 is zero and add <tr> accordingly.
      //Use Convert.ToChar(i); to get the wanted structure.
  }

Then it hit me, I could do it better, in a "smart" way, using nested for loops,

for(i=1; i<=5; i++)
{
    alpbahets.Append("<tr>")
    for(j=1; j<5; j++)
    {
        //Get the <a > link string here.
    }
    alphabets.Append("</tr");
}

Now the question is, what can I do to bind iAND jto get them in the range 65-87? (AW, since this is a 5x5 grid, I will skip the last iteration and manually add YZto one td).

(i*10 + j) + 54) (, , ), .

, , for? ? , ( , ).

+3
5

, char letter = 'A'; ?

, , letter++;, .

, j, .

- :)

+5
for(i=0; i<5; i++)
{
  alpbahets.Append("<tr>")
  for(j=0; j<5; j++)
  {
        int val = 65 + i*5 +j;
        //get the <a > link string here
  }
  alphabets.Append("</tr");
}
+2

j, :

 int x = (i*5) + j + 65;
 if (x<=87)
     printCharacter(x);
+1

, char,

, , , , - .

StringBuilder alphabets = new StringBuilder("<table class='table'>"); 
for(int i=0; i<=5; i++)
{
  alphabets.Append("<tr>\n");
  for(int j=0; j<5; j++)
  {
        alphabets.Append("<td><a href=\""+"linkGoesHere"+"\">"+ (65+i*5+j<91?Convert.ToChar(65+i*5+j):' ') +"</a></td>\n");
  }
  alphabets.Append("</tr>");
}
alphabets.ToString().Dump();

Pay attention to the ternary operator so that the last few cells are empty, not special characters.

+1
source

If you can output them as floating divs inside the container, and not as table cells, you can use the front-end repeater control (containing one floating div) and bind the repeater to the array of required letters (also takes care of Y and Z).

0
source

Source: https://habr.com/ru/post/1791196/


All Articles