out关键字-初始化 [英] out keyword - initialize

查看:73
本文介绍了out关键字-初始化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我在以下代码上收到以下错误.

错误1必须在控制离开当前方法之前将out参数``dtValueToCheck''分配给

错误2必须在控制离开当前方法之前将out参数``strValueToCheck''分配给

错误3必须在控制离开当前方法之前将out参数``strFieldNameToCheck''分配给

错误5方法``ValidateCriteria''没有重载需要5个参数

请问我在做什么错?
谢谢

Hi,
I get the following errors on the following code.

Error 1 The out parameter ''dtValueToCheck'' must be assigned to before control leaves the current method

Error 2 The out parameter ''strValueToCheck'' must be assigned to before control leaves the current method

Error 3 The out parameter ''strFieldNameToCheck'' must be assigned to before control leaves the current method

Error 5 No overload for method ''ValidateCriteria'' takes 5 arguments

What am I doing wrong please?
Thanks

private bool ValidateAssetCriteria(DataTable dtToCheck, string strAssetName)
        {
            string strFieldNameToCheck = string.Empty;
            string strValueToCheck = string.Empty;
            DateTime dtValueToCheck = DateTime.Now;

            bool validate = ValidateCriteria(dtToCheck, strAssetName,
                                                           out strFieldNameToCheck, out strValueToCheck,
                                                           out dtValueToCheck);

           
        }

public static bool ValidateCriteria(DataTable dtToCheck, string strAssetName,
                                            out string strFieldNameToCheck, out string strValueToCheck,
                                            out DateTime dtValueToCheck)
        {

...
...
strFieldNameToCheck = "fieldname";
strValueToCheck = "value";
dtValueToCheck = DateTime.now

}

推荐答案

我打赌您在包含在ValidateCriteria中的代码块之前有一个return语句,因此尚未使用参数设置.

您必须在任何return语句的之前设置三个out参数.
I''m betting you have a return statement before the code block you included in the ValidateCriteria and thus the parameters has not been set.

You have to set the three out parameters before any return statement.


错误消息中的提示.必须分配一个out参数,但是该方法会执行,即必须在没有分配它们的情况下从方法中返回.

如果要传递参数,则没有必要先对其进行初始化.您传递给他们的方法必须分配新值.如果希望您的方法能够但不要求更新值,则应使用ref而不是out.

我猜参数计数消息是因为这两件事在不同的项目中,并且第一次编译失败,因为它看起来像是这里的五个参数.
The clue''s in the error message. An out parameter must be assigned, however the method executes, i.e. there must be no way of returning from the method without assigning them.

If you''re passing out parameters, there''s no point initialising them first; the method you pass to them must assign new values. If you want your method to be able to, but not required to, update the values, you should use ref, not out.

I guess the parameter count message is because these two things are in different projects and the compile on the first fails, because it looks like five parameters from here.


这篇关于out关键字-初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆