简体   繁体   中英

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". Using debug breakpoints, I see the set routine doesn't execute (the get routine does execute).

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. SetArrVal() sets class internal data from the incoming array.

I tried using indexers but had too many problems passing class internal data into the class describing Str1 .

Please note that the statement

MyObj.Str1 = arr1;

works, where arr1 is a string array. 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. You are assigning "AAA" three elements to a single character . 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[] . So when you say 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; works fine.

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.

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