Passing Parameter by Reference

SaltyDawg
SaltyDawg used Ask the Experts™
on
I have
clsMySqlODBCConnection OpenConnection = new clsMySqlODBCConnection();
started in a function. I would like to call to
public bool Validate()
and pass OpenConnection in the function "Validate()" so I would I would not have to create a new "OpenConnection" in there.

What parameter should I have for
public bool Validate()

Thanks
public bool ValidateUsers()
        {
            string strQry = null;
            //
            strQry = "Select * FROM Users WHERE Users.UserLogin = '" + UserLogin + "' AND Users.UserPassword = '" + UserPass + "' LIMIT 0,1";
            OpenConnection.MySQLQuery(strQry);

            if (OpenConnection.OdbcDR.HasRows)
            {
                OpenConnection.OdbcDR.Read();
                UserID = Convert.ToString(OpenConnection.OdbcDR["UserID"]);
                UserLogin = Convert.ToString(OpenConnection.OdbcDR["UserLogin"]);
                UserPass = Convert.ToString(OpenConnection.OdbcDR["UserPassword"]);
                UserFName = Convert.ToString(OpenConnection.OdbcDR["UserFName"]);
                UserLName = Convert.ToString(OpenConnection.OdbcDR["UserLName"]);
                UserStatus = Convert.ToString(OpenConnection.OdbcDR["UserStatus"]);
                return true;
            }
            return false;
        }

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
public bool ValidateUsers(ref OpenConnection)
//not sure what you mean ??

Author

Commented:
Yes, not sure if I explained it correctly.
I am trying to perform something like the following code.
 

        public void SetTest()
        {
            clsMySqlODBCConnection OpenConnection = new clsMySqlODBCConnection();

            if (ValidateUser(OpenConnection))
            { 
                //
            }

        }
        public bool ValidateUsers(ref OpenConnection)
        {
            string strQry = null;
            // 
            strQry = "Select * FROM Users WHERE Users.UserLogin = '" + UserLogin + "' AND Users.UserPassword = '" + UserPass + "' LIMIT 0,1";
            OpenConnection.MySQLQuery(strQry);

            if (OpenConnection.OdbcDR.HasRows)
            {
                OpenConnection.OdbcDR.Read();
                UserID = Convert.ToString(OpenConnection.OdbcDR["UserID"]);
                UserLogin = Convert.ToString(OpenConnection.OdbcDR["UserLogin"]);
                UserPass = Convert.ToString(OpenConnection.OdbcDR["UserPassword"]);
                UserFName = Convert.ToString(OpenConnection.OdbcDR["UserFName"]);
                UserLName = Convert.ToString(OpenConnection.OdbcDR["UserLName"]);
                UserStatus = Convert.ToString(OpenConnection.OdbcDR["UserStatus"]);
                return true;
            }
            return false;
        }
    }

Open in new window

Such connection are mostly defined in a class scope not in a function scope.
So we have 2 solutions for your problem:
Approach #1:
Make OpenConnection a class level member
like
public class clsABC
{
private clsMySqlODBCConnection OpenConnection;
clsABC()
{}
void fun1() //Function in which you have created the Connection
{
   OpenConnection = new clsMySqlODBCConnection();
....
....
}

//in validate function you can directly access OpenConnection without passing any paramenter, as is the case now.
       public bool ValidateUsers()
        {
            string strQry = null;
            //
            strQry = "Select * FROM Users WHERE Users.UserLogin = '" + UserLogin + "' AND Users.UserPassword = '" + UserPass + "' LIMIT 0,1";
            OpenConnection.MySQLQuery(strQry);
  ......
  ......
}
}

Approach #2: Modify the function signature of ValidateUsers to:
public bool ValidateUsers(clsMySqlODBCConnection OpenConnection)

I suggest to go with approach #1 as it has minmum code changes and it won't affect your existing code (incase ValidateUsers is called from somewhere else also as it is public)

Also if ValidateUsers is present in some other class then thr must be a way via a setter property, function or constructor to set the OpenConnection. if not i will advise you to create one and not modift ValidateUsers funtion signature.

Thx!
Swaps...
OWASP: Avoiding Hacker Tricks

Learn to build secure applications from the mindset of the hacker and avoid being exploited.

Ok I didnt have a look on the code above...
Go with approach #1...it will solve your problem
adding the ref makes a pointer to your original -- so you need ref in the method signature and ref when you call it

if (ValidateUser(ref OpenConnection))
            {
                //
            }

        }
        public bool ValidateUsers(ref OpenConnection)
{

}


you are going to want to make sure that nothing else can touch that connection object while you are using it in another function.

Author

Commented:
Thanks for the help, it helped me figure out what to do.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial