gpt4 book ai didi

c# - boolean 值作为方法参数不改变状态

转载 作者:太空宇宙 更新时间:2023-11-03 18:52:28 25 4
gpt4 key购买 nike

我想更改函数中 boolean 值的状态。我的函数有 4 个参数,第四个是 bool,默认情况下为 true,但我想在函数内部将其状态更改为 false。

我正在调用我的方法,

SlotCheck("Red", "red_small_c", puzzle_9, GameControl.control.scoreRedGems, GameControl.control.stone_9);

GameControl.control.stone_9 默认为真。一旦 visibilty 设置为 false,它应该变为 false ..但这并不令人高兴...stone_9 保持为 true。

public void SlotCheck(string gemColor,string slotColor,GameObject puzzleStuk,int scoreGem,bool Visibility)
{
if (DragHandler2.itemBegingDragged.name.Contains(gemColor) && DragHandler2.itemBegingDragged.transform.parent.name == slotColor)
{
Debug.Log(DragHandler2.itemBegingDragged.name);
Visibility=false;
puzzleStuk.SetActive(visibility);
Debug.Log(GameControl.control.stone_9); //true
DragHandler2.itemBegingDragged.SetActive(false);

}

我希望 GameControl.control.stone_9 将其状态更改为 false,因为我正在将参数 (Visibility) 的状态更改为 false,但 GameControl.control.stone_9 保持为 true。

最佳答案

我相信您的印象是更改参数的值将反射(reflect)给调用者(您的第一个代码块)。除非您使用 refout 作为参数/参数,否则情况并非如此。只要 GameControl.control.stone_9 是一个字段而不是一个属性,它就可以工作。

简而言之,参数是按值传递的,除非使用这些关键字。 (对于引用类型也是如此,但它有点复杂,因为复制的是引用,而不是实际对象本身)

其他答案已经解释了语法(需要对参数和参数都使用 ref。)

还有一点建议,永远不要将变量或参数大写,因为大多数 C# 读者会将它们视为包含类中的属性,从而造成混淆。

关于c# - boolean 值作为方法参数不改变状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53998784/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com