Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8981129
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T20:15:24+00:00 2026-06-15T20:15:24+00:00

I have a class public class Foo{ public Foo{…} private void someFunction(){…} … private

  • 0

I have a class

public class Foo{
    public Foo{...}

    private void someFunction(){...}
    ...
    private Acessor{
        new Acessor
    }
}

with some private functionality (someFunction). However, sometimes, I want to allow another class to call Foo.SomeFunction, so I have an inner class access Foo and pass out that:

public class Foo{
    public Foo{...}

    private void someFunction(){...}
    ...
    public Acessor{
        Foo _myFoo;
        new Acessor(Foo foo){_myFoo = foo;}
        public void someFunction(){
            _myFoo.someFunction();
        }
    }
}

With this code, if I want a Foo to give someone else pemission to call someFunction, Foo can pass out a new Foo.Accessor(this).

Unfortunately, this code allows anyone to create a Foo.Accessor initiated with a Foo, and they can access someFunction! We don’t want that. However, if we make Foo.Accessor private, then we can’t pass it out of Foo.

My solution right now is to make Acessor a private class and let it implement a public interface IFooAccessor; then, I pass out the Foo.Accessor as an IFooAccessor. This works, but it means that I have to declaration every method that Foo.Accessor uses an extra time in IFooAccessor. Therefore, if I want to refactor the signature of this method (for example, by having someFunction take a parameter), I would need to introduce changes in three places. I’ve had to do this several times, and it is starting to really bother me. Is there a better way?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T20:15:25+00:00Added an answer on June 15, 2026 at 8:15 pm

    It’s difficult to answer this question, since it’s not clear (to me at least) what exactly you want to achieve. (You write make it difficult for someone to inadverdantly use this code in a comment).

    Maybe, if the method is to be used in a special context only, then explicitly implementing an interface might be what you want:

    public interface ISomeContract {
        void someFunction();
    }
    
    public class Foo : ISomeContract {
        public Foo() {...}
    
        void ISomeContract.someFunction() {...}
    }
    

    This would mean, that a client of that class would have to cast it to ISomeContract to call someFunction():

    var foo = new Foo();
    var x = foo as ISomeContract;
    x.someFunction();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class like this public class foo { private void getThread() {
Say I have some class, e.g. Foo: public class Foo { private Integer x;
I have a class class foo { public: foo(); foo( int ); private: static
I have: class A{ public: virtual void foo(); }; class B : public A{
If I have a class class foo { public: foo() { // spend some
Say I have : class Foo { public: Foo(std::function< void(std::shared_ptr< Foo >) > callback);
I have a class Foo which looks like this class Foo { public: void
I have a class namespace LogToolsTest { public class Foo { private static readonly
I have an already existing generic class public class Foo<T> { private T _item;
Suppose I have: class Foo { ... }; class Bar : public Foo {

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.