Solved

C# Single line if Statement return Null

Posted on 2007-11-24
19
750 Views
Last Modified: 2013-12-16
I have C# enum. If selected value is Customer(ie value 0) i want to return NULL in a Single line If Statement.
I am getting syntax Errors
 
public enum tables
  {
   customer
   Orders
   OrderDetail
}


public int GetTable(tables tableEnum)
     {
       int iTable = (int)tableEnum;  -- iTable returns 0,1 or 2 as per ENUM
     
            iTable= (iTable == 0) ? int? iTable= null  : iTable;    ---- Synatx Errors
0
Comment
Question by:dotnet0824
[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
  • 7
  • 7
  • 2
  • +2
19 Comments
 
LVL 10

Expert Comment

by:Oliver Amaya
ID: 20343369
Hi, should be:
iTable = (iTable == 0)?null:iTable;

Open in new window

0
 

Author Comment

by:dotnet0824
ID: 20343383
Hi,
I tried that I get Error "There is no implicit conversion between Null and Int"... Looks like we have to convert Int To Null as iTable is int    at ?Null
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20343392
why to convert to int previously to evaluation? enum advantage is precisely the legibility for evaluation.
Also, an integer cannot be null, so you have to use the Nullable type.
Something like:


public int? GetTable(tables tableEnum)
{
    return (tableEnum==tables.Customer) ? null : (int)tableEnum;
}

Open in new window

0
Increase Agility with Enabled Toolchains

Connect your existing build, deployment, management, monitoring, and collaboration platforms. From Puppet to Chef, HipChat to Slack, ServiceNow to JIRA, Splunk to New Relic and beyond, hand off data between systems to engage the right people.

Connect with xMatters.

 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20343402
This will need to receive the result of GetTable in a similar variable, like:
int? result = GetTable(........);
if (result.HasValue)
{
    // do something with result.Value
}

But this is not  agood approach, maybe you can solve it by returning -1 instead of null, so you don't have to use the nullable type.
0
 

Author Comment

by:dotnet0824
ID: 20343404
Hi Jaime,
Sorry the function return DataType is Dataset . I am v sorry here it is

public DataSet GetTable(tables tableEnum)
     {
       int iTable = (int)tableEnum;  -- iTable returns 0,1 or 2 as per ENUM
     
            iTable= (iTable == 0) ? int? iTable= null  : iTable;    ---- Synatx Errors
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20343419
well, I am more confused now, maybe you need:

public DataSet GetTable(tables tableEnum)
{
    return (tableEnum==tables.Customer) ? null : someCollection[(int)tableEnum];
}

But better send the rest of the code to understand you. I am suspecting you need DataTable as a return value, not DataSet
0
 
LVL 19

Expert Comment

by:SteveH_UK
ID: 20343477
The only way you can return NULL is by having a type that supports NULL, which an Enum does not directly.

However, from .NET 2.0, you have access to generics and this lets you have a type that can be Null.  It is called Nullable and is in the System namespace.

Note that you haven't told us anything about where you are getting your DataSet from, and I suspect that this is not what you are actually trying to achieve.
public Nullable(Of DataSet)(tables tableEnum)
{
  return (tableEnum==tables.Customer) ? null : myDataSet;
}

Open in new window

0
 

Author Comment

by:dotnet0824
ID: 20343484
its pretty simple James... inCase 0 is passed in Enum I want to pass @iTable value as NULL or else 1 or 2 as per ENUM Thats all i need now ... Only thing i need is if TableENUM value is 0 (ie Customer Passed in) I Want the variable iTable to take Null
public DataSet GetCustomers(tables tableEnum)
     {
       int iTable = (int)tableEnum;  -- iTable returns 0,1 or 2 as per ENUM
           db.AddInParameter(Command, "@Enabled", DbType.Int32, @iTable);
0
 
LVL 29

Expert Comment

by:Göran Andersson
ID: 20343539
Here is how you create a value that you can use in a data parameter:

object value = tableEnum==tables.Customer ? DbNull.Value : (int)tableEnum;
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20343548
@steve,
The nullable type is unnecessary in your example since DataSet is a reference type and can be null.
0
 

Author Comment

by:dotnet0824
ID: 20343571
James.. I am trying out what u had given

   tableEnum==tables.Customer ? null : (int)tableEnum;  ===Syntax Error

    I want to pass that NULL value to the following parameter
 db.AddInParameter(Command, "@Enabled", DbType.Int32, PassValueAsVariableFromENUM);
 
0
 
LVL 19

Expert Comment

by:SteveH_UK
ID: 20343591
jaime, this is true, my mistake.  I was still thinking in terms of the Enum...
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20343595
As I stated you don't have to convert to int to evaluate, just evaluate at enum level.
With the last code snippet you have posted, the GreenGhost suggestion appear to be well. So, maybe this is what you need:

       object value = tableEnum==tables.Customer ? DbNull.Value : (int)tableEnum;

           db.AddInParameter(Command, "@Enabled", DbType.Int32, value);
0
 

Author Comment

by:dotnet0824
ID: 20343627
i tried out this
object value = tableEnum==tables.Customer ? DbNull.Value : (int)tableEnum;
--Error There is no implicit conversion between DbNull and Int
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20343757
better you do:
object value;
if (tableEnum==tables.Customer)
       value = DbNull.Value;
else
       value = (int)tableEnum;
0
 

Author Comment

by:dotnet0824
ID: 20343773
cant it be done in a Single Line If Statement..........Can u please check this   Should we use ?int which is nullable int..  
http://www.experts-exchange.com/Programming/Languages/C_Sharp/Q_22157027.html?sfQueryTermInfo=1+convert+int+null
0
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 200 total points
ID: 20343856
it can be as my first suggestion:

int? value = (tableEnum==tables.Customer) ? null : (int?)tableEnum;
0
 
LVL 29

Assisted Solution

by:Göran Andersson
Göran Andersson earned 100 total points
ID: 20343874
You just have to cast the values to object:

object value = tableEnum==tables.Customer ? (object)DbNull.Value : (object)(int)tableEnum;
0
 

Author Comment

by:dotnet0824
ID: 20343958
Thanks Jaime. I tried out ur last Posting and it worked Great.
Thanks GreenGhost. Converting to object worked too. I have increased points by 100 and assigning
0

Featured Post

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

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 …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…

690 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