locked
Return True/Return False Question RRS feed

  • Question

  • I am not fathoming how to use a function that returns true or false.  What do you do with the returned value?  For example, I am wanting to check if a cell is null

    public static void CheckIfNull()
    {
    _Workbook workbooksExcel = docExcel.ActiveWorkbook;
    Worksheet sheetExcel = (Worksheet)workbooksExcel.ActiveSheet;
    if ((Range)sheetExcel.Cells["1","A"]).Value2 == "")
    {
    return true;
    }
    else
    {
    return false;
    }
    }

    So what do you do with the returned value of true or false?
    Tuesday, July 2, 2013 6:33 PM

Answers

  • First off, you cannot return anything from a void function.

    However, try this:

     
    public static bool CheckIfNull()
    {
    _Workbook workbooksExcel = docExcel.ActiveWorkbook;
    Worksheet sheetExcel = (Worksheet)workbooksExcel.ActiveSheet;
    if ((Range)sheetExcel.Cells["1","A"]).Value2 == "")
    {
    return true;
    }
    else
    {
    return false;
    }
    }

    Then, to retrieve the value:

    bool blReturn = CheckIfNull();
    Karl


    When you see answers and helpful posts, please click Vote As Helpful, Propose As Answer, and/or Mark As Answer



    My Blog: http://unlockpowershell.wordpress.com
    My Book: Windows PowerShell 2.0 Bible
    My E-mail: -join ("6B61726C6D69747363686B65406D742E6E6574"-split"(?<=\G.{2})",19|%{[char][int]"0x$_"})

    • Marked as answer by toptierdawg03 Tuesday, July 2, 2013 7:58 PM
    • Unmarked as answer by toptierdawg03 Tuesday, July 2, 2013 8:06 PM
    • Marked as answer by toptierdawg03 Tuesday, July 2, 2013 8:20 PM
    Tuesday, July 2, 2013 7:40 PM

All replies

  • First off, you cannot return anything from a void function.

    However, try this:

     
    public static bool CheckIfNull()
    {
    _Workbook workbooksExcel = docExcel.ActiveWorkbook;
    Worksheet sheetExcel = (Worksheet)workbooksExcel.ActiveSheet;
    if ((Range)sheetExcel.Cells["1","A"]).Value2 == "")
    {
    return true;
    }
    else
    {
    return false;
    }
    }

    Then, to retrieve the value:

    bool blReturn = CheckIfNull();
    Karl


    When you see answers and helpful posts, please click Vote As Helpful, Propose As Answer, and/or Mark As Answer



    My Blog: http://unlockpowershell.wordpress.com
    My Book: Windows PowerShell 2.0 Bible
    My E-mail: -join ("6B61726C6D69747363686B65406D742E6E6574"-split"(?<=\G.{2})",19|%{[char][int]"0x$_"})

    • Marked as answer by toptierdawg03 Tuesday, July 2, 2013 7:58 PM
    • Unmarked as answer by toptierdawg03 Tuesday, July 2, 2013 8:06 PM
    • Marked as answer by toptierdawg03 Tuesday, July 2, 2013 8:20 PM
    Tuesday, July 2, 2013 7:40 PM
  • So then I would do this:

    bool blReturn = CheckIfNull();
    
    if (blReturn == "Yes")
    {
    //this is just for examples sake
    Mesagebox.show("It's Yes");
    }
    else
    {
    Messagebox.Show("It's No");
    }

    Is that accurate?
    Tuesday, July 2, 2013 8:07 PM
  • That should work.

    Karl


    When you see answers and helpful posts, please click Vote As Helpful, Propose As Answer, and/or Mark As Answer



    My Blog: http://unlockpowershell.wordpress.com
    My Book: Windows PowerShell 2.0 Bible
    My E-mail: -join ("6B61726C6D69747363686B65406D742E6E6574"-split"(?<=\G.{2})",19|%{[char][int]"0x$_"})

    Tuesday, July 2, 2013 8:16 PM