From: nicol on
Hi

Error 1 Property or indexer 'System.Exception.Message' cannot be
assigned to -- it is read only

using System;

namespace ConsoleApplication126
{
class Program
{
static void Main(string[] args)
{
string x = null;
try
{
if (x == null)
{
Exception ex = new Exception();
ex.Message = "x was null"; // ****** error**
throw ex;
}
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
}

}

}
From: Alberto Poblacion on
"nicol" <nicol.young20(a)gmail.com> wrote in message
news:4f314dd6-1296-40a9-97b0-2e39a3214605(a)o8g2000yqo.googlegroups.com...
> Error 1 Property or indexer 'System.Exception.Message' cannot be
> assigned to -- it is read only
> Exception ex = new Exception();
> ex.Message = "x was null"; // ****** error**

You can pass the message in the constructor:

Exception ex = new Exception("x was null");


From: nicol on
On May 6, 12:39 pm, "Alberto Poblacion" <earthling-
quitaestoparacontes...(a)poblacion.org> wrote:
> "nicol" <nicol.youn...(a)gmail.com> wrote in message
>
> news:4f314dd6-1296-40a9-97b0-2e39a3214605(a)o8g2000yqo.googlegroups.com...
>
> > Error 1 Property or indexer 'System.Exception.Message' cannot be
> > assigned to -- it is read only
> >                    Exception ex = new Exception();
> >                    ex.Message = "x was null"; // ****** error**
>
>     You can pass the message in the constructor:
>
>     Exception ex = new Exception("x was null");

thanks it work . . . but could not code it in another way ? ? ?
From: Family Tree Mike on
On 5/6/2010 7:13 AM, nicol wrote:
> On May 6, 12:39 pm, "Alberto Poblacion"<earthling-
> quitaestoparacontes...(a)poblacion.org> wrote:
>> "nicol"<nicol.youn...(a)gmail.com> wrote in message
>>
>> news:4f314dd6-1296-40a9-97b0-2e39a3214605(a)o8g2000yqo.googlegroups.com...
>>
>>> Error 1 Property or indexer 'System.Exception.Message' cannot be
>>> assigned to -- it is read only
>>> Exception ex = new Exception();
>>> ex.Message = "x was null"; // ****** error**
>>
>> You can pass the message in the constructor:
>>
>> Exception ex = new Exception("x was null");
>
> thanks it work . . . but could not code it in another way ? ? ?

No, some objects have readonly properties such as this. Only the .Net
developers would know the decision behind doing this for Exception.Message.

--
Mike
From: Harlan Messinger on
Family Tree Mike wrote:
> On 5/6/2010 7:13 AM, nicol wrote:
>> On May 6, 12:39 pm, "Alberto Poblacion"<earthling-
>> quitaestoparacontes...(a)poblacion.org> wrote:
>>> "nicol"<nicol.youn...(a)gmail.com> wrote in message
>>>
>>> news:4f314dd6-1296-40a9-97b0-2e39a3214605(a)o8g2000yqo.googlegroups.com...
>>>
>>>> Error 1 Property or indexer 'System.Exception.Message' cannot be
>>>> assigned to -- it is read only
>>>> Exception ex = new Exception();
>>>> ex.Message = "x was null"; // ****** error**
>>>
>>> You can pass the message in the constructor:
>>>
>>> Exception ex = new Exception("x was null");
>>
>> thanks it work . . . but could not code it in another way ? ? ?
>
> No, some objects have readonly properties such as this. Only the .Net
> developers would know the decision behind doing this for Exception.Message.

My guess is that it's to keep handlers in the middle of the call stack
from tampering with exceptions and then rethrowing them.