Parent property in class hierarchy (Newbie)

  • Thread starter Thread starter Steve
  • Start date Start date
S

Steve

Hi, I'm just crossing over from VB6 to C# - so still quite new to it all.
My question is how is it possible to reference a parent class of a class for
example.

Books.cs
Book.cs
Pages.cs
Page.cs
Sentences.cs
Sentence.cs

And for me to go MySentence.Parent.Parent.Parent.Parent to get a reference
to the Book.
Thanks in advance and apologies if this is a dumb question from a novice.

Steve.
 
Steve, Something like this?

class Sentence
{
Sentences parent;
public Sentence(Sentences parent)
{
this.parent = parent;
}
public Sentences Parent
{
get{ return parent; }
}
}

class Sentences
{
Page parent;
public Sentences(Page parent)
{
this.parent = parent;
}
public Page Parent
{
get{ return parent; }
}
}

class Page
{
Pages parent;
public Page(Pages parent)
{
this.parent = parent;
}
public Pages Parent
{
get{ return parent; }
}
}

class Pages
{
Book parent;
public Pages(Book parent)
{
this.parent = parent;
}
public Book Parent
{
get{ return parent; }
}
}

class Book
{
Books parent;
public Book(Books parent)
{
this.parent = parent;
}
public Books Parent
{
get{ return parent; }
}
}

class Books
{
public string SeriesName;
}

Books bs = new Books();
Book b = new Book(bs);
Pages ps = new Pages(b);
Page p = new Page(ps);
Sentences ss = new Sentences(p);
Sentence s = new Sentence(ss);
string theName = s.Parent.Parent.Parent.Parent.Parent.SeriesName;

Obviously the Sentences, Pages and Books classes would need to hold a colection of their respective children and have methods to Add, Remove, etc and probably indexers to allow the client to program against them using array like syntax

Regards

Richard Blewett - DevelopMentor
http://staff.develop.com/richardb/weblog
http://www.dotnetconsult.co.uk

nntp://news.microsoft.com/microsoft.public.dotnet.languages.csharp/<[email protected]>

Hi, I'm just crossing over from VB6 to C# - so still quite new to it all.
My question is how is it possible to reference a parent class of a class for
example.

Books.cs
Book.cs
Pages.cs
Page.cs
Sentences.cs
Sentence.cs

And for me to go MySentence.Parent.Parent.Parent.Parent to get a reference
to the Book.
Thanks in advance and apologies if this is a dumb question from a novice.

Steve.
 
Excellent stuff, thanks for the sample.

Steve.

Richard Blewett said:
Steve, Something like this?

class Sentence
{
Sentences parent;
public Sentence(Sentences parent)
{
this.parent = parent;
}
public Sentences Parent
{
get{ return parent; }
}
}

class Sentences
{
Page parent;
public Sentences(Page parent)
{
this.parent = parent;
}
public Page Parent
{
get{ return parent; }
}
}

class Page
{
Pages parent;
public Page(Pages parent)
{
this.parent = parent;
}
public Pages Parent
{
get{ return parent; }
}
}

class Pages
{
Book parent;
public Pages(Book parent)
{
this.parent = parent;
}
public Book Parent
{
get{ return parent; }
}
}

class Book
{
Books parent;
public Book(Books parent)
{
this.parent = parent;
}
public Books Parent
{
get{ return parent; }
}
}

class Books
{
public string SeriesName;
}

Books bs = new Books();
Book b = new Book(bs);
Pages ps = new Pages(b);
Page p = new Page(ps);
Sentences ss = new Sentences(p);
Sentence s = new Sentence(ss);
string theName = s.Parent.Parent.Parent.Parent.Parent.SeriesName;

Obviously the Sentences, Pages and Books classes would need to hold a
colection of their respective children and have methods to Add, Remove, etc
and probably indexers to allow the client to program against them using
array like syntax
 
Back
Top