views:

49

answers:

1

currently I have a variable and a property:

private System.Xml.Linq.XDocument myDoc;

public System.Xml.Linq.XDocument getMyDoc
        {
            get
            {
                return myDoc;
            }
            set
            {
                myDoc = value;
            }
        }

now I need two docs:

private System.Xml.Linq.XDocument[] myDoc; // array with 2 or 3 XDocuments

My hope is that I'll be able to get or set a specific array element:

get
{
return myDoc(0);
}
set 
{
myDoc(0)=value;
}

is it possible?

If this is important... I don't have all the information at one place since I'm using multithreading.

+2  A: 

You can change your docs variable to an array and then use an indexer:

public class MyXmlDocument
{
    private readonly System.Xml.Linq.XDocument[] docs;

    public MyXmlDocument(int size)
    {
        docs = new System.Xml.Linq.XDocument[size];
    }

    public System.Xml.Linq.XDocument this [int index]
    {
        get
        {
            return docs[index];
        }
        set
        {
            docs[index] = value;
        }
    }
}

static void Main(string[] args)
{
    // create a new instance of MyXmlDocument which will hold 5 docs
    MyXmlDocument m = new MyXmlDocument(5);

    // use the indexer to set the element at position 0
    m[0] = new System.Xml.Linq.XDocument();

    // use the indexer to get the element at position 0
    System.Xml.Linq.XDocument d = m[0];
}
dcp
How can I call this property? I don't understand why there is a string instead of XDocument and what is the "this" stands for. Thanks
Asaf
@Asaf - Sorry about the string thing, that was a mistake on my part. I've edited my answer to include a complete example.
dcp
@scp: it looks like a collection: http://msdn.microsoft.com/en-us/library/6x16t2tx.aspx , it may answer my needs but how can I use it in my class? should I define my param as my new collection instead of Xdocument array
Asaf
@Asaf - You can use a collection or an array, it makes no difference to the indexer. I'm not sure how to answer your question about how to use it in your class. You can just add the array (or collection, if you decide to use that instead) and indexer code as shown above, and then you should be able to use it in your class. Refer to the Main method above for sample usage.
dcp
@scp :That is a first... it works but I can't understand why. There are many properties in the class how come it chooses the right one ... can there be only one property that uses this or I can play with the type and signature... So strange, yet a great answer thanks
Asaf
@Asaf - It's @dcp instead of @scp :). Anyway, you can think of indexers like instance methods, they must have a unique signature within the class. So you couldn't have 2 indexers in the same class that use an int for the index, however, you could have one indexer that uses an int for the index and another that uses a string for the index. The uniqueness of the indexer signature is how it knows which one to use. Refer here for more info: http://www.csharp-station.com/Tutorials/Lesson11.aspx
dcp
@dcp: this time I got it right ;) ... Wonder how I made the same mistake twice. Anyway I'll have to learn indexers better. Yet I think it is save to say that my mistake was to think of the indexer as a simple property... Even if I'm wrong I'll get it after practicing indexers. Until then, I thank you for helping me and showing me something new :)
Asaf