2010-10-18 3 views
1

actuellement j'ai une variable et une propriété:Puis-je définir une propriété pour un tableau?

private System.Xml.Linq.XDocument myDoc; 

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

maintenant je besoin de deux documents:

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

Mon espoir est que je serai en mesure d'obtenir ou de définir un élément de tableau spécifique:

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

est-ce possible?

Si c'est important ... Je n'ai pas toutes les informations à un seul endroit depuis que j'utilise le multithreading.

Répondre

2

Vous pouvez modifier vos documents variables à un tableau et ensuite utiliser un indexeur:

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]; 
} 
+0

Comment puis-je appeler cette propriété? Je ne comprends pas pourquoi il y a une chaîne à la place de XDocument et ce que signifie "ceci". Merci – Asaf

+0

@ Asaf - Désolé pour la chose chaîne, c'était une erreur de ma part. J'ai modifié ma réponse pour inclure un exemple complet. – dcp

+0

@scp: il ressemble à une collection: http://msdn.microsoft.com/en-us/library/6x16t2tx.aspx, il peut répondre à mes besoins mais comment puis-je l'utiliser dans ma classe? devrais-je définir mon param comme ma nouvelle collection au lieu du tableau Xdocument? – Asaf

Questions connexes