如何调用“公共”阵列? [英] How do I call a 'public' array?
问题描述
如何定义一个包含数组的公共方法?
我想将方法设置为public,以便它可以与多个按钮一起使用方法,但程序说并非所有代码路径都返回值。我认为这意味着我没有将''void''值更改为正确的类型但是有人可以告诉我它是什么吗?
我有以下代码:
How do I define a public method that has an array in it?
I would like to set a method to public so it can be used with multiple button methods but the program says ''not all code paths return a value''. I assume that means I have not changed the ''void'' value to the correct type but can anyone tell me what it is?
I have the following code:
public String bob ()
{
string [] averages = new string[2];
averages[0] = value1.Text;
averages[1] = value2.Text;
}
private void button1_Click(object sender, EventArgs e)
{
bob();
label1.Text = averages[1];
}
谢谢。
已添加代码块[/编辑]
Thanks.
Code block added[/Edit]
推荐答案
更改bob()
进入:
Hi,
Changebob()
into this:
public String[] bob ()
{
string [] averages = new string[2];
averages[0] = value1.Text;
averages[1] = value2.Text;
return averages;
}
更多关于返回
的关键字:
http://msdn.microsoft.com/en-us/library/1h3swy84.aspx [ ^ ]
希望这会有所帮助。
More about the return
keyword:
http://msdn.microsoft.com/en-us/library/1h3swy84.aspx[^]
Hope this helps.
为什么不这样做呢相反,既然你似乎正在尝试做什么?
Why not just do this instead, since it''s what you appear to be trying to do?
private void button1_Click(object sender, EventArgs e)
{
label1.Text = value2.Text;
}
public string[] bob ()
{
string [] averages = new string[2];
averages[0] = value1.Text;
averages[1] = value2.Text;
return averages;
}
private void button1_Click(object sender, EventArgs e)
{
string[] bobResult = bob();
label1.Text = bobResult[1];
}
这是您问题的一个解决方案。方法bob返回字符串数组。在button1_Click方法中,bob调用的结果被赋值给类型为string []且名为bobResult的变量。
This is one solution to your question. The method "bob" returns string array. In the button1_Click method the result from "bob" call is assigned to variable with type string[] and name bobResult.
这篇关于如何调用“公共”阵列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!