简体   繁体   English

c#,具有字符串数组类型的属性的类,设置不起作用

[英]c#, class with properties of type string array, set not working

I have a class that has several properties, a couple which are string arrays. 我有一个具有多个属性的类,其中几个属性是字符串数组。 When the following statement: 当以下语句:

MyObj.Str1[1] = "AAA";

MyObj.Str1[1] does not contain "AAA". MyObj.Str1[1]不包含“ AAA”。 Using debug breakpoints, I see the set routine doesn't execute (the get routine does execute). 使用调试断点,我看到set例程没有执行(get例程确实执行了)。

The property looks like: 该属性如下所示:

public string[] Str1
{
    get { return bdr.GetArrVal(1, 25, 7); }
    set { bdr.SetArrVal(1, 25, 7, value); }
}

GetArrVal() builds and returns a string array from class internal data. GetArrVal()构建并从类内部数据返回一个字符串数组。 SetArrVal() sets class internal data from the incoming array. SetArrVal()设置传入数组中的类内部数据。

I tried using indexers but had too many problems passing class internal data into the class describing Str1 . 我尝试使用索引器,但是在将类内部数据传递到描述Str1的类中时遇到了太多问题。

Please note that the statement 请注意,声明

MyObj.Str1 = arr1;

works, where arr1 is a string array. 有效,其中arr1是字符串数组。 The program breaks at the set routine. 程序在设置的例程处中断。

All of this makes me think I cant do what I want. 所有这些使我觉得我做不到我想要的。 Can you assign a single element of a string-array property of an object? 您可以分配对象的字符串数组属性的单个元素吗?

MyObj.Str1 is a string (which is a group of characters) and MyObj.Str1[1] is the char on the second index of that string. MyObj.Str1是一个字符串(一组字符),而MyObj.Str1[1]是该字符串的第二个索引上的char。 You are assigning "AAA" three elements to a single character . 您正在为单个字符分配"AAA" 三个元素 This does not make any sense if you are trying to " assign a single element of a string-array property of an object? " 如果尝试“ 分配对象的字符串数组属性的单个元素? ”,这没有任何意义

Str1 is the property name as posted by you public string[] Str1 { which returns a string[] . Str1是由您发布的属性名public string[] Str1 {返回string[] So when you say MyObj.Str1[1] = "AAA"; 所以当你说MyObj.Str1[1] = "AAA"; you are actually trying to set a array element returned by Str1 property and not the property itself and thus the statement MyObj.Str1 = arr1; 您实际上是在尝试设置由Str1属性返回的数组元素,而不是属性本身,因此设置了语句MyObj.Str1 = arr1; works fine. 工作正常。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM