?
Solved

Inline if using c#

Posted on 2014-12-07
6
Medium Priority
?
133 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 

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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

718 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