Resolved How to assign value to a variable based on condition.

Anonymous

Well-known member
Joined
Sep 29, 2020
Messages
84
Programming Experience
Beginner
There are three types of price in my application price1,price2 and price3. If price1 has priority over price2 and price 2 has priority over price3. If price 1 is not available then variable finalprice is assigned price 2 if price 2 is not available then the variable finalprice is assigned price3.
I am getting those three prices from a list of the following class type -
C#:
class ItemDetails
{
    public decimal Price1 { get; set; }
    public decimal Price2 { get; set; }
    public decimal Price3 { get; set; }
}
I am passing the list of price to my update function like this
C#:
public void Update(List<ItemDetails> itemDetailsList)
{
    foreach( var items in itemDetailsList)
    {
        decimal finalPrice = ???;
    }
}
Now, I am wondering if there is a short and efficient way in which I can assign my finalprice variable the value of price based on availability?
Or would I have to follow the If Else pattern only ?
 
Last edited by a moderator:
What does "available" mean in this context? Your variables are type decimal so they must always have a value. Please provide a FULL and CLEAR explanation of the problem.

That said, in general, you can use the ternary operator to make if/else assignment scenarios more concise, e.g.
C#:
decimal finalprice = item.Price1 == 0
                         ? item.Price2 == 0
                               ? item.Price3
                               : item.Price2
                         : item.Price1;
Nesting ternary operators can make things a bit harder to understand sometimes, although indenting properly helps. I'd avoid ever nesting more than two though.
 
What does "available" mean in this context? Your variables are type decimal so they must always have a value. Please provide a FULL and CLEAR explanation of the problem.

That said, in general, you can use the ternary operator to make if/else assignment scenarios more concise, e.g.
C#:
decimal finalprice = item.Price1 == 0
                         ? item.Price2 == 0
                               ? item.Price3
                               : item.Price2
                         : item.Price1;
Nesting ternary operators can make things a bit harder to understand sometimes, although indenting properly helps. I'd avoid ever nesting more than two though.
Actually , In my application it is a choice to have any price from any of the three price.So, it is not necessary for all variables to have value. I used decimal because the variable is dealing with price. Should I use some other data type?
 
Even if you don't assign a value, the default value will be 0. You could use a nullable decimal instead to determine if a value has been assigned or not, but in general, C# has been trending away from using nullable types.
 
There's also the null-coalescing operator that is even simpler if you want to use the result of an expression unless it's null:
C#:
class ItemDetails
{
    public decimal? Price1 { get; set; }
    public decimal? Price2 { get; set; }
    public decimal? Price3 { get; set; }
}
C#:
decimal finalPrice = item.Price1 ?? item.Price2 ?? item.Price3 ?? decimal.Zero;
That will use Price1 unless it's null, in which case it will use Price2 unless it's null, in which case it will use Price3 unless it's null, in which case it will use zero.
 
There's also the null-coalescing operator that is even simpler if you want to use the result of an expression unless it's null:
C#:
class ItemDetails
{
    public decimal? Price1 { get; set; }
    public decimal? Price2 { get; set; }
    public decimal? Price3 { get; set; }
}
C#:
decimal finalPrice = item.Price1 ?? item.Price2 ?? item.Price3 ?? decimal.Zero;
That will use Price1 unless it's null, in which case it will use Price2 unless it's null, in which case it will use Price3 unless it's null, in which case it will use zero.
Thanks, this is much simpler to use !
 
Back
Top Bottom