切换到对阵列中的项目

时间:2010-08-13 23:24:37

标签: c# arrays switch-statement

我正在使用authorize.net AIM,它们提供的示例代码打印了响应值的有序列表。而不是将有序列表打印到客户可以看到所有这些信息的屏幕,如何设置一个开关来访问数组的某些索引并根据为特定数组索引返回的文本执行某些操作?

String post_response;
HttpWebResponse objResponse = (HttpWebResponse)objRequest.GetResponse();
using (StreamReader responseStream = new StreamReader(objResponse.GetResponseStream()))
{
    post_response = responseStream.ReadToEnd();
    responseStream.Close();
}

// the response string is broken into an array
// The split character specified here must match the delimiting character specified above
Array response_array = post_response.Split('|');

// the results are output to the screen in the form of an html numbered list.
resultSpan.InnerHtml += "<OL> \n";
foreach (string value in response_array)
{
    resultSpan.InnerHtml += "<LI>" + value + "&nbsp;</LI> \n";
}
resultSpan.InnerHtml += "</OL> \n";

1 个答案:

答案 0 :(得分:4)

将response_array的类型更改为string[]

string[] response_array = post_response.Split('|');

在C#中你可以打开一个字符串:

switch (response_array[0])
{
    case "foo":
        // do something...
        break;
    case "bar":
        // do something else...
        break;
    default:
        // Error?
        break;
}