0

Hi guys i'm having a problem regarding returning multiple values from a method. I'm using 'out' to return other value from a method, here the snippet:

public DataTable ValidateUser(string username, string password, out int result)
{
    try
    {
        //Calls the Data Layer (Base Class)
        if (objDL != null)
        {
            int intRet = 0;
            sqlDT = objDL.ValidateUser(username, password, out intRet);
        }
    }
    catch (Exception ex)
    {
        ErrorHandler.Handle(ex);
        OnRaiseErrorOccuredEvent(this, new ErrorEventArgs(ex));
    }
    return sqlDT;
}

then when i compile having a error like this:

"The out parameter 'return' must be assigned to before control leaves the current method"

Anyone guys can help me solve this.

1
  • What is sqlDT? Where is it defined? Commented Sep 23, 2011 at 2:49

3 Answers 3

2

That means in all possibilities (inside and outside the if, in the catch), your result variable must be assigned.

The best approach would be to give it a default value at the start of the function:

public DataTable ValidateUser(string username, string password, out int result)
{
    result = 0;
    try
    {
        //Calls the Data Layer (Base Class)
    if (objDL != null)
    {
        int intRet = 0;
        sqlDT = objDL.ValidateUser(username, password, out intRet);
        result = intRet;
    }
//....
Sign up to request clarification or add additional context in comments.

1 Comment

thanks it works.. actually I put result = intRet awhile a go but same problem now the work around i made i initialize the value of result to zero first result = 0
1

The parameter result of your method is marked as out. Parameters marked with out must be assigned within your method, i.e

result = 5;

This is enforced so callers of your method have the guarantee that the parameter that is passed with out is always set once your method finishes.

Comments

1

You're not setting the result variable in the method.

I'm guessing you want to add an extra line such as

result = intRet;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.