gpt4 book ai didi

c# - 通过 Ref Textbox.Text

转载 作者:行者123 更新时间:2023-11-30 13:42:46 27 4
gpt4 key购买 nike

我目前有一些东西想通过 ref 传递一个 textbox.text。我不想传递整个文本框,我希望函数在返回其他变量的同时更改文本。

    public int function(int a, int b, string text)
{
//do something

if (a + b > 50)
{
text = "Omg its bigger than 50!";
}

return (a + b);
}

有没有办法通过 ref 传递 Textbox.text 并在函数内部更改它?

最佳答案

不能通过 ref 传递属性,只能传递字段或变量。

来自 MSDN :

Properties are not variables. They are actually methods, and therefore cannot be passed as ref parameters.

你必须使用一个中间变量:

string tmp = textBox.Text;
int x = function(1, 2, ref tmp);
textBox.Text = tmp;

关于c# - 通过 Ref Textbox.Text,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2028763/

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