Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
I know how to access a class one hierarchy up using the base.fieldname but how do I access one higher up than just one? For example, let's say 2 classes up the hierarchy.
Posted 18-Oct-12 14:27pm
Comments
Sergey Alexandrovich Kryukov at 18-Oct-12 19:40pm
   
Wow! one of the easiest questions I saw here!
--SA
FourCrate at 18-Oct-12 20:07pm
   
I'm a newbie xD
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

The distance in hierarchy makes absolutely no difference. Some indirect base class works exactly as a direct base class. Just access what you need directly, as with a direct base class. Only the context and access modifiers effect the access.
 
—SA
  Permalink  
v2
Comments
FourCrate at 18-Oct-12 20:02pm
   
Thanks for the input :)
Sergey Alexandrovich Kryukov at 18-Oct-12 21:42pm
   
You are welcome.
As this is the answer, please accept it formally (green button) -- thanks.
You can accept more than one.
--SA
FourCrate at 18-Oct-12 22:07pm
   
Done
Sergey Alexandrovich Kryukov at 18-Oct-12 22:18pm
   
Great. I think we can consider the issue closed, unless you have follow-up questions and they are closely related to the original one.
Good luck, call again.
--SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

If the higher level classes expose the member you want as something other than private, it'll be available to you at the same level as the class you're directly inheriting from using only the base identifier.
 
For example, you have Class3 that inherits from Class2 that inherits from Class1. In Class1 you expose a property called Test1 and in Class2 you expose a property called Test2. Now in Class3, both Test1 and Test2 will be available from the base identifier.
public class MyBaseClass1
{
 
	protected string Test1 {
		get { return "Test1"; }
	}
}
 
public class MyBaseClass2 : MyBaseClass1
{
 
	protected string Test2 {
		get { return "Test2"; }
	}
}
 
public class MyBaseClass3 : MyBaseClass2
{
 
	protected string Test3 {
		get { return base.Test1 + base.Test2 + "Test3"; }
	}
}
  Permalink  
v2
Comments
FourCrate at 18-Oct-12 20:02pm
   
Thanks for the input :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 7,903
1 Sergey Alexandrovich Kryukov 7,127
2 DamithSL 5,604
3 Manas Bhardwaj 4,986
4 Maciej Los 4,820


Advertise | Privacy | Mobile
Web02 | 2.8.1411023.1 | Last Updated 18 Oct 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100