2

I have two methods. First I pass one string from first method while calling the second method. In second method I do the calculations.

Now I want to pass the result back to the first method.

How do I achieve that?

My code is something like this:

    private void RegisterButton_Click(object sender, RoutedEventArgs e)
    {

        string databaseName = "databaseName";
            CheckDatabase(databaseName);
            bool test = bRet;
    }
    private bool CheckDatabase(string databaseName)
    {
        string connString = "Server=localhost\\SQLEXPRESS;Integrated Security=SSPI;database=master";
        string cmdText = "select * from master.dbo.sysdatabases where name=\'" + databaseName + "\'";
        bool bRet = false;
        using (SqlConnection sqlConnection = new SqlConnection(connString))
        {
            sqlConnection.Open();
            using (SqlCommand sqlCmd = new SqlCommand(cmdText, sqlConnection))
            {
                int nRet = sqlCmd.ExecuteNonQuery();
                if (nRet <= 0)
                {
                    bRet = false;
                }
                else
                {
                    bRet = true;
                }
            }
        }            
        return bRet;
    }

How do I pass back the bRet ??

  • 3
    `bool test = CheckDatabase(databaseName);` – Roman May 16 '18 at 08:23
  • Just the value of bRet? `bool test = CheckDatabase(databaseName);`. If you need more than one return value you can wrap them in a structure to return (e.g. `Tuple<>`) or use [reference or output parameters](https://stackoverflow.com/questions/135234/difference-between-ref-and-out-parameters-in-net). – Rup May 16 '18 at 08:24
  • Possible duplicate of [What are good ways to prevent SQL injection?](https://stackoverflow.com/questions/14376473/what-are-good-ways-to-prevent-sql-injection) – mjwills May 16 '18 at 08:52

2 Answers2

5

Based on the rest of your code, maybe I misunderstood the question, but this should work:

bool test = CheckDatabase(databaseName);
nvoigt
  • 75,013
  • 26
  • 93
  • 142
1

You have to do bool test = CheckDatabase(databaseName);

The reason why is because that bRet does not exist outside CheckDatabase, even though it has the same name.

Also, if you're planning on returning nRet as well, then you need to use System.ValueTuples and do something like this:

private (bool bRet, int nRet) CheckDatabase(string databaseName)
{
    string connString = "Server=localhost\\SQLEXPRESS;Integrated 
    Security=SSPI;database=master";
    string cmdText = "select * from master.dbo.sysdatabases where name=\'" + databaseName + "\'";
    bool bRet;
    int nRet;

    using (SqlConnection sqlConnection = new SqlConnection(connString))
    {
        sqlConnection.Open();
        using (SqlCommand sqlCmd = new SqlCommand(cmdText, sqlConnection))
        {
            nRet = sqlCmd.ExecuteNonQuery();
            bRet = !(nRet <= 0)
        }
    }
    return (bRet, nRet);
}

And you can get the multiple returns in any one of these ways (ordered by my personal preference, based off what's cleaner and better for performance, 1. being my least favorite, and 4. easily my favorite)

// 1. Declare and assign variables individually (calls function needlessly twice)
bool bTest = CheckDatabase("databaseName").bRet;
int nTest = CheckDatabase("databaseName").nRet;


// 2. Declare variables first, then assign them at the same time (calls function only once)
bool bTest;
int nTest;
(bTest, nTest) = CheckDatabase("databaseName");

// 3. Declare and assign at the same time and use only one line
(bool bTest, int nTest) = CheckDatabase("databaseName");

// 4. Use the var keyword to infer the types of the tuple.
// Useful for larger tuples. 
var (bTest, nTest) = CheckDatabase("databaseName");
AustinWBryan
  • 3,249
  • 3
  • 24
  • 42
  • Do you define "extended discussion" as trying to help them out? We weren't talking about movies, he was confused about my answer and I was trying to help him. – AustinWBryan May 16 '18 at 22:47
  • 1
    Please use chat for back and forth discussions. Important/supporting information should then be edited into your answer. – Samuel Liew May 16 '18 at 23:01