0

I have the following piece of code which I am using to set a series of labels to invisible when a testtype 2 is being performed

        if ((int)testType == 2)
            indmetaL.Visible = false;
        if ((int)testType == 2)
            midmetaL.Visible = false;
        if ((int)testType == 2)
            rinmetaL.Visible = false;
        if ((int)testType == 2)
            litmetaL.Visible = false;

I have tried structuring these statements like this:

 if ((int)testType == 2)
       indmetaL.Visible = false;
       midmetaL.Visible = false;
       rinmetaL.Visible = false
       litmetaL.Visible = false;

however when I do the midmetaL, rinmetaL and litmetaL stay invisible during testtype 1

I am sure there is a way to set all these 'visible' properties to false in one line however I am not sure of the syntax, any help is appreciated, thanks!

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939

5 Answers5

3
if ((int)testType == 2)
{
       indmetaL.Visible = false;
       midmetaL.Visible = false;
       rinmetaL.Visible = false
       litmetaL.Visible = false;
}
gordy
  • 9,360
  • 1
  • 31
  • 43
2

Either use curly braces to create code block:

if ((int)testType == 2)
{
   indmetaL.Visible = false;
   midmetaL.Visible = false;
   rinmetaL.Visible = false
   litmetaL.Visible = false;
}

or use chain assignment:

if ((int)testType == 2)
   indmetaL.Visible = midmetaL.Visible = rinmetaL.Visible = litmetaL.Visible = false;
Ňuf
  • 6,027
  • 2
  • 23
  • 26
  • whoa I didn't know you could do this in C# +1 for learning something new. May as well make it a one-liner and assign them all to `((int)testType == 2) – gordy Apr 29 '12 at 02:06
1

You should use curly braces to define a statement block for the if, otherwise only the first line belongs to it. So this works:

if ((int)testType == 2)
{
    indmetaL.Visible = false;
    midmetaL.Visible = false;
    rinmetaL.Visible = false
    litmetaL.Visible = false;
}

Why is it considered a bad practice to omit curly braces?

Community
  • 1
  • 1
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
1

Use curly braces to group multiple statements into a block;

if ((int) testType == 2)
{
    indmetaL.Visible = false;
    midmetaL.Visible = false;
    rinmetaL.Visible = false;
    litmetaL.Visible = false;
}

In C#, the if statement conditionally executes only the immediately following statement or block. Thus, you need the curly braces. The same is generally true of other flow control statements such as for, while, etc.

Mike Mertsock
  • 11,825
  • 7
  • 42
  • 75
  • Right, that's the point. But that may not be obvious to less experienced programmers. Thus I explicitly said "statement or block" in case one isn't familiar with that equivalence. – Mike Mertsock Apr 29 '12 at 02:01
0

use Switch statements. They are faster than if statements because they directly jump to the correct case.

switch((int)testType)
{
    case 2:
        indmetaL.Visible = false;
        midmetaL.Visible = false;
        rinmetaL.Visible = false;
        litmetaL.Visible = false;
    break;
}

or if you want if statement you can write it in a single line without curly braces like this

if((int)testType==2)
    indmetaL.Visible = midmetaL.Visible = rinmetaL.Visible = litmetaL.Visible = false;
Nikhil Agrawal
  • 47,018
  • 22
  • 121
  • 208