Note that there are some explanatory texts on larger screens.

plurals
  1. POC#: Defining fields as private but modifiable within generics
    primarykey
    data
    text
    <p>Is there a way to define a field as <code>private</code> but still modifiable within a method using the generic of the containing class?</p> <p>(Apologies for the length of the question; I am struggling to nail down exactly what I am asking)</p> <p>Specifically, I currently have three classes <code>A</code>, <code>B</code>, <code>C</code> with the following requirements:</p> <ol> <li><code>B</code> and <code>C</code> have a field called <code>name</code>, but <code>A</code> does not</li> <li><code>A</code> and <code>C</code> have a method <code>Load()</code>, but <code>B</code> does not</li> <li>Collections of <code>B</code> and <code>C</code> use <code>name</code> in a custom <code>Add()</code> method, but <code>A</code> does not use this method</li> </ol> <p>This is my current solution:</p> <pre><code>public class BaseClass { public SomeType Load() { ... } } public interface IClass { string Name {get; set;} } public class MyList : SomeCollection&lt;IClass&gt; { // Depends on x.Name, and may modify Name before adding x to the collection public new void Add(IClass x) { ... } } public class A : BaseClass { ... } public class B : IClass { ... } public class C : BaseClass, IClass { ... } </code></pre> <p>This solution works for me if <code>Name</code> has a public <code>set</code>. But is there some way to properly encapsulate <code>x.Name</code> so that <code>Add</code> can modify <code>x.Name</code>, but <code>x.Name</code> cannot be modified in instances of <code>B</code> and <code>C</code>?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload