Click here to Skip to main content
15,889,335 members
Home / Discussions / C#
   

C#

 
AnswerRe: Coding Challenge Pin
Richard Deeming29-Aug-17 2:28
mveRichard Deeming29-Aug-17 2:28 
GeneralRe: Coding Challenge Pin
Mycroft Holmes4-Sep-17 21:06
professionalMycroft Holmes4-Sep-17 21:06 
SuggestionRe: Coding Challenge Pin
Richard Deeming5-Sep-17 1:55
mveRichard Deeming5-Sep-17 1:55 
GeneralRe: Coding Challenge Pin
Mycroft Holmes4-Sep-17 22:24
professionalMycroft Holmes4-Sep-17 22:24 
GeneralRe: Coding Challenge Pin
Pete O'Hanlon4-Sep-17 23:24
mvePete O'Hanlon4-Sep-17 23:24 
GeneralRe: Coding Challenge Pin
Richard Deeming5-Sep-17 2:04
mveRichard Deeming5-Sep-17 2:04 
AnswerRe: Coding Challenge Pin
Pete O'Hanlon29-Aug-17 3:53
mvePete O'Hanlon29-Aug-17 3:53 
Questionc++ const in c# (I assume, once again) Pin
User 1106097925-Aug-17 8:04
User 1106097925-Aug-17 8:04 
Dear experts
I am somewhat "c++ corrupted". With this background I'm looking for a replacement for c++ const stuff in c#.

I don't look for a solution. First and foremost, this question I ask to get an assessment of the class design from you.

What I need/I try to solve:
I have a class Spectrum (basically a vector of doubles) for which I need full access for some tasks of the app(means manipulate each value of the vector) while other tasks should only have the possibility to read the values.
C#
public class Spectrum
{
   private readonly List _values;
   public double this[int lambda]
   {
      get
      {
         return _values[lambda];
      }
      set
      {
          _values[lambda] = value;
      }
   }
}

[Edit]
The target is to protect some data from unintentional/Programme mistaken modifications
[/Edit]


Approach I.
For a first approach I thought I introduce an interface IConstSpectrum which hides the setter and will be used where I like to return a read only Spectrum.
But like in c++ one can cast the interface and have afterwards full access (in c++ this means a dirty const_cast)

Approach II.
a.) I introduce a class ConstSpectrum ...
C#
public class ConstSpectrum
{
   protected readonly List _values;

   public double this[int lambda]
   {
       get
       {
           return _values[lambda];
       }
   }
}
b.) ... and derive from this the full accessible spectrum
C#
public class Spectrum: ConstSpectrum
    {
        public double this[int lambda]
        {
            set
            {
                _values[lambda] = value;
            }
        }
    }
Conclusion Approach II:
Depending on the task , I would use ConstSpectrum or Spectrum. And I'm Aware returning a Spectrum as ConstSpectrum opens the door to cast again.

Questions, resp, how do you see this:
- What do you think about this design? Is it overkill?
- Is there a better or maybe more easy solution?

Thank you very much in advance.
Bruno

modified 19-Jan-21 21:04pm.

AnswerRe: c++ const in c# (I assume, once again) Pin
OriginalGriff25-Aug-17 8:37
mveOriginalGriff25-Aug-17 8:37 
GeneralRe: c++ const in c# (I assume, once again) Pin
User 1106097925-Aug-17 8:43
User 1106097925-Aug-17 8:43 
GeneralRe: c++ const in c# (I assume, once again) Pin
User 1106097925-Aug-17 9:01
User 1106097925-Aug-17 9:01 
GeneralRe: c++ const in c# (I assume, once again) Pin
harold aptroot25-Aug-17 8:41
harold aptroot25-Aug-17 8:41 
GeneralRe: c++ const in c# (I assume, once again) Pin
User 1106097925-Aug-17 8:47
User 1106097925-Aug-17 8:47 
AnswerRe: c++ const in c# (I assume, once again) Pin
jschell25-Aug-17 9:40
jschell25-Aug-17 9:40 
GeneralRe: c++ const in c# (I assume, once again) Pin
User 1106097925-Aug-17 9:50
User 1106097925-Aug-17 9:50 
AnswerRe: c++ const in c# (I assume, once again) Pin
Pete O'Hanlon25-Aug-17 12:19
mvePete O'Hanlon25-Aug-17 12:19 
GeneralRe: c++ const in c# (I assume, once again) Pin
User 1106097925-Aug-17 12:47
User 1106097925-Aug-17 12:47 
AnswerRe: c++ const in c# (I assume, once again) Pin
Gerry Schmitz26-Aug-17 8:54
mveGerry Schmitz26-Aug-17 8:54 
AnswerRe: c++ const in c# (I assume, once again) Pin
Bernhard Hiller28-Aug-17 3:39
Bernhard Hiller28-Aug-17 3:39 
GeneralRe: c++ const in c# (I assume, once again) Pin
User 1106097928-Aug-17 3:54
User 1106097928-Aug-17 3:54 
QuestionCreate .NET COM dll as existing C++ COM Pin
MrKBA24-Aug-17 22:38
MrKBA24-Aug-17 22:38 
AnswerRe: Create .NET COM dll as existing C++ COM Pin
Pete O'Hanlon24-Aug-17 22:44
mvePete O'Hanlon24-Aug-17 22:44 
GeneralRe: Create .NET COM dll as existing C++ COM Pin
MrKBA25-Aug-17 0:41
MrKBA25-Aug-17 0:41 
GeneralRe: Create .NET COM dll as existing C++ COM Pin
Pete O'Hanlon25-Aug-17 0:57
mvePete O'Hanlon25-Aug-17 0:57 
GeneralRe: Create .NET COM dll as existing C++ COM Pin
User 1106097925-Aug-17 9:25
User 1106097925-Aug-17 9:25 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.