-3

I have a requirement to define properties with private set. Is it possible for me to set this property in a different class (in the same .cs file)? I'm having trouble accessing the property outside of the class.

E.g.

public class ClassA
{
    public string ID { get; private set; }
}

public class ClassB
{
    // Set ID here
}
Backs
  • 24,430
  • 5
  • 58
  • 85
J. Patwary
  • 427
  • 1
  • 7
  • 22

3 Answers3

1

Private set is equivalent to the below code.

private int myProperty;
public int MyProperty
{
    get { return myProperty; }
}

So you cannot set the property outside the class. You can set only in the nested class and derived class.

Though you can use the reflection also.

Reference: https://stackoverflow.com/a/1565766/6527049

Vivek Nuna
  • 25,472
  • 25
  • 109
  • 197
  • 3
    It is not _quite_ equivalent to that code. Since with a private setter the class itself can set `this.MyProperty` (which your 'equivalent' code won't allow). – mjwills Jul 20 '20 at 02:33
  • @John I clearly mentioned that you cannt sey outside the class, it implicitly means the same that you can set within the same class. Example is just to show the meaning of private set to help OP to understand the concept – Vivek Nuna Jul 20 '20 at 02:40
  • private set is NOT equivalent to no setter..... a no setter can only be set from the constructor..... while a private setter can be set from anywhere inside the containing class. So this is answer is all kinds of wrong. – Jonathan Alfaro Jul 20 '20 at 02:57
  • 2
    @JonathanAlfaro My point was https://dotnetfiddle.net/RzTcxF (lacking a setter, as shown in **this answer**) doesn't compile. If you are instead referring to https://dotnetfiddle.net/ZoV7YZ (i.e. auto-implemented properties, which is different to this answer) then yes you are correct. – mjwills Jul 20 '20 at 03:36
0

I believe you are looking for the C++ concept of friend class where given two classes A and B , defining B as friend of A gives B complete access to A , and other friend classes if any. This concept is not supported in C#.

Bercovici Adrian
  • 8,794
  • 17
  • 73
  • 152
-1

The only way to do it is to create a method in ClassA which sets the property

public class ClassA
{
    public string ID { get; private set; } = "default id";

    public void SetID(string id) 
    {
        ID = id;
    }
}
public class ClassB
{
    private ClassA _classA = new ClassA();

    public ClassB() 
    {
        Debug.WriteLine(_classA.ID);
        _classA.SetID("some id");
        Debug.WriteLine(_classA.ID);
    }
}

Then call that method from ClassB.

This approach can be used if you want to perform some form or shape of validation or perhaps adding dynamically constructed values to the property.

You could also use a "smart property" in those cases but then again that is a controversial topic...

And sometimes you are just not allowed to change the interface of a class and then you have to find some approach.

As mentioned in the comments this defeats the purpose of the private setter.

But then again there might be some scenarios in which you want to use this approach.

In any case this answers the question.

Jonathan Alfaro
  • 4,013
  • 3
  • 29
  • 32
  • 1
    This is pointless. It defeats the purpose of making it `private set`. – mjwills Jul 20 '20 at 02:49
  • @JonathanAlfaro not an answer at all – Vivek Nuna Jul 20 '20 at 02:52
  • @viveknuna it actually answers the question which is to find a way to set the property from another class. – Jonathan Alfaro Jul 20 '20 at 02:54
  • @mjwills what if you want to set a dynamic value like appending a suffix or prefix? What if you want to do validation? So it is not "pointless". Of course you could use a "smart property" for that but then again that is a controversial topic and some people do not like "smart properties" – Jonathan Alfaro Jul 20 '20 at 03:12
  • 1
    The whole point of setters in C# is to set a property. `SetID` is a Java way of setting an ID, not a C# way. This answer is like answering - "how do I get into the front door without unlocking it?" with "install a second unlocked door right next to the front door". I mean, sure, it works. But it is weird and not idiomatic and defeats the purpose of the lock in the first place. – mjwills Jul 20 '20 at 03:34
  • @mjwills you could do validation on a setter or dynamic value generation.... But sometimes you are simply not allowed or you cannot modify an existing interface for whatever reason. So there still is a valid scenario for using a method. Plus the purpose of the property could be to expose a value to a consumer of an interface after a cast for example in which the SetPropertyID method will not be available. – Jonathan Alfaro Jul 20 '20 at 22:11