读书人

怎么让1个方法返回多个值

发布时间: 2013-07-01 12:33:04 作者: rapoo

如何让1个方法返回多个值?
本帖最后由 YJDP0918 于 2013-04-28 18:48:00 编辑 本人写了1个程序,实现:根据用户在页面上输入的字符串的第1个字符返回多个值。
用户输入abc 则 返回 大写的首字符 和1
用户输入basdas 则 返回 大写的首字符 和2
用户输入不是a和b字母开头的字符 则返回 大写的首字母 和 3

下面是我的代码
txtChange(sourceString)的方法在下面没有写出,因为只有1句转换字符大小写的代码。
返回多个值的问题已解决,但是我不知道怎么去获取,


protected void btnChangeString_Click(object sender, EventArgs e)
{
txtNewAddress.Text = StringConverter.checkString(txtSourceString.Text.Trim());


我直接调用该方法提示:“checkString”方法没有任何重载采用“1”个参数。

如果我在页面上声明了1个int 变量TypeID,并传过去

int TypeID=999;
txtNewAddress.Text = StringConverter.checkString(txtSourceString.Text.Trim(),out TypeID);


虽然可以通过编绎,但是我怎么获取checkString()方法返回的 txtTypeID 的值?

以下是返回2个值的代码


public static string checkString(string address, out int txtTypeID)
{
string addressTpye = address.Substring(0, 1).ToLower();
if (addressTpye == "a")
{
txtTypeID =1;
return txtChange(sourceString);
}
else
{
if (addressTpye == "b")
{
txtTypeID =2;
return txtChange(sourceString);
}
else
{
txtTypeID =3;
return txtChange(sourceString);
}

多个返回值 方法
[解决办法]
为什么要这样呢?直接返回一个数组不行吗,
 public string[] checkString(string txtvalue)
{
string[] txtchange = new String[2];
string addressTpye = txtvalue.Substring(0, 1).ToLower();
if (addressTpye == "a")
{
txtchange[0] = "1";
txtchange[1] = "A";
}
else
{
if (addressTpye == "a")
{
txtchange[0] = "2";
txtchange[1] = "B";


}
else
{

//......

}
}

return txtchange;
}


[解决办法]
new 1个 类 返回这个类 类里自然能有多个值

out ref http://www.cnblogs.com/gjahead/archive/2008/02/28/1084871.html

读书人网 >C#

热点推荐