使用out参数定义方法

时间:2014-04-05 16:46:26

标签: c#-4.0 out

public  class ArgParseHelper
    {
        /// <summary>
        /// Constant for GuardMode "Report"
        /// </summary>
        private const string c_ModeReport = "REPORT";

        /// <summary>
        /// Constant for GuardMode "Enforce"
        /// </summary>
        private const string c_ModeEnforce = "ENFORCE";

        public static bool TryGetSecurityGuardModeValue(string securityMode, out SecurityGuardMode securityGuardMode)
        {
            securityGuardMode = securityGuardMode = SecurityGuardMode.Enforce;
            if (c_ModeEnforce.Equals(securityMode, StringComparison.OrdinalIgnoreCase))
            {
                return true;
            }
            else if (c_ModeReport.Equals(securityMode, StringComparison.OrdinalIgnoreCase))
            {
                securityGuardMode = SecurityGuardMode.Report;
                return true;
            }
            return false;
        }
    }

我将此方法称为:

SecurityGuardMode  guardModeService;
ArgParseHelper m_GuardArgParseHelper = new GuardArgParseHelper();
if (m_GuardArgParseHelper.TryGetSecurityGuardModeValue(value, out guardModeService))

//<-compilation error "`cannot be accessed with an instance reference; qualify it with a type name instead`"
            {

            }

出了什么问题?

1 个答案:

答案 0 :(得分:1)

您正在访问通过实例传递的静态方法。

您必须使用:

// Us the class name ArgParseHelper (where the static method is defined) not one
// of its instances
if (ArgParseHelper.TryGetSecurityGuardModeValue(..)) {

}