我对此问题有一个较小的编程问题。下面是一个简单的SOAP请求,该请求接受3个参数(itemIDArr,depth,customInfo)。如果firstID,surName,streetName…(上面提到的所有值)都存在于itemIDArr中,则此程序可以正常工作。否则给我一个错误。
private void getItemSummary(long itemID)
{
//Create client
MYAPI.ResolvingClient resol = new MYAPI.ResolvingClient();
//Create parameters
MYAPI.ItemID[] itemIDArr = new MYAPI.Item [1];
MYAPI.ItemID itemIDunit = new MYAPI.ItemID();
itemIDunit.itemID = itemID;
itemIDArr[0] = itemIDunit; //param 1
MYAPI.DepthSpecifier depth = new MYAPI.DepthSpecifier(); //param 2
MYAPI.CustomInformation customInfo = new MYAPI.CustomInformation(); //param 3
//Make request
MYAPI.ItemSummary[] itemSummaryRes = resol.getItemSummaries(itemIDArr, depth, customInfo);
//Handle response
foreach (MYAPI.ItemSummary e in itemSummaryRes)
{
string firstName = e.bestName.givenName;
string surName = e.bestName.surname;
string streetName = e.bestAddress.street1;
string city = e.bestAddress.city;
string state = e.bestAddress.state;
string country = e.bestAddress.country;
string address = streetName + " " + city + " " + state + " " + country;
string email = e.bestEmail.emailAddress;
string number = e.bestNumber.numberValue;
MessageBox.Show(firstName + " " + surName + ", " + address);
}
}
我的问题是在像上面那样分配firstName,surName,streetName等值之前,我需要检查它们是否存在于itemIDArr中。我尝试通过以下方式对所有值使用try catch块:
try
{
string firstName = e.bestName.givenName;
}
catch (Exception)
{
Console.WriteLine("Value of first Name does not exist.");
}
这行得通,但是出现了问题,因为我必须在消息框中显示firstName,surName和address,如果我使用try catch,则无法访问try catch范围之外的值。我该怎么做?
简而言之,如何首先检查给定名称,姓氏,street1,城市,postalCode,国家/地区,numberValue等是否存在,然后最终将其设置为相应的字符串?
任何帮助将不胜感激!如果问题仍然不清楚,请告诉我。
这里有些不清楚的地方,例如itemSummaryRes和EntitySummaryRes,以及EntitySummaryRes中的任何条目是否可以为空。但是……也许只是在其中贴上一些“?”,就像这样:
string firstName = e.bestName?.givenName;
还建议您修剪地址,因为如果街道名称为空,则会出现例如“”的开头。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句