I'm drawing a blank how I need to structure these classes to get what I want - here's a simplified version of what I've got:
public abstract class DocumentBase
{
public abstract class IndexingBase
{
public string universalField;
}
public IndexingBase myIndex;
}
public class ATypeOfDocument : DocumentBase
{
public class Indexing : DocumentBase.IndexingBase
{
public string nonUniversalField;
}
public ATypeOfDocument()
{
this.myIndex = new Indexing();
}
}
Which works, except I'm not able to access the nonUniversalField - because I'm accessing it through MyInstanceOfATypeOfDocument.myIndex, which is declared as IndexingBase. I tried adding 'public new Indexing myIndex' in ATypeOfDocument, but that caused all sorts of problems (the big one being that there was then two indexing structures in an instance of ATypeOfDocument.)
I guess what I'm looking for is for this code to work:
ATypeOfDocument specDoc = new ATypeOfDocument();
DocumentBase baseDoc = new ATypeOfDocument();
specDoc.myIndex.nonUniversalField = "asdf";
specDoc.myIndex.universalField = "asdf";
baseDoc.myIndex.universalField = "asdf";
// ((ATypeOfDocument)baseDoc).myIndex.universalField should be "asdf"
Also, I apologize if I'm missing anything or not posting correctly; it's my first question on Stack Exchange.
You're missing another cast to access the nonUniversalField
, namely of the myIndex
to ATypeOfDocument.Indexing
:
((ATypeOfDocument.Indexing)((ATypeOfDocument)baseDoc).myIndex).nonUniversalField