Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Inline if using c#

Posted on 2014-12-07
6
Medium Priority
?
146 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 34

Assisted Solution

by:Mike Eghtebas
Mike Eghtebas earned 1500 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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 

Author Comment

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

Accepted Solution

by:
Mike Eghtebas earned 1500 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses

783 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