[英]How to assign a variable name to each index of an array?
public string[] TestResults = new string[8];
I want to assign each item of the array above to a variable.我想将上面数组的每个项目分配给一个变量。 For example,
例如,
TestName = TestResults[0];
I am getting the message: A field initializer cannot reference the non static field, method or property" when I do the following:我收到消息:当我执行以下操作时,字段初始值设定项无法引用非 static 字段、方法或属性:
public string TestName = TestResults[0];
Please suggest me how can I resolve this.请建议我如何解决这个问题。
You can't do that in a variable initializer, basically... although the value would be null anyway.基本上,您不能在变量初始化程序中执行此操作……尽管无论如何该值都是 null 。 You can't refer to
this
within a variable initializer, so you'd have to write:您不能在变量初始化程序中引用
this
,因此您必须编写:
public class Foo
{
// I hope your fields aren't really public...
public string[] TestResults = new string[8];
public string TestName;
public Foo()
{
TestName = TestResults[0];
}
}
Note that this would only retrieve the value at construction anyway.请注意,无论如何这只会在构造时检索值。 It wouldn't associate the variable itself with the first element in the array;
它不会将变量本身与数组中的第一个元素相关联; either could change without affecting the other.
任何一个都可以改变而不影响另一个。 If you want
TestName
to always be associated with TestResults[0]
you might want to use a property instead:如果您希望
TestName
始终与TestResults[0]
相关联,您可能希望使用属性:
public class Foo
{
// I hope your fields aren't really public...
public string[] TestResults = new string[8];
public string TestName
{
get { return TestResults[0]; }
set { TestResults[0] = value; }
}
}
You seem to assume that, if your code worked, TestName
becomes an alias for TestResults[0]
, such that reading from or writing to that variable also changes the array.您似乎假设,如果您的代码有效,则
TestName
成为TestResults[0]
的别名,这样读取或写入该变量也会更改数组。 This is not the case.不是这种情况。
What you can do, is using a property for this:您可以做的是为此使用属性:
public string[] TestResults;
public MyClass()
{
TestResults = new string[8];
}
public string TestName
{
get { return TestResults[0]; }
set { TestResults[0] = value; }
}
If you are looking to have a synonym for the index of the array you can use the following:如果您正在寻找数组索引的同义词,您可以使用以下内容:
public string TestName
{
get { return TestResults[0]; }
set { TestResults[0] = value; }
}
This creates a set of methods called a property that are called in a syntax similar to a variable.这将创建一组称为属性的方法,这些方法以类似于变量的语法调用。 You can drop the
set
part if you don't want write access externally.如果您不想在外部进行写访问,则可以删除
set
部分。
If you want a copy of the variable you will need to write to it at some other point such as in the constructor.如果您想要一个变量的副本,您将需要在其他某个点写入它,例如在构造函数中。
This happens because发生这种情况是因为
public string TestName = TestResults[0];
Will set TestName to the same instance of string that is stored in TestResults[0].将 TestName 设置为存储在 TestResults[0] 中的相同字符串实例。 In other words, TestName will be a reference to the object stored in TestResults[0], not a reference to the memory address that is TestResults[0].
换句话说,TestName 将是对存储在 TestResults[0] 中的 object 的引用,而不是对作为 TestResults[0] 的 memory 地址的引用。
Depending on how your code is set up, I would just use properties and their getters:根据您的代码的设置方式,我将只使用属性及其吸气剂:
public string TestName
{
get { return TestResults[0]; }
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.