Solved

Inline if using c#

Posted on 2014-12-07
6
105 Views
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.

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
0
Comment
Question by:ksd123
6 Comments
 
LVL 12

Expert Comment

by:FarWest
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
regards
0
 

Author Comment

by:ksd123
ID: 40485621
Just to understand my requirement I gave above example.In reality it is different,  but I have similar requirement as above.
0
 
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)
        {
            MessageBox.Show(FindResponse(0).ToString());
        }
        private string FindResponse(int indx)
        {
            string msg="";
            switch (indx)
            {
                case 0:
                    msg = "Hi";
                    break;
                case 1:
                    msg = "Hello";
                    break;
                case 2:
                    msg = "Welcome";
                    break;
                case 3:
                    msg = "Thank you";
                    break;
            }
            return msg;
        }

Open in new window

0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

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

Accepted Solution

by:
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

use:

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

Expert Comment

by:louisfr
ID: 40486294
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

0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

707 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

13 Experts available now in Live!

Get 1:1 Help Now