【问题标题】:how to use VAR outside any function如何在任何函数之外使用 VAR
【发布时间】:2016-08-17 03:29:43
【问题描述】:

我正在尝试学习 tweetinvi 凭证,所以我有以下 UI

还有这两种方法,initAuthenticationstartAuthentication

    private void initAuthentication()
    {            
            var appCredentials = new TwitterCredentials(consumerKey, consumerSecret);
            var authenticationContext = AuthFlow.InitAuthentication(appCredentials);
            Process.Start(authenticationContext.AuthorizationURL);

            //i am commenting the following code

            //var userCredentials = AuthFlow.CreateCredentialsFromVerifierCode(textBox1.Text, authenticationContext);
            //Auth.SetCredentials(userCredentials);

            //so the user have some time to copy the pinCode
            //paste them into available textbox
            // and continue executing startAuthentication() by clicking authenticate button.
    }

private void startAuthentication (string pinCode)
{
    //if we split like this, the authenticationContext is error, because it doesn't exist in current context

    var userCredentials = AuthFlow.CreateCredentialsFromVerifierCode(pinCode, authenticationContext);
    Auth.SetCredentials(userCredentials);
}

如果我们将这两个部分加入一个功能,用户就没有时间将密码复制粘贴到文本框中。

但是,如果我们将这两个部分拆分为不同的功能,则用户需要一些时间将 pin 码复制粘贴到文本框中,但 authenticationContext 似乎出错了,因为 it doesn't exist in current context

有什么办法可以解决这个问题吗?

【问题讨论】:

  • 你只能在本地范围内使用 var 而不是在全球范围内
  • @MostafizurRahman 同意,但是如何绕过或使我的代码按预期工作?
  • 您想让对话框成为模态,然后在按下验证按钮之前它不会返回到应用程序。
  • 大家好,我是 tweetinvi 的开发者。我可以说这两个答案都是正确的。有很多方法可以做你想做的事,但最简单的一种是将变量存储在私有属性中。

标签: c# var tweetinvi


【解决方案1】:

我不确定这个 API 应该如何工作,但在方法中创建的变量的范围仅存在于该方法中。

你有两个选择:

  1. 创建一个AuthFlow.InitAuthentication(appCredentials);返回的任何类型的类变量,在你的第一个方法中设置它,然后你可以从第二个方法访问它。

  2. AuthFlow.InitAuthentication(appCredentials); 的返回类型的任何参数添加到您的第二种方法,然后在您调用第二种方法时传入上下文。

编辑

稍微考虑一下,上下文通常是你需要传递的东西,所以选项 1 可能会更好。

编辑 2

我查了一下,InitAuthentication 返回IAuthenticationContext。所以像这样在你的方法之外创建一个类变量

IAuthenticationContext _authContext;

然后在方法一中

_authContext = AuthFlow.InitAuthentication(appCredentials);

然后在方法二中

var userCredentials = AuthFlow.CreateCredentialsFromVerifierCode(pinCode, _authContext);

还要回答关于var的问题... var 基本上是创建变量的简写,而不必每次都输入类型。但是对var 的一个警告是,必须为它所使用的变量分配一些值,否则 var 无法推断出基础类型,您将收到编译器错误。

例如

var myVariable1;

var myVariable1 = null;

将引发编译时错误,因为编译器无法推断 myVariable1 的类型应该是什么。

正确的语法是

var myVariable1 = 4;

或者

var myVariable1 = "hello world";

或者

var myVariable1 = SomeMethodThatReturnsSomething();

【讨论】:

    【解决方案2】:

    创建类字段而不是局部变量

    class ClassName
    {
        AuthenticationContext authenticationContext;
    
        private void initAuth()
        {
            // set authenticationContext
            authenticationContext = AuthFlow.InitAuthentication(appCredentials);
        }
    
        private void startAuth(string pin)
        {
            // use authenticationContext
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-27
      • 2021-01-19
      • 1970-01-01
      • 2016-11-30
      • 1970-01-01
      • 2022-08-21
      相关资源
      最近更新 更多