Click here to Skip to main content
15,868,014 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Comments
Sergey Alexandrovich Kryukov 18-Oct-12 19:40pm    
Wow! one of the easiest questions I saw here!
--SA
FourCrate 18-Oct-12 20:07pm    
I'm a newbie xD

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
 
Share this answer
 
v2
Comments
FourCrate 18-Oct-12 20:02pm    
Thanks for the input :)
Sergey Alexandrovich Kryukov 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 18-Oct-12 22:07pm    
Done
Sergey Alexandrovich Kryukov 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
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"; }
	}
}
 
Share this answer
 
v2
Comments
FourCrate 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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900