Home > Software design >  Is there an another way to have the value of an expression in C# than selection statements
Is there an another way to have the value of an expression in C# than selection statements

Time:01-24

Edit : I have some of you confused, it's not a test more a POC or a little piece of code to understand how to do something.

I need to compare two values.

I tried by doing a POC of what I need to do, but I'm a bit confused.

My test for my POC look like this :

public bool ValueMethod()
{
    bool returnValue = false;
    var expectedValue = new List<string>()
    {
        "Value1", "Value2", "Value3"
    };
    var ValueTestTrue = "Value1";
    var ValueTestFalse = "Value4";
    if (expectedValue.Contains(ValueTestTrue) && !expectedValue.Contains(ValueTestFalse))
    {
         returnValue = true;
    }
    return returnValue;
}

How can I do this but without an if statement ?

I need to apply this on a bigger project but I want to understand how to do my POC first.

My real Project look something like this

public bool Load(SpreadsheetDocument spreadsheetDocument)
{
    bool returnValue = false;
    WorkbookPart workbookPart = spreadsheetDocument.WorkbookPart;
    WorksheetPart worksheetPart = ExcelHelper.GetWorksheetFromSheetName(workbookPart, "Computer");
    if (worksheetPart != null)
    {
        SheetData sheetData = worksheetPart.Worksheet.GetFirstChild<SheetSata>();
        if (computerList.Contains(ExcelHelper.GetCellValue(WorkbookPart, sheetData, "A2")))
        {
            returnValue = true;
        }
    }
    return returnValue;
}

CodePudding user response:

try using inline condition like here

public bool TestMethod()
    {
        var expectedValue = new List<string>()
        {
            "Value1", "Value2", "Value3"
        };
        var ValueTestTrue = "Value1";
        var ValueTestFalse = "Value4";
        bool returnValue  =  (expectedValue.Contains(ValueTestTrue) && expectedValue.Contains(ValueTestFalse));
        return returnValue;
    }

CodePudding user response:

I didn't quite get why would you need such a code. Looks like it's an artificial constraint for learning.

Then your function could look like:

public bool Load(SpreadsheetDocument spreadsheetDocument)
{
    WorkbookPart workbookPart = spreadsheetDocument.WorkbookPart;
    WorksheetPart worksheetPart = ExcelHelper.GetWorksheetFromSheetName(workbookPart, "Computer");

    return worksheetPart != null 
        && worksheetPart.Worksheet.GetFirstChild<SheetSata>() is SheetData sheetData
        && sheetData != null
        && computerList.Contains(ExcelHelper.GetCellValue(WorkbookPart, sheetData, "A2"))
}
  • Related