不能在同一类内的函数中使用声明类的变量。为什么 ?

发布于 2024-09-30 17:45:52 字数 1463 浏览 3 评论 0原文

我在“Main”类中声明了一个名为“context2”的变量。但我无法在函数“Main_Load”中使用该变量。我做错了什么?

using System;
using System.Windows.Forms;
using Soapi;
using Soapi.Domain;

namespace SO_Console_Client
{
    public partial class Main : Form
    {
        const string apiKey = "*************";
        var context2 = new ApiContext(apiKey).Initialize(false);
        public Main(String GravatarURL, User user)
        {
            InitializeComponent();
            pictureBox1.Load(GravatarURL);  //Loads the Gravatar image from the url

            //set the reputation details
            lblRep.Text = String.Format("Reputation: {0}", user.Reputation);

            //Sets the badge details
            lblBadge.Text = String.Format("Badges: gold={0} silver={1} bronze={2}", user.BadgeCounts.Gold, user.BadgeCounts.Silver, user.BadgeCounts.Bronze);

            groupBox1.Text = user.DisplayName.ToString();
        }

        private void Main_Load(object sender, EventArgs e)
        {
            Soapi.Queries.QuestionsUnansweredQuery query = context2.Official.StackOverflow.Questions.Unanswered;
            foreach (Question q in query)
            {
                try
                {
                    Console.WriteLine(q.Title.ToString());
                    //Console.WriteLine(q.Body.ToString());
                }
                catch (System.NullReferenceException ex)
                {

                }

            }

        }
    }
}

i have declared a variable in the class 'Main' with name 'context2'. But i cannot use the variable inside the function 'Main_Load'. what am i doing wrong ?

using System;
using System.Windows.Forms;
using Soapi;
using Soapi.Domain;

namespace SO_Console_Client
{
    public partial class Main : Form
    {
        const string apiKey = "*************";
        var context2 = new ApiContext(apiKey).Initialize(false);
        public Main(String GravatarURL, User user)
        {
            InitializeComponent();
            pictureBox1.Load(GravatarURL);  //Loads the Gravatar image from the url

            //set the reputation details
            lblRep.Text = String.Format("Reputation: {0}", user.Reputation);

            //Sets the badge details
            lblBadge.Text = String.Format("Badges: gold={0} silver={1} bronze={2}", user.BadgeCounts.Gold, user.BadgeCounts.Silver, user.BadgeCounts.Bronze);

            groupBox1.Text = user.DisplayName.ToString();
        }

        private void Main_Load(object sender, EventArgs e)
        {
            Soapi.Queries.QuestionsUnansweredQuery query = context2.Official.StackOverflow.Questions.Unanswered;
            foreach (Question q in query)
            {
                try
                {
                    Console.WriteLine(q.Title.ToString());
                    //Console.WriteLine(q.Body.ToString());
                }
                catch (System.NullReferenceException ex)
                {

                }

            }

        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

遮云壑 2024-10-07 17:45:52

隐式输入不适用于字段;它仅适用于局部变量。

我认为这就是您的真正意图:

ApiContext context2 = new ApiContext(apiKey);

public Main(String GravatarURL, User user)
{
   context2.Initialize(false);
   ...
}

在极不可能的情况下,ApiContext 是某种连贯接口,ApiContext.Initialize(bool)< /code> 返回一个不同ApiContext对象,这应该是您想要的:

ApiContext context2 = new ApiContext(apiKey).Initialize(false); 

尽管如果您这样做会更清楚:

ApiContext context2;

public Main(String GravatarURL, User user)
{
   context2 = new ApiContext(apiKey).Initialize(false);
   ...
}

不过,我真的对此表示怀疑。

Implicit typing doesn't work with fields; it only works with local variables.

I think this is what your real intention is:

ApiContext context2 = new ApiContext(apiKey);

public Main(String GravatarURL, User user)
{
   context2.Initialize(false);
   ...
}

In the highly unlikely case that ApiContext is some sort of fluent-interface for which ApiContext.Initialize(bool)returns a differentApiContextobject, this should be what you want:

ApiContext context2 = new ApiContext(apiKey).Initialize(false); 

although it would be much clearer if you did:

ApiContext context2;

public Main(String GravatarURL, User user)
{
   context2 = new ApiContext(apiKey).Initialize(false);
   ...
}

I really doubt that, though.

入画浅相思 2024-10-07 17:45:52

这不能编译。 var 类型变量声明不能在类级别,只能在方法级别。

This can't compile. var-type variable declarations cannot be at the class level, only the method level.

╰◇生如夏花灿烂 2024-10-07 17:45:52

允许 var 与字段一起使用存在技术问题。这就是为什么必须指定具体类型的原因。以下是 Eric Lippert 对问题的解释:

为什么不字段上的 var

There are technical issues with allowing var to be used with fields. This is why a concrete type must be specified. Here is an explanation of the issues from Eric Lippert:

Why no var on fields

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文