Last post Dec 04, 2018 01:22 PM by bjeako
Member
12 Points
37 Posts
Dec 04, 2018 10:53 AM|bjeako|LINK
Hi
I currently use the following:
@{ string sTitle = Model.prop == 1 ? "ABC" : "DEF"; }
How can I remove the string sTitle and directly check if the prop = 1 or not?
All-Star
194865 Points
28100 Posts
Moderator
Dec 04, 2018 11:30 AM|Mikesdotnetting|LINK
if(Model.prop == 1) { // do something } else { // do something else }
if(Model.prop == 1)
{
// do something
}
else
// do something else
Is that what you are after?
Dec 04, 2018 12:31 PM|bjeako|LINK
Yes it is basically the same. But can I use the ? : syntax instead of the whole if else, if that is possible.
Dec 04, 2018 01:17 PM|Mikesdotnetting|LINK
Do you mean that instead of an assignment, you want to do something like :
x == 1 ? SomeMethod() : AnotherMethod(); If so, you can't do that I'm afraid. The conditional operator ?: is used to return a value: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/conditional-operator
Dec 04, 2018 01:22 PM|bjeako|LINK
Ah I see! I will just the if else structure then. Thanks for your answers!
Member
12 Points
37 Posts
if else statement with ? & :
Dec 04, 2018 10:53 AM|bjeako|LINK
Hi
I currently use the following:
How can I remove the string sTitle and directly check if the prop = 1 or not?
All-Star
194865 Points
28100 Posts
Moderator
Re: if else statement with ? & :
Dec 04, 2018 11:30 AM|Mikesdotnetting|LINK
if(Model.prop == 1)
{
// do something
}
else
{
// do something else
}
Is that what you are after?
Member
12 Points
37 Posts
Re: if else statement with ? & :
Dec 04, 2018 12:31 PM|bjeako|LINK
Yes it is basically the same. But can I use the ? : syntax instead of the whole if else, if that is possible.
All-Star
194865 Points
28100 Posts
Moderator
Re: if else statement with ? & :
Dec 04, 2018 01:17 PM|Mikesdotnetting|LINK
Do you mean that instead of an assignment, you want to do something like :
x == 1 ? SomeMethod() : AnotherMethod();
If so, you can't do that I'm afraid. The conditional operator ?: is used to return a value: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/conditional-operator
Member
12 Points
37 Posts
Re: if else statement with ? & :
Dec 04, 2018 01:22 PM|bjeako|LINK
Ah I see! I will just the if else structure then. Thanks for your answers!