在方法中使用OUT [英] using OUT in a method

查看:58
本文介绍了在方法中使用OUT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好!

我得到这个错误3次:输出参数必须在

之前分配控件离开当前方法 (dor dMin和dMax)

这一次1次:使用参数''out''没有分配''dMax''


这是我在C#中的第一步:我无法管理(理解)这个错误。


我想测试变量,可以在form1上更改2个数字

updown控件在发送给UserControl之前控制它们使用这两个数字进行一些计算:


if(_genData.NumberMin< _genData.NumberMax)

{


S =(int)(_ rand.Next((int)_genData.NumberMin,

(int)_genData.NumberMax + 1)/ 3)* 3;

}


else

{

// Choisir le nombre cible(_iS)包含dans la fourchette

et le rendre multiple de trois。

//设置目标编号(_iS)in范围和制作

三的倍数。

因此,如果用户修改了值并且nudNumMin .Value变为nudNumMax

..值得计算可能会在计算值S时发送错误。

这就是为什么我必须在将值发送到

UserControl之前测试并更正这些值。


form1中的代码是:

#region min max check

string message ="注意! Le nombre maximaldoitêtreau

nombre minimal。 L''erreurseracorrigée;

string caption =" Erreur dans le choix de la taille des nombres。" ;;

MessageBoxButtons buttons = MessageBoxButtons.OK;

DialogResult结果;


//此方法检查值并显示错误消息如果

//它们不符合预期并将它们更正为有效状态


public bool CheckMinMaxValues(out int dMin,out int dMax)

{


if(nudNumMin .Value nudNumMax .Value)

{

result = MessageBox.Show(消息,标题,按钮);

dMin = dMax - 1;

返回false;

}

返回true;

}

#endregion // min max check


然后:


private void nudNumMax_ValueChanged(object sender,EventArgs e)

{

int dMin =(int)nudNumMin.Value;

int dMax =(int)nudNumMax.Value;

if(!CheckMinMaxValues(out dMin,out dMax))

{

// ...当检查失败时做任何需要的事情

nudNumMin.Value = dMin;

nudNumMax.Value = dMax;

nudNumMax.Value = nudNumMin.Value + 1;

_currParam.GenerationData.NumberMax =(int)nudNumMin.Value

+ 1; //例如


}

_currParam.GenerationData.NumberMax =(int)nudNumMax.Value;

ValueChanged(true) ;

}


我希望你理解我....

并且可以帮助我!

pascal

-
http://www.scalpa .info

解决方案

4月13日上午12:58,Pascal < scalpaSansS ... @ scalpa.infowrote:


大家好!

我收到此错误3次:"输出参数必须在

之前指定控件离开当前方法 (dor dMin和dMax)

这一次1次:使用参数''out''没有分配''dMax''


这是我在C#中的第一步:我无法管理(理解)这个错误。



你发布的代码没有修改dMax,所以dMax不需要是一个

out参数。删除out dMax的关键字应该有效。


Jesse


看看
http://www.blackwasp.co.uk/CSharpMethodParameters.aspx\"target =_ blank> http://www.blackwasp.co.uk/CSharpMethodParameters.aspx
关于输出参数的详细信息。


-


BlackWasp
www.blackwasp.co.uk

" Pascal" < sc ************ @ scalpa.infowrote留言

news:66 ****************** **************** @ microsof t.com ...


大家好!

我收到此错误3次:输出参数必须在

之前分配,控件将保留当前方法。 (dor dMin和dMax)

这一次1次:使用参数''out''没有分配''dMax''


这是我在C#中的第一步:我无法管理(理解)这个错误。


我想测试变量,可以在form1上更改2

数字更新控件在发送给UserControl之前使用这两个数字进行一些计算:


if(_genData.NumberMin< _genData.NumberMax)

{


S =(int)(_ rand.Next((int)_genData.NumberMin,

(int)_genData.NumberMax + 1)/ 3)* 3;

}


else

{

// Choisir le nombre cible(_iS)包含dans la fourchette

et le rendre multiple de trois。

//设置目标编号(_iS)in范围和制作

三个中的一个。

所以如果用户mod如果值和nudNumMin .Value变为nudNumMax

。值计算可能会在计算值S时发送错误。

这就是我必须测试和纠正的原因将值发送给用户控件之前的价值




form1中的代码为:

#region min max check

string message ="注意! Le nombre maximaldoitêtreau

nombre minimal。 L''erreurseracorrigée;

string caption =" Erreur dans le choix de la taille des nombres。" ;;

MessageBoxButtons buttons = MessageBoxButtons.OK;

DialogResult结果;


//此方法检查值并显示错误消息如果

//它们不符合预期并将它们更正为有效状态


public bool CheckMinMaxValues(out int dMin,out int dMax)

{


if(nudNumMin .Value nudNumMax .Value)

{

result = MessageBox.Show(消息,标题,按钮);

dMin = dMax - 1;

返回false;

}

返回true;

}

#endregion // min max check


然后:


private void nudNumMax_ValueChanged(object sender,EventArgs e)

{

int dMin =(int)nudNumMin.Value;

int dMax =(int)nudNumMax.Value;

if(!CheckMinMaxValues(out dMin,out dMax))

{

// ...执行检查失败时需要的东西

nudNumMin.Value = dMin;

nudNumMax.Value = dMax;

nudNumMax.Value = nudNumMin.Value + 1;

_currParam.GenerationData.NumberMax =(int)nudNumMin.Value

+ 1; //例如


}

_currParam.GenerationData.NumberMax =(int)nudNumMax.Value;

ValueChanged(true) ;

}


我希望你理解我....

并且可以帮助我!

pascal

-
http://www.scalpa .info


Pascal写道:


大家好!

我收到此错误3次:必须在控件离开当前方法之前指定输出参数

(dor dMin和dMax)

这一次1次:使用参数''out''没有分配''dMax''


这是我在C#中的第一步:我无法管理(理解)这个错误。


我想测试变量,可以在form1上更改2

数字更新控件在发送给UserControl之前使用这两个数字进行一些计算:


if(_genData.NumberMin< _genData.NumberMax)

{


S =(int)(_ rand.Next((int)_genData.NumberMin,

(int)_genData.NumberMax + 1)/ 3)* 3;

}


else

{

// Choisir le nombre cible(_iS)包含dans la

fourchette et le rendre multiple de trois。

//设置目标编号(_iS)in范围和制作

三个中的一个。

所以如果用户mod如果值和nudNumMin .Value变为nudNumMax

。值计算可能会在计算值S时发送错误。

这就是我必须测试和纠正的原因将值发送给用户控件之前的价值




form1中的代码为:

#region min max check

string message ="注意! Le nombre maximaldoitêtreau

nombre minimal。 L''erreurseracorrigée;

string caption =" Erreur dans le choix de la taille des nombres。" ;;

MessageBoxButtons buttons = MessageBoxButtons.OK;

DialogResult结果;


//此方法检查值并显示错误消息如果

//它们不符合预期并将它们更正为有效状态


public bool CheckMinMaxValues(out int dMin,out int dMax)

{


if(nudNumMin .Value nudNumMax .Value)

{

result = MessageBox.Show(消息,标题,按钮);

dMin = dMax - 1;

返回false;

}

返回true;

}

#endregion // min max check


然后:


private void nudNumMax_ValueChanged(object sender,EventArgs e)

{

int dMin =(int)nudNumMin.Value;

int dMax =(int)nudNumMax.Value;

if(!CheckMinMaxValues(out dMin,out dMax))

{

// ...执行检查失败时需要的东西

nudNumMin.Value = dMin;

nudNumMax.Value = dMax;

nudNumMax.Value = nudNumMin.Value + 1;

_currParam.GenerationData.NumberMax =(int)

nudNumMin.Value + 1; //例如


}

_currParam.GenerationData.NumberMax =(int)nudNumMax.Value;

ValueChanged(true) ;

}


我希望你理解我....

并且可以帮助我!

pascal



如果你使用out参数的关键字,您必须在退出方法之前为其分配

值。 外出关键字要求。

在你的方法CheckMinMaxValues()中,两个out的值是参数
如果if,则不会设置
声明是假的,所以编译器没有工作。


-

谢谢,

Duy Lam Phuong


hello everybody !
I get this error 3 times : "The output parameter must be assigned before
the control leaves the current method" (dor dMin and dMax)
this one 1 time : Use of the parameter ''out'' is not assigned ''dMax''

This is my first steps in C#: i can''t manage(understand) this error.

I would like to test to variables which can be change on form1 by 2 numeric
updown controls before sending them to a UserControl which make some
calculation with these 2 numbers like this :

if (_genData.NumberMin < _genData.NumberMax)
{

S = (int)(_rand.Next((int)_genData.NumberMin,
(int)_genData.NumberMax + 1) / 3) * 3;
}

else
{
//Choisir le nombre cible (_iS) compris dans la fourchette
et le rendre multiple de trois.
// Set the target number (_iS) in the range and make
multiple of three.
So if user modify the value and nudNumMin .Value becomes nudNumMax
..Value the calculation could send an error in calculating the value S.
That''s why i have to test and correct the values before sending them to the
UserControl.

the code in form1 is :
#region min max check
string message = "Attention ! Le nombre maximal doit être au
nombre minimal. L''erreur sera corrigée";
string caption = "Erreur dans le choix de la taille des nombres.";
MessageBoxButtons buttons = MessageBoxButtons.OK;
DialogResult result;

// this method checks the values and shows an error message if
// they are not as expected and corrects them to a valid state

public bool CheckMinMaxValues(out int dMin, out int dMax)
{

if ( nudNumMin .Value nudNumMax .Value )
{
result = MessageBox.Show(message, caption, buttons);
dMin = dMax - 1;
return false;
}
return true;
}
#endregion //min max check

and then :

private void nudNumMax_ValueChanged( object sender, EventArgs e )
{
int dMin = (int)nudNumMin.Value;
int dMax = (int)nudNumMax.Value;
if (!CheckMinMaxValues(out dMin, out dMax))
{
//... do what ever is needed when the check fails
nudNumMin.Value = dMin;
nudNumMax.Value = dMax;
nudNumMax.Value = nudNumMin.Value + 1;
_currParam.GenerationData.NumberMax = (int) nudNumMin.Value
+ 1; // for example

}
_currParam.GenerationData.NumberMax = (int)nudNumMax.Value;
ValueChanged( true );
}

I hope you understand me....
and can help me !
pascal
--
http://www.scalpa.info

解决方案

On Apr 13, 12:58 am, "Pascal" <scalpaSansS...@scalpa.infowrote:

hello everybody !
I get this error 3 times : "The output parameter must be assigned before
the control leaves the current method" (dor dMin and dMax)
this one 1 time : Use of the parameter ''out'' is not assigned ''dMax''

This is my first steps in C#: i can''t manage(understand) this error.

The code you posted doesn''t modify dMax, so dMax doesn''t need to be an
out parameter. Remove the "out" keyword for dMax and it should work.

Jesse


Have a look at http://www.blackwasp.co.uk/CSharpMethodParameters.aspx for
details on output parameters.

--

BlackWasp
www.blackwasp.co.uk
"Pascal" <sc************@scalpa.infowrote in message
news:66**********************************@microsof t.com...

hello everybody !
I get this error 3 times : "The output parameter must be assigned before
the control leaves the current method" (dor dMin and dMax)
this one 1 time : Use of the parameter ''out'' is not assigned ''dMax''

This is my first steps in C#: i can''t manage(understand) this error.

I would like to test to variables which can be change on form1 by 2
numeric updown controls before sending them to a UserControl which make
some calculation with these 2 numbers like this :

if (_genData.NumberMin < _genData.NumberMax)
{

S = (int)(_rand.Next((int)_genData.NumberMin,
(int)_genData.NumberMax + 1) / 3) * 3;
}

else
{
//Choisir le nombre cible (_iS) compris dans la fourchette
et le rendre multiple de trois.
// Set the target number (_iS) in the range and make
multiple of three.
So if user modify the value and nudNumMin .Value becomes nudNumMax
.Value the calculation could send an error in calculating the value S.
That''s why i have to test and correct the values before sending them to
the UserControl.

the code in form1 is :
#region min max check
string message = "Attention ! Le nombre maximal doit être au
nombre minimal. L''erreur sera corrigée";
string caption = "Erreur dans le choix de la taille des nombres.";
MessageBoxButtons buttons = MessageBoxButtons.OK;
DialogResult result;

// this method checks the values and shows an error message if
// they are not as expected and corrects them to a valid state

public bool CheckMinMaxValues(out int dMin, out int dMax)
{

if ( nudNumMin .Value nudNumMax .Value )
{
result = MessageBox.Show(message, caption, buttons);
dMin = dMax - 1;
return false;
}
return true;
}
#endregion //min max check

and then :

private void nudNumMax_ValueChanged( object sender, EventArgs e )
{
int dMin = (int)nudNumMin.Value;
int dMax = (int)nudNumMax.Value;
if (!CheckMinMaxValues(out dMin, out dMax))
{
//... do what ever is needed when the check fails
nudNumMin.Value = dMin;
nudNumMax.Value = dMax;
nudNumMax.Value = nudNumMin.Value + 1;
_currParam.GenerationData.NumberMax = (int) nudNumMin.Value
+ 1; // for example

}
_currParam.GenerationData.NumberMax = (int)nudNumMax.Value;
ValueChanged( true );
}

I hope you understand me....
and can help me !
pascal
--
http://www.scalpa.info


Pascal wrote:

hello everybody !
I get this error 3 times : "The output parameter must be assigned
before the control leaves the current method" (dor dMin and dMax)
this one 1 time : Use of the parameter ''out'' is not assigned ''dMax''

This is my first steps in C#: i can''t manage(understand) this error.

I would like to test to variables which can be change on form1 by 2
numeric updown controls before sending them to a UserControl which make
some calculation with these 2 numbers like this :

if (_genData.NumberMin < _genData.NumberMax)
{

S = (int)(_rand.Next((int)_genData.NumberMin,
(int)_genData.NumberMax + 1) / 3) * 3;
}

else
{
//Choisir le nombre cible (_iS) compris dans la
fourchette et le rendre multiple de trois.
// Set the target number (_iS) in the range and make
multiple of three.
So if user modify the value and nudNumMin .Value becomes nudNumMax
.Value the calculation could send an error in calculating the value S.
That''s why i have to test and correct the values before sending them to
the UserControl.

the code in form1 is :
#region min max check
string message = "Attention ! Le nombre maximal doit être au
nombre minimal. L''erreur sera corrigée";
string caption = "Erreur dans le choix de la taille des nombres.";
MessageBoxButtons buttons = MessageBoxButtons.OK;
DialogResult result;

// this method checks the values and shows an error message if
// they are not as expected and corrects them to a valid state

public bool CheckMinMaxValues(out int dMin, out int dMax)
{

if ( nudNumMin .Value nudNumMax .Value )
{
result = MessageBox.Show(message, caption, buttons);
dMin = dMax - 1;
return false;
}
return true;
}
#endregion //min max check

and then :

private void nudNumMax_ValueChanged( object sender, EventArgs e )
{
int dMin = (int)nudNumMin.Value;
int dMax = (int)nudNumMax.Value;
if (!CheckMinMaxValues(out dMin, out dMax))
{
//... do what ever is needed when the check fails
nudNumMin.Value = dMin;
nudNumMax.Value = dMax;
nudNumMax.Value = nudNumMin.Value + 1;
_currParam.GenerationData.NumberMax = (int)
nudNumMin.Value + 1; // for example

}
_currParam.GenerationData.NumberMax = (int)nudNumMax.Value;
ValueChanged( true );
}

I hope you understand me....
and can help me !
pascal

If you use the "out" keyword for the parameters, you must assign the
value to it before go out of method. The "out" keyword requires that.
In your method CheckMinMaxValues(), the value of two "out" parameters
will not be set if the "if" statement is false, so the compiler didn''t work.

--
Thanks,
Duy Lam Phuong


这篇关于在方法中使用OUT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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