Re: initonly array member as an lvalue

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance




"Mark Salsbery [MVP]" <MarkSalsbery[MVP]@newsgroup.nospam> wrote in message
news:1A6D5254-8C82-4E3E-B1E1-DF8FD85D2F3B@xxxxxxxxxxxxxxxx
Right. I see what you mean, but from the readonly (C#) docs:

"...assignments to the fields introduced by the declaration can only occur
as part of the declaration or in a constructor in the same class."

He isn't assigning to the field, he's performing member access on the object
referenced by the field.

It's like having

class C
{
bool* const p; // p must be assigned in constructor, p[0] can be
assigned anywhere
};


The workaround would be:

ref class C
{
initonly array<bool>^ b;

C() : b(gcnew array<bool>(8)) {}
void doit() { /* b[0] = false; */ array<bool>^ bprime; bprime[0] =
false; }
};


and

"The readonly keyword is different from the const keyword. A const field
can only be initialized at the declaration of the field. A readonly field
can be initialized either at the declaration or in a constructor.
Therefore, readonly fields can have different values depending on the
constructor used."

That reads to me just like initonly and const in C++. That's why I wonder
if C# should allow you to change readonly variables later.

It doesn't read like it should just apply to the reference.

Mark

--
Mark Salsbery
Microsoft MVP - Visual C++


"lee.crabtree" <lee.crabtree@xxxxxxxxx> wrote in message
news:5334ee6e-bb71-4f58-af3b-a58f9fafd627@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
I think it's more likely that the association of 'initonly' to
'readonly' is erroneous in some regard. As far as I can tell,
'readonly' is only supposed to prevent the REFERENCE from being
overwritten. In that sense, a List<> marked 'readonly' will allow
adding, removing, and clearing from the list, but setting the list to
null or trying to re-new it would be an error. In that sense, it's
similar to what I remember (from my long-past days of C++) a 'const'
pointer being used for. The VALUE is changeable, it's the REFERENCE
that isn't.



.



Relevant Pages

  • To vall a copy constructor by value
    ... Test::Test// An INCORRECT declaration of a copy ... It should be declared in this way by using call by reference ... Test::Test// A correct declaration of a copy konstructor ...
    (alt.comp.lang.learn.c-cpp)
  • Re: initonly array member as an lvalue
    ... "...assignments to the fields introduced by the declaration can only occur as part of the declaration or in a constructor in the same class." ... A const field can only be initialized at the declaration of the field. ... A readonly field can be initialized either at the declaration or in a constructor. ... It doesn't read like it should just apply to the reference. ...
    (microsoft.public.dotnet.languages.vc)
  • Re: const or readonly
    ... readonly can be set to a value in the constructor. ... Const cannot - its value ... must be provided inline with the declaration. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: How to pass information, classes between forms in Windows Application mode
    ... constructor when the child form is created--btw this is common in OOP ... There's not really a "Console mode" beyond whether or not a console is ... Form1, that you suggested, I am getting "data binding" between ... declaration public Class0 myClass0; even though the nested class is ...
    (microsoft.public.dotnet.languages.csharp)
  • specialized constructor arg signitaure for const object
    ... is enforced at compile time to be used only when constructing a const ... The constructor argument signature could be different depening ... on whether a const object is being constructed or not. ... the class designer to require that a const reference be passed in when a ...
    (comp.lang.cpp)