Tony The Lion
Tony The Lion

Reputation: 63190

C# property inaccessible

Why can I not have a private struct and then create a public property from it?

I get a 'property PubTest is less accessible than test' when I compile.

E.g.:

private struct test
{
    int a;
    double b;
}

test t = new test();

public test PubTest
{
     get { return t; }
}

Upvotes: 1

Views: 1017

Answers (7)

Kilanash
Kilanash

Reputation: 4559

My 2 cents:

Use interfaces?

public interface ITest
{
    int a { get; }
    double b { get; }
}
public class MyClass
{
    private struct test : ITest
    {
        private int _a;
        private double _b;

        public int a 
        { 
            get { return _a; }
        }

        public double b 
        { 
            get { return _b; }
        }
    }
    public ITest PubTest
    {
        get { return new test() as ITest; }
    }
}

Upvotes: 0

Steve Guidi
Steve Guidi

Reputation: 20200

yes, you're right, it's pointless having it private if I need access to it from the outside. What I wanted was readonly access to the members of this struct outside the class its in. That's why I was trying to hide it and only have a getter property

If you need to control the access to the fields of a struct, then you can decorate thet get or set method with an access modifier as follows.

public struct Test
{
    private int a;
    private double b;

    public int A
    {
        get { return a; }
        internal set { a = value; }
    }

    public double B
    {
        get { return b; }
        internal set { b = value; }
    }
}

The internal modifier restricts access to the code in the assembly for which the encompassing type is defined. Using private would restrict the access to the methods on the struct.

Upvotes: 2

Agent_9191
Agent_9191

Reputation: 7253

Like others have mentioned, the structure needs to be at least as visible as the property. If you only want to expose fiends of the structure to outside code, you can use something like this:

    private class Test
    {
        public int MyInt = 1;
        public double MyDouble = 2d;
    }

    Test test = new Test();

    public int PubTest
    {
        get { return test.MyInt; }
    }

Upvotes: 0

Aaronaught
Aaronaught

Reputation: 122624

You can't expose the struct itself if it's private, but you can expose members of it:

public int TestA
{
    get { return t.a; }
    set { t.a = value; }
}

public double TestB
{
    get { return t.b; }
    set { t.b = value; }
}

That's about the best you can do here.

Upvotes: 2

Joel Coehoorn
Joel Coehoorn

Reputation: 415600

It sounds like you think that setting "private" on the struct changes the default access level for the members. That is not what happens at all. The default access level of all the members of the struct is still public; they can be seen anywhere your new struct type can be seen.

What does change is that the struct type itself is private; it's visible only to the class where you declared it (and it must be in a class rather than a namespace, because you can't declare it as private in a namespace). It can't be accessed outside of there at all. That means you have to be careful even using the struct in that assembly. If a public or internal type in the assembly returns it from a public or internal method, you are potentially exposing it to the world outside of where it's allowed to be used, and that's a compile error.

Upvotes: 0

Jon Seigel
Jon Seigel

Reputation: 12401

Outside calling code has to know the return type of the property.

If the struct is made private, it can't be seen from the outside.

Upvotes: 12

user197015
user197015

Reputation:

Because the struct is private, it is accessible only to the type containing it (which you didn't show in your example, but which I'll call T for brevity). If T has a public property, the consumer of that property must, perforce, have access to the type that is returned from the property... but it doesn't, because you declared that type as private to T.

Upvotes: 0

Related Questions