Class accessibility question

if a have a class defined as:

public class ClassB : ClassA
{

}

any objects of ClassB i create will have access to the members of ClassA. how do i prevent that? i want the members of ClassA to be accessible from within the methods of ClassB, but not any objects of ClassB.
RepriseMISAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mike TomlinsonMiddle School Assistant TeacherCommented:
Don't Inherit from ClassA then...instead declare an Instance of ClassA inside ClassB as a private member.  Then make your public Functions of ClassB access the private instance of ClassA in the manner you desire...
0
PockyMasterCommented:
Make the members of classA protected, in that way they can only be accessed by derived classes, but derived classes will not expose them
0
RepriseMISAuthor Commented:
PockyMaster, can you give me an example of how this could be implemented? thanks
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

PockyMasterCommented:
using System;

namespace ConsoleApplication1
{
      /// <summary>
      /// Summary description for Class1.
      /// </summary>
      class Class1
      {
            /// <summary>
            /// The main entry point for the application.
            /// </summary>
            [STAThread]
            static void Main(string[] args)
            {
                        
                  ClassB b = new ClassB();
                  Console.WriteLine( b.GetValue());

                  b.Increase();
                  Console.WriteLine( b.GetValue());

            }

      }


      class ClassA
      {
            protected  int m_iSomeValue;

            public void SomeMethodYouLikeToExpose()
            {
                  //implement this
            }
      }

      class ClassB : ClassA
      {
            public void Increase()
            {
                  //increase the protected member in your base class
                  m_iSomeValue++;
            }

            public String  GetValue()
            {
                  //you are still allowed to return it
                  //but you cannot access it directly
                  //in instances from class B
                  return m_iSomeValue.ToString();
            }

      }
}


0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
RepriseMISAuthor Commented:
thanks. one more question: the protected members of ClassA can still be accessed by an object of class A correct?

ClassA clsA = new ClassA();
clsA.m_iSomeValue++;
0
PockyMasterCommented:
Yeah, I don't know if you want to have that or not, if you want to prevent that change
class ClassA into:

abstract class ClassA

in that way people are not able to create instances directly from classA
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.