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!

1
  • @ChaosPandion he's probably just a Python developer writing Python code in C# ;)
    – gordy
    Apr 29, 2012 at 1:59

5 Answers 5

3
if ((int)testType == 2)
{
       indmetaL.Visible = false;
       midmetaL.Visible = false;
       rinmetaL.Visible = false
       litmetaL.Visible = false;
}
2
  • I knew it was something ridiculously simple, this is what I get for working too late, thank you :D Apr 29, 2012 at 1:57
  • I think a bit more explanation is necessary for me to up vote. Such a fundamental concept should be understood before writing serious code. Apr 29, 2012 at 2:02
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;
1
  • 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, 2012 at 2: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?

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.

1
  • 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. Apr 29, 2012 at 2: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;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.