Link to home
Start Free TrialLog in
Avatar of ksd123
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

Public bool IsDisplayName
{

get

{
  int? paramvalue=Getvalue();

  return paramvalue.Hasvalue && paramvalue.value=1?true:false


}

}

Open in new window



Class1.cs


//Checking condition for ToDisplayName or Not

FieldSettings Prjconf=new FieldSettings();

if(Prjconf.IsDisplayName)
{
//do something
}

else
{
//do something
}

Open in new window


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
Avatar of FarWest
FarWest

Me too, I became very confused :)
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
Avatar of ksd123

ASKER

Just to understand my requirement I gave above example.In reality it is different,  but I have similar requirement as above.
SOLUTION
Avatar of Mike Eghtebas
Mike Eghtebas
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ksd123

ASKER

Please check my original question,I am looking for best way of achieving my task.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of louisfr
You're making things more complicated than they are.
1) About the conditional operator.
This:
xx ? true : false

Open in new window

is exactly the same thing as
xx

Open in new window

2) About nullable types
This:
xx.HasValue && xx.Value == yy

Open in new window

is nearly the same as
xx == yy

Open in new window


In short, this
return paramvalue.Hasvalue && paramvalue.value==1?true:false

Open in new window

can be rewritten as
return paramvalue == 1

Open in new window