Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want string output from a boolean method.

For positive cases I'm returning true, but for negative case I need to send one error message which is string only.

Is that possible in C sharp?

Please respond, I want your help.
Posted
Updated 2-Dec-10 22:14pm
v2
Comments
Dalek Dave 3-Dec-10 4:14am    
Edited for Grammar.

Here are your options:

1) Use out parameter[^] to pass the error message. So the message is available to the calling function.

2) Return String from your method instead of Boolean. So, in the calling function, this will be the case:

C#
String valid = someMethod(); //returns true or error code
if (valid == "true") //the quotes are important
{
    //do something
}
else
{
    //you have the error message stored in valid
}


Hope this helps!
 
Share this answer
 
v2
Comments
Dalek Dave 3-Dec-10 4:15am    
More or less exactly what I would have said.
Example:

public bool SomeBooleanFunction(out String errorMessage)
{
    errorMessage = "";
    if(checkSomething())
    {
        return true;
    }
    else
    {
        errorMessage = "Something went awfully wrong: XXXX";
        return false;
    }
}


String msg;
bool test = SomeBooleanFunction(out msg);
if(!test)
    Console.WriteLine(msg);



Cheers


Manfred
 
Share this answer
 
Comments
Dalek Dave 3-Dec-10 4:15am    
Yep, good answer.
Ankur\m/ 3-Dec-10 4:30am    
Precise answer. Take another 5! :)
Is this what you mean?

C#
class Program
{
  static void Main(string[] args)
  {
    Program myProgram = new Program();
    string strMessage = "";
    strMessage = Convert.ToString(myProgram.BoolFunction());
    Console.WriteLine(strMessage);
    Console.ReadKey();
  }

  public bool BoolFunction()
  {
    return true;
  }
}
 
Share this answer
 
Yes u can return using ref and out parameters of your bool method. :)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900