简体   繁体   English

变量未声明或从未分配,并且重建不起作用

[英]The variable is either undeclared or was never assigned and rebuilding does not work

I have the same problem that has been asked here and over at the MS forums a lot - I have a combobox control and a string array, so I assign the string array to the combobox in 我有很多在MS论坛上和这里反复问过的相同问题-我有一个combobox控件和一个字符串数组,因此我将字符串数组分配给了

Form1.Designer.cs Form1.Designer.cs

private void InitializeComponent()
{
...
this.ComboBox_Target_0.Items.AddRange(Ranks);
...
}

the array, itself is declared like so: 数组本身声明如下:

string[] Ranks = { "Rank 1", "Rank 2", "Rank 3", "Rank 4", "Rank 5", "Rank 6", "Rank 7", "Rank 8", "Rank 9", "Rank 10", "Rank 11", "Rank 12" };

If I build my solution the form runs fine and the combobox has all the items listed and it works fine. 如果我构建解决方案,则表单运行正常,并且组合框列出了所有项目,并且工作正常。 However the form designer refuses to start saying "The variable Ranks is either undeclared or was never assigned". 但是,表单设计者拒绝开始说“变量Rank未声明或从未分配”。

I have seen the other questions about this same problem and have already tried their (only) solution - to rebuild the solution, close VS and open it back up. 我已经看到了关于同一问题的其他问题,并且已经尝试了他们的(唯一的)解决方案-重建解决方案,关闭VS并将其打开备份。 But that didn't help. 但这没有帮助。 I tried putting the string array in "InitializeComponent()" and "public Form1()", rebuilding and etc. each time. 我尝试将字符串数组分别放置在“ InitializeComponent()”和“ public Form1()”中,并进行重建等。 However it has not fixed itself for me. 但是它并没有为我自己解决。

Let me just check - choosing "Ignore and Continue" in this case would not jeopardize anything, right? 让我检查一下-在这种情况下选择“忽略并继续”不会危害任何内容,对吗?

I agree that you shouldn't modify the designer file. 我同意您不应修改设计器文件。 If there's some reason you need those values available at design time, and you don't want to use the editor, the only way I got it to work without an error is to put this at the very top of the InitializeComponent method: 如果出于某种原因需要在设计时使用这些值,并且又不想使用编辑器,则可以正常使用的唯一方法是将其放在InitializeComponent方法的最顶部:

string[] Ranks = new string[] { "Rank 1", "Rank 2", "Rank 3", "Rank 4", "Rank 5", "Rank 6", "Rank 7", "Rank 8", "Rank 9", "Rank 10", "Rank 11", "Rank 12" };

I added new string[] to the declaration. 我在声明中添加了new string[] This is based on how the editor will create the list at design time: 这基于编辑器在设计时如何创建列表的方式:

this.ComboBox_Target_0.Items.AddRange(new object[] {...

After defining Ranks like that, your line will work: 在定义了这样的Ranks之后,您的行将起作用:

this.ComboBox_Target_0.Items.AddRange(Ranks);

If you only need the values at runtime, it's better to do it like this (or a variant thereof): 如果仅在运行时需要这些值,则最好这样做(或其变体):

public partial class Form1 : Form
{
    string[] Ranks = { "Rank 1", "Rank 2", "Rank 3", "Rank 4", "Rank 5", "Rank 6", "Rank 7", "Rank 8", "Rank 9", "Rank 10", "Rank 11", "Rank 12" };

    public Form1()
    {
        InitializeComponent();
        this.ComboBox_Target_0.Items.AddRange(Ranks);
    }
}

暂无
暂无

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

相关问题 变量未声明或从未分配 - The variable is either undeclared or was never assigned 变量未声明或从未分配警告 - The variable is either undeclared or was never assigned warning Winform设计X64不起作用“变量usercontrol1未声明或从未分配” - Winform design X64 doesn't work “The variable usercontrol1 is either undeclared or was never assigned” 得到错误变量 <variable name> 要么未申报,要么从未分配 - got error The variable <variable name> is either undeclared or was never assigned 变量'variable_name'要么未声明,要么从未分配过 - The variable 'variable_name' is either undeclared or was never assigned 变量“ control”是未声明的或从未分配过的-除非如此,否则应用程序将进行编译 - The variable 'control' is either undeclared or was never assigned - Except it is, and the application compiles 未声明或从未分配导致错误“变量”的Windows Forms Designer类继承 - Windows Forms Designer class inheritance causing error 'variable' is either undeclared or was never assigned 变量未声明或从未分配? 和数据读取器无法正常工作? - variable was undeclared or was never assigned? and data reader not working? 变量已分配但从未使用过 - The variable is assigned but never used '变量已赋值,但从未使用过它的值' &amp; '该名称在当前上下文中不存在' - 'The variable is assigned but its value is never used' & 'The name does not exist in the current context'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM