I am having problems understanding how to correctly encapsulate my class. It is (or should be) an inmutable class.
I am using a "helper class" and I want it to be not accesible from the outside.
namespace MyApp
{
[Flags]
public enum OctaveGroups
{
None = 0,
oneOctave = 1
}
class Frequencies
{
// HelperClass
public class Frequency
{
public string Name { get; set; }
public OctaveGroups Octave { get; set; }
}
public readonly static List<Frequency> All = new List<Frequency>
{
#region Complete Frequencies Data
new Frequency { Name = "63", Hz = 63,
Octave = OctaveGroups.oneOctave | OctaveGroups.None,
},
new Frequency { Name = "80", Hz = 80,
Octave = OctaveGroups.None,
}
// And so on..
//..
#endregion
};
public readonly List<Frequency> OneOctave = All.Where(f => f.Octave.HasFlag(OctaveGroups.oneOctave)).ToList();
public readonly List<Frequency> None = All.Where(f => f.Octave.HasFlag(OctaveGroups.None)).ToList();
}
}
If I make my Frequency
class protected
or private
I get this error:
Inconsistent accessibility: field type 'List' is less accesible than field 'Frequencies.All'
I get the same error if I make class Frequency
and List<Frequency> All
protected and try to make a method that returns a List<Frequency>
like:
public List<Frequency> GetAll()
{
return All.Where(f => f.Octave.HasFlag(OctaveGroups.OneOctave)).ToList();
}
How will be the correct way to expose just .All
.OneOctave
and .None
fields while keeping them read only?
You can't expect to hide
Frequency
when you are planning in having public methods returningList<Frequency>
.Now, what I understand is your issue is that you need accessible property setters in
Frequency
fromFrequencies
but you don't want to expose them to the outside. The way to do this is through an interface that only exposes getters:And now, you make
Frequencies.Frequency
a private nested class and you expose onlyIFrequency
:Now a consumer of
Frequencies
will only seeIFrequency
instances where no setter is exposed and is therefore immutable to the outside world (excluding reflection of course).