-3

How do i declare an indexed property?

public class PublishProperties : ScriptableObject {

List<string> m_shellPathsT = new List<string>();
List<string> m_shellPathsL = new List<string>();
public List<string> ShellPath[int index]
{
    get 
    {
        if (index == 0)
            return m_shellPathsT;
        else
            return m_shellPathsL;
    }
}

This doesn't compile and Im not sure how to code this. Because of other requirements i have to have the two different lists which are declared like this.

I normally would have an array of lists...

or like this

public List<string>[] m_shellPaths = { new List<string>(), new List<string>() };

however this again doesn't work with other factors... (basically there is some serialization that happens automatically that doesn't work with variables declared in a constructor or like the above. )

vbbartlett
  • 179
  • 2
  • 14
  • Did I get it right, are you trying to create an index-access for your own class? – bash0r Feb 19 '13 at 23:35
  • http://msdn.microsoft.com/en-us/library/vstudio/6x16t2tx.aspx you need to change property to public string this[int i] – Andriy Khrystyanovich Feb 19 '13 at 23:36
  • 1
    You can only make an indexer (like `string this[int index]`), you can't make an indexed property. To achieve that, just have the type of `ShellPath` be something that implements `this[int]` – millimoose Feb 19 '13 at 23:36
  • 1
    Seems like a strange design... if there are only two lists why not just have two different methods / getters? It doesn't make much sense for `[0]` to refer to one list and for `[-1]`, `[10000]` and `[0xDEADBEEF]` to all refer to another list. – dreamlax Feb 19 '13 at 23:40
  • because of serialization, it writes this data out, if I have arrays of lists or other (better) methods of declaring this, the data isn't serialized... sucks but it is what I am stuck with... – vbbartlett Feb 19 '13 at 23:46
  • @dreamlax because another class that is accessing this is basically doing it by index. This code was simplified to only show data specific to the question. I have more data... other lists and arrays of strings and integers that can be declared inline... which then makes them accessible because they are simple data types. – vbbartlett Feb 19 '13 at 23:48

3 Answers3

1

Please read the docs before asking questions.

public List<string> this[int index]
{
    get 
    {
        if (index == 0)
            return m_shellPathsT;
        else
            return m_shellPathsL;
    }
}
wRAR
  • 25,009
  • 4
  • 84
  • 97
  • If we have List m_shellPathsT than indexer should return simple string ? – Andriy Khrystyanovich Feb 19 '13 at 23:38
  • @AndriyKhrystyanovich the original code returns lists. – wRAR Feb 19 '13 at 23:38
  • @wRAR I simplified the code and have read the docs... this isn't what I need. I need to index an array within the class not the class itself... – vbbartlett Feb 19 '13 at 23:45
  • 1
    @vbbartlett your code suggests totally different thing. – wRAR Feb 19 '13 at 23:48
  • @wRAR, I can see that after reading many of the comments... from a previous comment, it seems like this isn't possible? I would have to actually write a get & set method to do what I intend? – vbbartlett Feb 19 '13 at 23:50
  • @vbbartlett if code that you posted is totally different from code you wanted to post, we cannot answer your question as we didn't see it. – wRAR Feb 19 '13 at 23:54
  • @wRAR the code is not different, it is minimized to the specific question... Basically is there a way to index an accessor? I don't want an indexer to the class itself. Thought that was clear from the first line. – vbbartlett Feb 20 '13 at 00:00
  • @vbbartlett "indexing the accessor" is nonsense. The getter returns an object and you can index this object if it's indexable. – wRAR Feb 20 '13 at 00:13
  • @wRAR apology, what I should have asked is can you index a property. But I am somewhat new to c# and its syntax and terminology. – vbbartlett Feb 20 '13 at 00:17
0

C# doesn't support indexed properties. You can index the class itself using

public List<string> this[int index]
{
    get 
    {
        if (index == 0)
            return m_shellPathsT;
        else
            return m_shellPathsL;
    }
}

But in this very specific case I was unable to do such because needed several sets of this type of implementation and the serialization prevented me from using more elegant declarations.

Methods could be used but don't have the same power as properties.
I found more information about this.

However under the circumstance there is no reason why I can't add an additional array

public List<string> m_shellPathsT = new List<string>();
public List<string> m_shellPathsL = new List<string>() ;
public List<string>[] m_shellPaths = new List<string>[2];

public PublishProperties()
{
    m_shellPaths[0] = m_shellPathsT;
    m_shellPaths[1] = m_shellPathsL;
}
Community
  • 1
  • 1
vbbartlett
  • 179
  • 2
  • 14
0

Based on your comments, it seems you want to access items of either m_shellPathsT or m_shellPathsS via ShellPaths.

So, assuming foo is an object of type PublishProperties, you want to be able to write code like this:

foo.ShellPaths[0];

What happens here is actually two things:

  1. You are accessing property ShellPaths on type PublishProperties. It returns a List<string>
  2. You are accessing the item at index 0 of the returned list. It returns a string.

The property ShellPaths has the following structure:

public List<string> ShellPaths
{
    get { return <something>; }
}

As you can see, there is no index.

But because you want to access different lists based on the index, the implementation of ShellPaths can't simply return either one of your internally stored lists.

You will have to create a new list that returns the first item of m_shellPathsT if the index is 0. Otherwise it returns the corresponding item of m_shellPathsS.

To achieve this, you could implement ShellPaths like this:

public List<string> ShellPaths
{
    get { return m_shellPathsT.Take(1).Concat(m_shellPathsS.Skip(1)).ToList(); }
}

Now, that would work, but would be quite unefficient: Every time someone accesses ShellPaths a new list is created. It would be better if you would create that special list once, maybe in the constructor or whenever the content of any of the internal lists changes.

Daniel Hilgarth
  • 171,043
  • 40
  • 335
  • 443