Solved

Inline if using c#

Posted on 2014-12-07
6
107 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VB.NET - Error - Object not set in Module 4 33
Modal Popup Extender control 1 20
VB.NET 2008 - SQL Timeout 9 24
JSON  parse help 7 26
Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net 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…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

773 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