Click here to Skip to main content
15,848,415 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
if (DmApiCacheUpdateStrategy.Equals("Delete"))
{
    subscribers.Add(new Subscriber()
    {
        Arguments = DmProject.GetDmProjectPath(dmProject),
        ExecutablePath = _apiDataPublisher.IeconPath,
        PostExecutionAction = _apiDataPublisher.OldFileCleanup
    });
}

else
{
    if (_apiDataPublisher != null)
    {
        subscribers.Add(new Subscriber()
        {
            Arguments = _apiDataPublisher.BuildExportArgumentsAndOutputPathForMergedCsv(DmProject.GetDmProjectPath(dmProject)).Item1,
            ExecutablePath = _apiDataPublisher.IeconPath,
            PostExecutionAction = _apiDataPublisher.OldFileCleanup
        });
    }
}


What I have tried:

C#
subscribers = DmApiCacheUpdateStrategy.Equals("Delete") ?
    subscribers.Add(
        new Subscriber
        {
            PostExecutionAction = _apiDataPublisher.OldFileCleanup
        }) :
    subscribers.Add(
        new Subscriber()
        {
        Arguments = _apiDataPublisher.BuildExportArgumentsAndOutputPathForMergedCsv(DmProject.GetDmProjectPath(dmProject)).Item1,
        ExecutablePath = _apiDataPublisher.IeconPath,
        PostExecutionAction = _apiDataPublisher.OldFileCleanup
        });
Posted
Updated 29-Jan-22 20:05pm
v5
Comments
Gerry Schmitz 27-Jan-22 23:59pm    
Create a new "empty" Subscriber first; set the properties based on the if; then add it to the collection.
MayankSemwal 28-Jan-22 0:02am    
Thanks for the reply Gerry. Is it possible for you to give an example?

A conditional statement is literally just a "compressed" if ... else statement:
C#
if (a)
   b;
else
   c;
And it becomes
C#
a ? b : c
But it's intended for assignments so it really needs the "expanded" version to be something like this:
C#
if (a)
   x = b;
else
   x = c;
Which becomes
C#
x = a ? b : c
As a result, both the b and c parts of the conditional statement must return the same types

Your original code isn't an assignment, so your "compressed" version doesn't do the same thing and probably should be something like
C#
subscribers.Add(DmApiCacheUpdateStrategy.Equals("Delete")) ?
                new Subscriber()    
                    {
                    Arguments = DmProject.GetDmProjectPath(dmProject),
                    ExecutablePath = _apiDataPublisher.IeconPath,
                    PostExecutionAction = _apiDataPublisher.OldFileCleanup
                    } :
                new Subcriber()
                    {
                    ...

Except that your original doesn't add an item in all cases so you can't convert the whole thing to a conditional statement at all!

Even if you could, the code you would end up with would be pretty impenetrable to read compared to the if ... else version, so I woudln't do it at all if I was you.
 
Share this answer
 
Comments
MayankSemwal 28-Jan-22 1:05am    
Hello Griff,

This statement will not add but just compare subscribers.Add(DmApiCacheUpdateStrategy.Equals("Delete")).

Output: cannot convert from 'bool' to 'Subscriber'
OriginalGriff 28-Jan-22 1:41am    
What part of "Except that your original doesn't add an item in all cases so you can't convert the whole thing to a conditional statement at all!" did you not understand?
MayankSemwal 28-Jan-22 1:45am    
Understood. Sorry for that I was too focused on getting the result.
The very first step is to build the idea. May be like this

DmApiCacheUpdateStrategy.Equals("Delete") ? <true> : <false>;


Then improving a bit more

DmApiCacheUpdateStrategy.Equals("Delete") ? <true> : _apiDataPublisher != null ? <true> : <false>;


Further improvement could be like

DmApiCacheUpdateStrategy.Equals("Delete") ? AddSubscriber(DmProject.GetDmProjectPath(dmProject)) : _apiDataPublisher != null ? AddSubscriber(_apiDataPublisher.BuildExportArgumentsAndOutputPathForMergedCsv(DmProject.GetDmProjectPath(dmProject)).Item1) : <false>;


But then the question arises what to do if _api_dataPublisher is null.

I think your initial way of doing things with If else seems more maintenance-friendly instead of using it via a conditional operator. However you could rewrite the If conditions in following way

C#
if (DmApiCacheUpdateStrategy.Equals("Delete"))
{
    AddSubscriber(DmProject.GetDmProjectPath(dmProject))
}

else
{
    if (_apiDataPublisher != null)
    {
        string _t= _apiDataPublisher.BuildExportArgumentsAndOutputPathForMergedCsv(DmProject.GetDmProjectPath(dmProject)).Item1;
        AddSubscriber(_t);
    }
}

public void AddSubscriber(string _p)
{
//Try 
        subscribers.Add(new Subscriber()
        {
            Arguments = _p,
            ExecutablePath = _apiDataPublisher.IeconPath,
            PostExecutionAction = _apiDataPublisher.OldFileCleanup
        });
//Catch Block
}
 
Share this answer
 
Comments
MayankSemwal 28-Jan-22 1:31am    
Hello Asif,
Using the below answer you provided it throws an error:
DmApiCacheUpdateStrategy.Equals("Delete") ? AddSubscriber(DmProject.GetDmProjectPath(dmProject)) : _apiDataPublisher != null ? AddSubscriber(_apiDataPublisher.BuildExportArgumentsAndOutputPathForMergedCsv(DmProject.GetDmProjectPath(dmProject)).Item1) : false;

Error:
1. Only assignment, call can be used as statement.
2. Type of conditional expression cannot be determined because there is no implicit conversion between void and target typed conditional expression.
MayankSemwal 28-Jan-22 1:39am    
The final optimal solution by you works fine but I am asked to do using the statement throwing an error.


DmApiCacheUpdateStrategy.Equals("Delete") ? AddSubscriber(DmProject.GetDmProjectPath(dmProject)) : _apiDataPublisher != null ? AddSubscriber(_apiDataPublisher.
BuildExportArgumentsAndOutputPathForMergedCsv(DmProject.GetDmProjectPath(dmProject)).Item1) : <false>;
The simple (?) version:

A nested if/else structure defines a set of (explicit) conditions each of which is unique (mutually exclusive). For each unique condition, the programmer can supply an action )r, omit the action). Of course, a nested if/else structure that omits an action for each condition is ... purposeless.

A conditional statement in C# ... the Ternary Operator '?' ... returns a value: if you do not save the returned value, it is "lost." Each of the Ternary Operator's two outcomes must be specified explicitly, and their execution must return a valid object of the Type specied.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900