Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 无法访问其他方法中的字符串_C#_Methods - Fatal编程技术网

C# 无法访问其他方法中的字符串

C# 无法访问其他方法中的字符串,c#,methods,C#,Methods,过去几天我一直在努力解决这个问题,但运气不佳,我希望你能帮忙,我对c#很陌生 下面是我的控制台应用程序的一部分,这两个不同的方法在各自的计时器中,以不同的速度运行,因此它们不能在同一个方法中。我使用的是通过httpclient发送JSON的JSON.net/JObject 我正在尝试访问的结果 JObject Grab = JObject.Parse(httpResponse(@"https://api.example.jp/json.json").Result); string itemTi

过去几天我一直在努力解决这个问题,但运气不佳,我希望你能帮忙,我对c#很陌生

下面是我的控制台应用程序的一部分,这两个不同的方法在各自的计时器中,以不同的速度运行,因此它们不能在同一个方法中。我使用的是通过httpclient发送JSON的JSON.net/JObject

我正在尝试访问的结果

JObject Grab = JObject.Parse(httpResponse(@"https://api.example.jp/json.json").Result);

string itemTitle = (string)Grab["channel"]["item"][0]["title"];
从另一个方法,使用以下代码

Console.WriteLine(itemTitle);
我尝试过很多不同的方法,但都没有成功。 下面是关于Json.net的完整代码部分

namespace ConsoleApplication3
{
internal class Program
{
        ...other code

    public static async Task<string> httpResponse(string url)
    {
        HttpClientHandler httpHandler = new HttpClientHandler()
        {
            AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate
        };
        using (var httpClient = new HttpClient(httpHandler))
            return await httpClient.GetStringAsync(url);
    }

    public static void JSONUpdateTimer(object sender, ElapsedEventArgs e)
    {
        JObject Grab = JObject.Parse(httpResponse(@"https://api.example.jp/json.json").Result);

        string itemTitle = (string)Grab["channel"]["item"][0]["title"];
        Console.WriteLine(itemTitle);

        JSONUpdate.Interval = JSONUpdateInterval();
        JSONUpdate.Start();
    }

    public static void SecondTimer(object source, ElapsedEventArgs e)
    {
        Console.WriteLine(itemTitle);
        ...other Commands using "itemTitle"
    }
}
}
命名空间控制台应用程序3
{
内部课程计划
{
…其他代码
公共静态异步任务httpResponse(字符串url)
{
HttpClientHandler httpHandler=新的HttpClientHandler()
{
AutomaticDecompression=DecompressionMethods.GZip | DecompressionMethods.Deflate
};
使用(var httpClient=新的httpClient(httpHandler))
返回wait-httpClient.GetStringAsync(url);
}
公共静态void JSONUpdateTimer(对象发送方,ElapsedEventArgs e)
{
JObject Grab=JObject.Parse(httpResponse(@)https://api.example.jp/json.json(a)结果);
string itemTitle=(string)抓取[“频道”][“项目”][0][“标题”];
Console.WriteLine(itemTitle);
JSONUpdate.Interval=JSONUpdateInterval();
JSONUpdate.Start();
}
公共静态void SecondTimer(对象源,ElapsedEventArgs e)
{
Console.WriteLine(itemTitle);
…使用“itemTitle”的其他命令
}
}
}

我有一种不好的感觉,我错过了一些非常明显的东西,如果它被指出来,我将面对手掌。但我会感谢任何帮助

将名为itemTitle的字符串字段声明为类的成员,在任何方法之外

internal class Program
{
    static string itemTitle; 
    //other code...
}
在方法中,不要声明新变量,只需引用字段即可

public static void JSONUpdateTimer(object sender, ElapsedEventArgs e)
{
    //...
    itemTitle = (string)Grab["channel"]["item"][0]["title"];
    //...
}

方法内部声明的变量在本地范围内限定为该方法,并且在该方法外部不存在。

在任何方法外部声明名为itemTitle的字符串字段作为类的成员

internal class Program
{
    static string itemTitle; 
    //other code...
}
在方法中,不要声明新变量,只需引用字段即可

public static void JSONUpdateTimer(object sender, ElapsedEventArgs e)
{
    //...
    itemTitle = (string)Grab["channel"]["item"][0]["title"];
    //...
}

方法内部声明的变量在局部范围内限定于该方法,并且不存在于该方法之外。

非常感谢,我花了这么多时间在这个问题上,真不敢相信它有多简单。非常感谢,我花了这么多时间在这个问题上,真不敢相信它有多简单。