我正在使用这段代码,它给了我一个错误,说它不能将 void 转换为 int:
private static int aBtn;
public static int ABtn
{
get => aBtn;
set => aBtn = CheckArgumentRange(nameof(value), value, 0, 5);
}
internal static void CheckArgumentRange(
string paramName, int value, int minInclusive, int maxInclusive)
{
if (value < minInclusive || value > maxInclusive)
{
throw new ArgumentOutOfRangeException(paramName, value,
$"Value should be in range [{minInclusive}-{maxInclusive}]");
}
}
任何人都可以看到出了什么问题以及为什么会出现此错误?
如果未超出范围,您将需要返回该值。否则该集合不会收到一个 int。
所以只需return value;
在你的 if 后面加上你应该很好。
编辑:当然将返回类型从 void 更改为 int。
private static int aBtn;
public static int ABtn
{
get => aBtn;
set => aBtn = CheckArgumentRange(nameof(value), value, 0, 5);
}
internal static int CheckArgumentRange(
string paramName, int value, int minInclusive, int maxInclusive)
{
if (value < minInclusive || value > maxInclusive)
{
throw new ArgumentOutOfRangeException(paramName, value,
$"Value should be in range [{minInclusive}-{maxInclusive}]");
}
return value;
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句