ksd123
asked on
Inline if using c#
Hi Experts,
I have to display fields in UI based on Field setting file. In this file we can set each field to some value (0,1,2,3,4 etc) and retrieve this information and will show/hide/do something for the field in the UI. For instance to display a Name field it has just 2 values (0 and 1). Here is the code using inline if statement that will check if it is set to 1 or 0.
FieldSettings.cs
//ToDisplayName
0=show
1=HIDE
Class1.cs
//Checking condition for ToDisplayName or Not
Similarly I have below fields that have 3 and 4 values.I am confused if I should return bool or int and how to write them using inline (Fieldsettings.cs) and retrieve this information in class.cs file.
ToDisPlayAge(3 values)
0=No
1=Yes-Hide
2=Yes-Show
ToDisplayMobile(4 values)
0=Hi
1=Hello
2=welcome
3=Thank you
I have to display fields in UI based on Field setting file. In this file we can set each field to some value (0,1,2,3,4 etc) and retrieve this information and will show/hide/do something for the field in the UI. For instance to display a Name field it has just 2 values (0 and 1). Here is the code using inline if statement that will check if it is set to 1 or 0.
FieldSettings.cs
//ToDisplayName
0=show
1=HIDE
Public bool IsDisplayName
{
get
{
int? paramvalue=Getvalue();
return paramvalue.Hasvalue && paramvalue.value=1?true:false
}
}
Class1.cs
//Checking condition for ToDisplayName or Not
FieldSettings Prjconf=new FieldSettings();
if(Prjconf.IsDisplayName)
{
//do something
}
else
{
//do something
}
Similarly I have below fields that have 3 and 4 values.I am confused if I should return bool or int and how to write them using inline (Fieldsettings.cs) and retrieve this information in class.cs file.
ToDisPlayAge(3 values)
0=No
1=Yes-Hide
2=Yes-Show
ToDisplayMobile(4 values)
0=Hi
1=Hello
2=welcome
3=Thank you
ASKER
Just to understand my requirement I gave above example.In reality it is different, but I have similar requirement as above.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Please check my original question,I am looking for best way of achieving my task.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
You're making things more complicated than they are.
1) About the conditional operator.
This:
This:
In short, this
1) About the conditional operator.
This:
xx ? true : false
is exactly the same thing as
xx
2) About nullable typesThis:
xx.HasValue && xx.Value == yy
is nearly the same as
xx == yy
In short, this
return paramvalue.Hasvalue && paramvalue.value==1?true:false
can be rewritten as
return paramvalue == 1
i did not get how and why "ToDisplayMobile" will return "welcome" or "Hello"
my advise is to use strong typing and user structures as datatypes for those values
since you using full coding block then no need to inline if just make your code clear
using if blocks
regards