Search code examples
c#stringerror-handlingboolean-expression

Extracting string variable from a bool function c#


so here I was wondering if it was possible to extract a string variable that was made in a bool function, the bottom code is just simplified down below:

public string string_value = "";
public string first_wafer = "";

string_value = Wafer_value(out first_wafer);//know this won't work because cannot convert string to bool

 public static bool Wafer_value( out string first_wafer)
        {
            try
            {            
                first_wafer = "string that I want to extract";   
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                first_wafer = null;
                return false;
            }
            return true;

        }//End Function Wafer_value

So what I am trying to do is extract first_wafer into string_value. The reason I want to do it this way where Wafer_value returns a bool, is so I can work on error handling later where it will be easier to deal with, since I can just use true or false as values.

I know for this I would need to use ref and out, but I am not sure how to implement it so it extracts the variable.


Solution

  • The function itself returns bool, but You are modifying first_wafer value in your function. Only thing you have to do is to assign string_value with it.

    var isWafer = Wafer_value(out first_wafer); //return boolean
    string_value = first_wafer; //assign string
    

    isWafer in this case is bool.

    Some simple fiddle