不能在同一类内的函数中使用声明类的变量。为什么 ?
我在“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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
隐式输入不适用于字段;它仅适用于局部变量。
我认为这就是您的真正意图:
在极不可能的情况下,
ApiContext
是某种连贯接口,ApiContext.Initialize(bool)< /code> 返回一个不同
ApiContext
对象,这应该是您想要的:尽管如果您这样做会更清楚:
不过,我真的对此表示怀疑。
Implicit typing doesn't work with fields; it only works with local variables.
I think this is what your real intention is:
In the highly unlikely case that
ApiContext
is some sort of fluent-interface for whichApiContext.Initialize(bool)
returns a differentApiContext
object, this should be what you want:although it would be much clearer if you did:
I really doubt that, though.
这不能编译。
var
类型变量声明不能在类级别,只能在方法级别。This can't compile.
var
-type variable declarations cannot be at the class level, only the method level.允许 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