Inline if using c#

Posted on 2014-12-07
Last Modified: 2014-12-28
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.




Public bool IsDisplayName


  int? paramvalue=Getvalue();

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



Open in new window


//Checking condition for ToDisplayName or Not

FieldSettings Prjconf=new FieldSettings();

//do something

//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)


ToDisplayMobile(4 values)

3=Thank you
Question by:ksd123
LVL 12

Expert Comment

ID: 40485597
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

Author Comment

ID: 40485621
Just to understand my requirement I gave above example.In reality it is different,  but I have similar requirement as above.
LVL 33

Assisted Solution

by:Mike Eghtebas
Mike Eghtebas earned 500 total points
ID: 40485840
In place of inline if, you could use a function call like:

This way you can have as many options you like. You need to make it work with your project.

     private void button1_Click(object sender, EventArgs e)
        private string FindResponse(int indx)
            string msg="";
            switch (indx)
                case 0:
                    msg = "Hi";
                case 1:
                    msg = "Hello";
                case 2:
                    msg = "Welcome";
                case 3:
                    msg = "Thank you";
            return msg;

Open in new window

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.


Author Comment

ID: 40486063
Please check my original question,I am looking for best way of achieving my task.
LVL 33

Accepted Solution

Mike Eghtebas earned 500 total points
ID: 40486076
You add one simple function and that is it. I think it is simple all right. Inline if never can handle more than two.

Instead of:

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


return paramvalue.Hasvalue && FindResponse(1)
LVL 11

Expert Comment

ID: 40486294
You're making things more complicated than they are.
1) About the conditional operator.
xx ? true : false

Open in new window

is exactly the same thing as

Open in new window

2) About nullable types
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


Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Resolve Dependency Issues 4 47
What .NET URL re-routing tool did I use? 2 37
Help with C#, MVC, razor. 6 23
What namespace do I need to import? 2 8
Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now