1

Suppose I have a structure in my class:

class MyClass 
{
    public struct MyStruct_t
    {
        public int someValue1;
        public int someValue2;
    }
    public MyStruct_t MyStruct;
    public InitStructure()
    {
        MyStruct.someValue1 = 1;
        MyStruct.someValue2 = 1;
    }
}

I want this structure will be read only from outside only and read/write inside the class. Like this:

MyClass cls = new MyClass();
cls.InitStructure();
int value = cls.MyStruct.someValue1;
cls.MyStruct.someValue2 = value; // here must be an error

I guess it should be something like friend keyword in C++. How can I do that?

folibis
  • 12,048
  • 6
  • 54
  • 97
  • 1
    actually here: `cls.MyStruct.someValue2 = value; // here must be an error` you should get an error. have a look on [this](http://stackoverflow.com/questions/1747654/cannot-modify-the-return-value-error-c-sharp) – Mong Zhu Aug 31 '16 at 06:52
  • Thanks, @MongZhu. I was surprized that structure returns by value and not by reference. From one side it solve my problem. From another size it's not so good to copy the structure every time I need to access its member. All that because I still think in terms of C++ :) – folibis Aug 31 '16 at 07:03
  • Definitely, the first thing to do is to get out of C++ thinking. In C++, `struct` and `class` are practically the same (so far as I'm aware) except for the default accessibility. If you are going to program in C#, first familiarise yourself with what `struct` and `class` mean here and the differences between them, before deciding which one to use in each situation. – Damien_The_Unbeliever Aug 31 '16 at 07:40

2 Answers2

0

public int someValue1 { get; protected set; } public int someValue2 { get; protected set; }

This is probably what you are looking for. protected keyword makes the set operation available in the declaring class, as well as in derived ones, but not in other contexts.

Jakub Jankowski
  • 731
  • 2
  • 9
  • 20
  • I get an error: `cannot be used in this context because the set accessor is inaccessible`. I think the reason is that I don't override the structure itself. – folibis Aug 31 '16 at 06:38
  • I'm sorry, I misunderstood your intent the first time. You should probably add accessors in MyClass, like this: `public int SomeValue { get { return MyStruct.someValue1; } protected set { MyStruct.someValue1 = value; } }` If this is what works for you, I will edit my answer. – Jakub Jankowski Aug 31 '16 at 06:41
  • Yes that works but my idea was to group some set of properties in structure. In my real project I have tens of them. – folibis Aug 31 '16 at 06:47
0

You can do that :

class MyClass
{
    internal struct MyStruct_t
    {
        public readonly int someValue1;
        public readonly int someValue2;
        public MyStruct_t(int i, int j)
        {
            someValue1 = i;
            someValue2 = j;
        }
    }
    public MyStruct_t MyStruct;
    public void InitStructure()
    {
        MyStruct = new MyStruct_t(1, 1);
    }
}

But you can't change someValueX from MyClass. Maybe this could help you : https://stackoverflow.com/a/14650082/6479770

Community
  • 1
  • 1
A.Pissicat
  • 3,023
  • 4
  • 38
  • 93