nimmy nimmy - 3 months ago 11
ASP.NET (C#) Question

needed to know how to make the code better

variable = reader.GetInt32(reader.GetOrdinal("seatno"));

if (variable == 1)
{
ImageButton img1 = (ImageButton)GridView2.Rows[0].Cells[0].FindControl("ImageButton4");

img1.ImageUrl = "~/images/saveseat.jpg";
img1.Enabled = false;



}
if (variable == 2)
{
ImageButton img1 = (ImageButton)GridView2.Rows[0].Cells[0].FindControl("ImageButton6");

img1.ImageUrl = "~/images/saveseat.jpg";
img1.Enabled = false;
}
if (variable == 3)
{
ImageButton img1 = (ImageButton)GridView2.Rows[0].Cells[0].FindControl("ImageButton7");

img1.ImageUrl = "~/images/saveseat.jpg";
img1.Enabled = false;
}
if (variable == 4)
{
ImageButton img1 = (ImageButton)GridView2.Rows[0].Cells[0].FindControl("ImageButton8");

img1.ImageUrl = "~/images/saveseat.jpg";
img1.Enabled = false;
}
if (variable == 5)
{
ImageButton img1 = (ImageButton)GridView2.Rows[0].Cells[0].FindControl("ImageButton9");

img1.ImageUrl = "~/images/saveseat.jpg";
img1.Enabled = false;
}

if (variable == 6)
{
ImageButton img1 = (ImageButton)GridView2.Rows[0].Cells[0].FindControl("ImageButton10");

img1.ImageUrl = "~/images/saveseat.jpg";
img1.Enabled = false;
}
if (variable == 7)
{
ImageButton img1 = (ImageButton)GridView2.Rows[0].Cells[0].FindControl("ImageButton11");

img1.ImageUrl = "~/images/saveseat.jpg";
img1.Enabled = false;
}
if (variable == 8)
{
ImageButton img1 = (ImageButton)GridView2.Rows[0].Cells[0].FindControl("ImageButton12");

img1.ImageUrl = "~/images/saveseat.jpg";
img1.Enabled = false;
}
if (variable == 9)
{
ImageButton img1 = (ImageButton)GridView2.Rows[0].Cells[0].FindControl("ImageButton13");

img1.ImageUrl = "~/images/saveseat.jpg";
img1.Enabled = false;
}
if (variable == 10)
{
ImageButton img1 = (ImageButton)GridView2.Rows[0].Cells[0].FindControl("ImageButton14");
// ImageButton img2 = (ImageButton)img1.FindControl("ImageButton4");
img1.ImageUrl = "~/images/saveseat.jpg";
img1.Enabled = false;
}
if (variable == 11)
{
ImageButton img1 = (ImageButton)GridView2.Rows[0].Cells[0].FindControl("ImageButton15");
// ImageButton img2 = (ImageButton)img1.FindControl("ImageButton4");
img1.ImageUrl = "~/images/saveseat.jpg";
img1.Enabled = false;
}
if (variable == 12)
{
ImageButton img1 = (ImageButton)GridView2.Rows[0].Cells[0].FindControl("ImageButton20");

img1.ImageUrl = "~/images/saveseat.jpg";
img1.Enabled = false;
}
if (variable == 13)
{
ImageButton img1 = (ImageButton)GridView2.Rows[0].Cells[0].FindControl("ImageButton21");

img1.ImageUrl = "~/images/saveseat.jpg";
img1.Enabled = false;
}
if (variable == 14)
{
ImageButton img1 = (ImageButton)GridView2.Rows[0].Cells[0].FindControl("ImageButton22");

img1.ImageUrl = "~/images/saveseat.jpg";
img1.Enabled = false;
}
if (variable == 15)
{
ImageButton img1 = (ImageButton)GridView2.Rows[0].Cells[0].FindControl("ImageButton23");

img1.ImageUrl = "~/images/saveseat.jpg";
img1.Enabled = false;
}
if (variable == 16)
{
ImageButton img1 = (ImageButton)GridView2.Rows[0].Cells[0].FindControl("ImageButton24");

img1.ImageUrl = "~/images/saveseat.jpg";
img1.Enabled = false;
}
if (variable == 17)
{
ImageButton img1 = (ImageButton)GridView2.Rows[0].Cells[0].FindControl("ImageButton25");

img1.ImageUrl = "~/images/saveseat.jpg";
img1.Enabled = false;
}
if (variable == 18)
{
ImageButton img1 = (ImageButton)GridView2.Rows[0].Cells[0].FindControl("ImageButton26");

img1.ImageUrl = "~/images/saveseat.jpg";
img1.Enabled = false;
}
if (variable == 19)
{
ImageButton img1 = (ImageButton)GridView2.Rows[0].Cells[0].FindControl("ImageButton27");

img1.ImageUrl = "~/images/saveseat.jpg";
img1.Enabled = false;
}
if (variable == 20)
{
ImageButton img1 = (ImageButton)GridView2.Rows[0].Cells[0].FindControl("ImageButton15");

img1.ImageUrl = "~/images/saveseat.jpg";
img1.Enabled = false;
}
if (variable == 21)
{
ImageButton img1 = (ImageButton)GridView2.Rows[0].Cells[0].FindControl("ImageButton26");
// ImageButton img2 = (ImageButton)img1.FindControl("ImageButton4");
img1.ImageUrl = "~/images/saveseat.jpg";
img1.Enabled = false;
}


please tell me how i can make this code better and short.here i have to go through each and every if condition.which is time consuming.i need to make it such that if the variable value is say 4 it has to go to 4directly instead of going through every if.

if (image4click)
{
a = 1;

}
if (image4click1)
{
a = 2;
}
if (image4click2)
{
a = 3;
}
if (image4click3)
{
a = 4;
}
if (image4click4)
{
a = 5;
}
if (image4click5)
{
a = 6;
}


in this image4click is boolean variable.how to shorten this make it better.

Answer

I'm not sure which language you are using, but there is something called switch-case in programming.

    switch 
      case 1:
         do this...
      case 2: 
         do this...
      case 3
         do this...
    end

Obviously, you would have to look at the syntax of the programming language you are using. Hope this helps!

Comments