我不明白这个方括号,我必须创建什么,这个代码行正在工作,仅用于测试。
filetype = dataBandREP["VRZ.Parent.SIGNATUR"].ToString();
C# can overload operator, including square brackets.
public class Example
{
public string this[string s] // square bracket operator with string argument
{
get
{
return somethingToReturnString;
}
set
{
somethingToSetString = value;
}
}
public string this[int i] // square bracket operator with int argument
{
get
{
return somethingToReturnInt;
}
set
{
somethingToSetInt = value;
}
}
}
The square brackets mean you are referencing a collection and within that collection, you want "VRZ.Parent.SIGNATUR". []
is an indexer with a string as argument.
This code: filetype = dataBandREP["VRZ.Parent.SIGNATUR"].ToString();
uses the indexer of some sort of collection, referring to an object, then converting it to its string format in order to populate the filetype variable.
A very common usage of this is with arrays and DataColumn
collections. String indexers allow you to fetch a value based on, say, a column header, instead of knowing what integral index in the DataTable.Columns
collection is associated with the column.
You're getting different behavior when debugging because the validity of "VRZ.Parent.SIGNATUR"
as a string indexer has changed, probably because of a change in the actual data source. That's assuming that you're getting a null reference or similar. Without knowing what exception you're getting, we can't help much more than that.
Below syntax also works.. my class is derived from a ReadOnlyDictionary of some type "AttributeValue" and it exports double values. It gives the user an error box when the value is not found.
public abstract class AttribDoubleReader: IReadOnlyDictionary<string, AttributeValue>
{
public double this[string key] => GetValue(key);
private double GetValue(string key)
{
if (TryGetValue(key, out AttributeValue value))
{
return value.AsDouble();
}
else
{
MessageBox.Show("ERROR: KEY "+ key + " NOT FOUND.");
throw new KeyNotFoundException();
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.