开发者

Ref argument is not Classified as a Variable - better fix for this?

I have this piece of code that was converted from VB.NET.

    private static void PrivateExecuteNonQuery(ref Providers enumProvider, ref string statement, ref CommandType commandType, ref ArrayList paramCollection, ref IDbConnection conn, ref Int16 CommandTimeout)
    {
        ExecuteSqlServerNonQuery(ref statement, ref commandType, ref paramCollection, ref (SqlConnection)conn, ref CommandTimeout);
    }

I am getting an error because it's saying the ref (SqlConnection)conn is not in the form of a variable so I guess you can't pass in method params for a ref?

So this seems like a hacky solution to me though:

    private static void PrivateExecuteNonQuery(ref Providers enumProvider, ref string statement, ref CommandType commandType, ref ArrayList para开发者_运维问答mCollection, ref IDbConnection conn, ref Int16 CommandTimeout)
    {
        SqlConnection sqlConnection = (SqlConnection)conn;

    ExecuteSqlServerNonQuery(ref statement, ref commandType, ref paramCollection, ref sqlConnection, ref CommandTimeout);
    }

Anyone know of a better way to satisfy this ref or make this cleaner?


The purpose of ref is to actually permit the method to modify the contents of a variable passed in by the caller. If you have incidental ref parameters, due to conversion from Visual Basic code that used ByRef indiscriminately, then you end up in this situation.

You have two choices:

  • Remove unnecessary ref parameter qualifiers, both in the caller and the callee, or
  • Use real variables, dummy variables if you have to, as arguments to the method

So, the answer is, no, there is no better way to do it without getting rid of unnecessary ref parameter qualifiers.

What is an unnecessary ref parameter qualifier? It is one for a parameter that is never actually assigned anywhere in the body of the method. Most parameters don't need to be ref and those that do are usually pretty obvious from inspecting logic of the method.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜