Home > database >  Getting information from a private class inside a public method
Getting information from a private class inside a public method

Time:07-20

I have the following.

public static class Foo()
{
    private class Bar()
    {
        public float x;
        public float y;
        public float z;
        //... many more properties....
        
    }
    private static _bar = new Bar()

    // Need other classes to be able to get x, y, z
}

I know it is a somewhat strange methodology, but the API I am interfacing with (Unity) requires that Bar() be an empty class (cannot inherit from anything and no functions, nor can it be a static class) of only public fields. For proper accessibility, I don't want other classes to be able to see this class as there is no need to create more than one. So, I put the class inside the static class that utilizes it and create a singleton of sorts (there are static functions that will be in this class that interact with the singleton).

I will have situations where I need to access data from that singleton.

I have tried a basic GetBar() that returns _bar but the compiler does not let me do this (which makes sense, other classes wouldn't understand the type being returned). Thus, my only real solution is to make a bunch of GetX(), GetY(), etc. for every variable that I add to Bar(). This would work, but I would consider it poor practice if it can be avoided since now I need to make sure I manually add a Get() function every time I add a new property (which is messy).

I cannot make _bar public without making Bar() public and then any class could create an instance of Bar() which I don't want.

Is there any way to automate getting the public properties of a private class that is within a public static class?

CodePudding user response:

This really strange architecture. I'm not sure I understood the problem correctly, but I can offer a classic implementation of Singleton pattern:

public static class Foo
{
    public class FooBar
    {
        // Will work
        private float x = Bar.GetInstance().x;

        // Will not work
        private Bar bar = new Bar();
        private float y = Bar.y;
    }
}

public sealed class Bar
{
    public float x;
    public float y;
    public float z;

    private static Bar _instance;

    // You can call the constructor only from Bar class
    private Bar() { }

    // Use this method to access Bar fields
    public static Bar GetInstance()
    {
        // If Bar called first time it creates new instance
        if (_instance == null) _instance = new Bar();

        // If Bar instance was created use old instance
        return _instance;
    }
}

And about:

Thus, my only real solution is to make a bunch of GetX(), GetY(), etc. for every variable that I add to Bar(). This would work, but I would consider it poor practice if it can be avoided since now I need to make sure I manually add a Get() function every time I add a new property (which is messy).

You can use auto-property like:

public float X { get; set; }

This automatically create private field with accessors.

CodePudding user response:

If you want to conceal the implementation (class Bar), but have an access to interface (i.e. x, y, z) you can extract interface required:

// Interface we want to have access to
public IBar {
  float x {get; set;}
  float y {get; set;}
  float z {get; set;}
}

then

public static class Foo() {
  // now Bar implements IBar
  private class Bar : IBar {
    // turn fields into properties which can be accessed via IBar interface
    public float x {get; set;}
    public float y {get; set;}
    public float z {get; set;}
    //... many more properties....
        
  }

  // Now other classes are able to get x, y, z:
  // bar is public and it is of public interface IBar 
  public static readonly IBar bar = new Bar();
}
  •  Tags:  
  • c#
  • Related