从void方法返回一个字符串 [英] return a string from void method

查看:108
本文介绍了从void方法返回一个字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为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 calls getID which is the only one which sets your mainID variable - so the email method will always return null, so you will pass null 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆