从void方法返回一个字符串 [英] return a string from void method
问题描述
我有一个名为Wrequest方法的Web请求方法,该方法向API发送一个post请求,它返回一个XML输出,我从中提取一个'id'。
提取函数称为'getID'。但是,当我将'getID'传递给email()方法时,我得到一个空响应而不是id。
I have a web request method called Wrequest method, which sends a post request to a API and it returns a XML output, from which i extract an 'id'.
The extraction function is called 'getID'. however, when I pass the 'getID' to email() method, i a get a null response instead of the id.
public string mainID = "";
public void getID()
{
var document = XDocument.Parse(WRequest());
var href = document.Descendants("link").Single().Attribute("title").Value;
href = href.Replace("Issue", "");
string id = Regex.Match(href, @"\d+").Value;
mainID = id.ToString();
}
public string email()
{
var url = "";
string uri = mainID;
return url = uri;
}
我调用void方法的方法是不正确的?如果是的话,请提出一些建议,如何解决上述问题。
这就是我在pageload中执行上述方法的方法,以供进一步参考:
Is my approach to the calling the void method incorrect? if so, please advice some advice, as how I can resolve the above issue.
This is how I am executing the above methods in my pageload, for further reference:
string nm = WRequest();
string em = email();
Label1.Text = Server.HtmlEncode(nm);
Label2.Text = Server.HtmlEncode(em);
推荐答案
该代码中没有任何内容调用getID
这是只有一个设置mainID
变量 - 所以电子邮件
方法将始终返回null
,因此您将null
传递给HtmlEncode。
Nothing in that code callsgetID
which is the only one which sets yourmainID
variable - so thenull
, so you will passnull
to HtmlEncode.
将您的代码更改为:
change your code to this:
public string getID()
{
var document = XDocument.Parse(WRequest());
var href = document.Descendants("link").Single().Attribute("title").Value;
href = href.Replace("Issue", "");
string id = Regex.Match(href, @"\d+").Value;
return id.ToString();
}
public string email()
{
return getID()
}
但它仍然是一个奇怪的结构。
调用getId()或调用email()没有任何区别。
您的电子邮件()不做任何事情。
尝试为您的代码赋予更多意义
But it's still a strange construction.
Calling getId() or calling email() makes no difference.
Your Email() does not do anything.
Try to give more meaning to your code
公共字符串电子邮件()
{
mainID = string.Empty;
getID();
var url = ;
string uri = mainID;
return url = uri;
}
试试这个。
public string email()
{
mainID = string.Empty;
getID();
var url = "";
string uri = mainID;
return url = uri;
}
Try this.
这篇关于从void方法返回一个字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!