Azurewrath

Is it possible to write this:

if (this.WindowState == FormWindowState.Normal)
{
Settings.Default.WindowSize = this.Size;
}
else
{
Settings.Default.WindowSize = this.RestoreBounds.Size;
}

like this:

Settings.Default.WindowSize = if (this.WindowState == FormWindowState.Normal) this.Size; else this.RestoreBounds.Size;

If so, what's the cleanest way (as in indentation and formatting)

Thanks,

aw



Re: Visual C# Language Setting a value based on if condition

Azurewrath

Anyone knows the answer, because I can't get it to work.

Thanks,

aw





Re: Visual C# Language Setting a value based on if condition

IsshouFuuraibou

if you want to do inline conditional statements use <condition> value1:value2;

ie

Code Snippet

Settings.Default.WindowSize = (this.WindowState == FormWindowState.Normal) this.Size: this.RestoreBounds.Size;







Re: Visual C# Language Setting a value based on if condition

Azurewrath

Thanks man. I forgot that method.

Do you think I use write all this in 1 line

Thanks,

aw





Re: Visual C# Language Setting a value based on if condition

IsshouFuuraibou

This situation it makes sense to write it all on one line, you're only doing conditional assignment. It really depends mostly on what is happening inside the code on how you should format it for display.





Re: Visual C# Language Setting a value based on if condition

Azurewrath

Thanks man.

aw





Re: Visual C# Language Setting a value based on if condition

Matthew Watson

Personally, I think writing all that one one line is just horrible.

* It makes it much harder to read.
* There is no benefit to doing it at all (it doesn't make it faster).





Re: Visual C# Language Setting a value based on if condition

Azurewrath

Thanks Matthew. Can you please show me your formatting for this

aw





Re: Visual C# Language Setting a value based on if condition

IsshouFuuraibou

I'd actually agree with Matthew on this. I tend to prefer if/else statements more than :

if (this.WindowState == FormWindowState.Normal)
Settings.Default.WindowSize = this.Size;
else
Settings.Default.WindowSize = this.RestoreBounds.Size;

or
if (this.WindowState == FormWindowState.Normal)
{
Settings.Default.WindowSize = this.Size;
}
else
{
Settings.Default.WindowSize = this.RestoreBounds.Size;
}

would be how I'd format it. It's always a good idea to leave braces, but isn't a requirement. Leaving braces allows you to modify what is done in each case without worrying about needing to add the braces, it also very clearly shows what is each case.

There are cases where formatting it with inline conditional would be advantageous, but this case is fairly open to personal preference or a decision in local coding standards. I've know people who prefer : over if/else. Ternary operators ( : ) are an option, they are not "better" than if/else, if/else isn't "better" than ternary operators. There are times when you can't use a ternary operator, and times when using an if/else is not feasible or desired but a ternary operator is. It's important to know and use all your tools.

In general, if/else statements are verbose about what the condition and cases are, so it's a lot easier to read (especially for people without a strong C background...)





Re: Visual C# Language Setting a value based on if condition

ShellShock

The ternary condition operator is useful because it evaluates to an expression whereas the standard if does not. You can therefore use the ternary operator wherever you would use an expression. I use the ternary operator for simple conditions because in my opinion it can make code MORE readable.

Compare these two methods:

Code Snippet

public int Foo(bool flag)

{

int result = 0;

if (flag)

{

result = 1;

}

else

{

result = 2;

}

return result;

}

This can be rewritten as:

Code Snippet

public int Foo(bool flag)

{

return flag 1 : 2;

}

I much prefer the second style, but I understand it gives some people the heebie-jeebies.

In true functional languages assignment is considered to be a side effect. Every time we assign a value to a variable we have to mentally track that variable as a side effect. The computer has lodged the variable somewhere in its memory and we need to do the same when we are trying to understand what the method does. This applies to the variable "result" in my first example. It is only at the end of the first method that it becomes apparent that "result" is the value of the method. With my second example it is immediately apparent what the value of the method is, and we do not have any side effects to worry about.

Although this is a simple example it can be applied to many situations. But like many things in life, the ternary operator can be abused if used inappropriately.

I think the only think that makes the ternary operator difficult for beginners is the rather cryptic I think the only think that makes the ternary operator difficult for beginners is the rather cryptic symbols chosen (originally in C) for the operators. Imagine the syntax was slightly different:

Code Snippet

public int Foo(bool flag)

{

return if (flag) 1 else 2;

}

This is semantically the same as the : operator, just with more readable syntax. I hope this demonstrates that semantically the ternary operator can be easier to understand, it is just the syntax that takes a bit of getting used to (and worth it in my opinion).





Re: Visual C# Language Setting a value based on if condition

Hugo

Try considering this, if the logic is needed frequently in several places then this is worth considering:

Instead of:

if (this.WindowState == FormWindowState.Normal)
Settings.Default.WindowSize = this.Size;
else
Settings.Default.WindowSize = this.RestoreBounds.Size;

Code this

Setting.Default.WindowSize = GetDesiredSize (this);

Now of course, GetDesiredSize will pretty much be this:

private Size GetDesiredSize (Form form)

{

if (form.WindowState == Normal)
return(this.Size);

return(this.RestoreBounds.Size);

}

This means that the logic and algorithm for determinig size is encapsulated, and could be modifed without impacting using code.

Of course if its used once in the logic, then it may be overkill to do this and is a matter of taste. Passing in 'this' might be unnecessary too, so this is just an example of an alternative way.





Re: Visual C# Language Setting a value based on if condition

Azurewrath

Thanks guys, I appreciate the info.

ShellShock now I see why if else didn't work in my assignment, because I thought it would evaluate to an expression. I will make another thread about it.

Thanks again,

aw