如何在另一个方法中的一个方法中使用局部变量? [英] How can I use a local variable in a method from another method?
本文介绍了如何在另一个方法中的一个方法中使用局部变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
private void UserYoutubeService()
{
var youtubeService = new YouTubeService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = Assembly.GetExecutingAssembly().GetName().Name
});
}
我想在此方法中使用变量 youtubeService
:
And I want to use the variable youtubeService
in this method:
static List<string> videosList = new List<string>();
public async void RetrieveUploadsList()
{
UserCredentials();
UserYoutubeService();
var channelsListRequest = youtubeService.Channels.List("contentDetails");
}
我在代码的其他位置使用了 UserYoutubeService
方法,但现在我需要在方法 RetrieveUploadsList .如何从
UserYoutubeService
中传递变量 youtubeService
?
I'm using the method UserYoutubeService
in other places in my code but now I need to use the local variable youtubeService
properties in the method RetrieveUploadsList
. How can I pass the variable youtubeService
out of the UserYoutubeService
?
推荐答案
最简单的方法是从 UserYoutubeService
返回所需的值,如:
The simplest way would be to return the value you want from UserYoutubeService
, as in:
private YouTubeService UserYoutubeService() // <-- Note return type
{
return new YouTubeService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = Assembly.GetExecutingAssembly().GetName().Name
});
}
您将这样使用:
public async void RetrieveUploadsList()
{
UserCredentials();
var youtubeService = UserYoutubeService(); // <--- Change is here
var channelsListRequest = youtubeService.Channels.List("contentDetails");
...
这篇关于如何在另一个方法中的一个方法中使用局部变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文