-3

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.

  • if you use "out" (or "ref"), the string "first_wafer" you are passing to the function, will after the function call have the extracted string you wanted. – Atr0x Jun 20 '20 at 00:13
  • 1
    Its unclear what you are asking, any attempt at answering this would be a wild guess, please be more specific – TheGeneral Jun 20 '20 at 00:15
  • You're trying to assign a boolean to a string variable. If you want to convert it to a string, just do `YourBooleanFunction().ToString()`. Other than that I have no clue what you're trying to do. – Jason Jun 20 '20 at 00:23
  • This question is very unclear. I can barely tell what you're asking. Also, did you know you can call the function and declare the _out_ variable and check success/fail all at once with `if (Wafer_value(out string string_value)) { ... }` – Wyck Jun 20 '20 at 02:42

1 Answers1

1

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

Jay Nyxed
  • 497
  • 5
  • 12