简体   繁体   English

错误CS0103:名称“ ressourcesText”在当前上下文中不存在

[英]error CS0103: The name “ressourcesText” does not exist in the current context

I set up a string named ressourcesText, which will be set as the text LblRessources 我设置了一个名为ressourcesText的字符串,该字符串将设置为文本LblRessources

This is the code for this problem : 这是此问题的代码:

public void ressources()
{
    string ressourcesText = "Ressources:"
                        + "\n Wood :" + wood
                        + "\n Stone : " + stone
                        + "\n Wheat : " + wheat
                        + "\n Food : " + food;
}

public void Form1_Load(object sender, EventArgs e)
{ 
    LblRessources.Text = ressourcesText;
}

How can the text for LblRessources use the string ressourcesText? LblRessources的文本如何使用字符串ressourcesText? It usually worked this way. 通常以这种方式工作。

The issue with your code is that ressourcesText is declared locally in the ressources() function. 您的代码存在的问题是ressourcesTextressources()函数中本地声明。

Variables that are declared within { } brackets are destroyed at the end of the bracket, and are called local variables . { }括号内声明的变量在括号的末尾被销毁,称为local variables

To solve this you could make your variable global: 为了解决这个问题,您可以将变量设置为全局变量:

string ressourcesText;
public void ressources()
{
    ressourcesText = "Ressources:"
                        + "\n Wood :" + wood
                        + "\n Stone : " + stone
                        + "\n Wheat : " + wheat
                        + "\n Food : " + food;
}

public void Form1_Load(object sender, EventArgs e)
{ 
    LblRessources.Text = ressourcesText;
}

Now this isn't necessarily good practice, because at the time of declaring your variable, it will be null, and thus can not be used until you call ressources() . 现在这并不一定是一种好习惯,因为在声明变量时,该变量将为null,因此只有在调用ressources()之前才能使用。

Another alternative would be to return the variable through the ressources() function and calling it directly to retrieve the value, like follows: 另一种选择是通过ressources()函数返回变量,然后直接调用它以检索值,如下所示:

public string ressources()
{
    string ressourcesText = "Ressources:"
                        + "\n Wood :" + wood
                        + "\n Stone : " + stone
                        + "\n Wheat : " + wheat
                        + "\n Food : " + food;
    return ressourcesText;
}

public void Form1_Load(object sender, EventArgs e)
{ 
    LblRessources.Text = ressources();
}

That's somewhat safer. 这样比较安全。

You should check out these links for more info: 您应该查看以下链接以获取更多信息:

Local variables 局部变量

C# Methods C#方法

Scopes 范围

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误 CS0103:当前上下文中不存在名称“_context” - Error CS0103: The name '_context' does not exist in the current context 错误CS0103:名称“ TimeSpan”在当前上下文(CS0103)(testingProgram)中不存在? - Error CS0103: The name 'TimeSpan' does not exist in the current context (CS0103) (testingProgram)? 错误 CS0103:当前上下文中不存在名称“currentScreen”(CS0103) - Error CS0103: The name 'currentScreen' does not exist in the current context (CS0103) 错误CS0103:名称“ DebugDisplayString”在当前上下文中不存在 - error CS0103: The name 'DebugDisplayString' does not exist in the current context 错误CS0103:名称“ txtReferralCode”在当前上下文中不存在 - error CS0103: The name 'txtReferralCode' does not exist in the current context 错误 CS0103:当前上下文中不存在名称“playfabManager” - error CS0103: The name 'playfabManager' does not exist in the current context 错误 CS0103:名称“SetSolvedState”在当前上下文中不存在 - error CS0103: The name 'SetSolvedState' does not exist in the current context 错误cs0103名称'IEnumerator'在当前上下文中不存在 - error cs0103 The Name 'IEnumerator' does not exist in the current context 错误CS0103:名称“ HttpUtility”在当前上下文中不存在 - error CS0103: The name `HttpUtility' does not exist in the current context "错误 CS0103:当前上下文中不存在名称“AssetPreview”" - error CS0103: The name 'AssetPreview' does not exist in the current context
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM