423,498 Members | 1,177 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 423,498 IT Pros & Developers. It's quick & easy.

Cannot assign to <this> because it is read-only

P: n/a
C# won't let me do something elegant.

I'm creating a class and want to give it "write to file" and "read from
file" methods. I'm actually using the XML serializer, but that's not the
problem.

The problem is that I cannot, after reading the data from a file, assign it
to 'this'. So even though I have a fine method for
foo.WriteToFile(filename), I can't implement foo.LoadFromFile(filename).

One idea is that perhaps instead of LoadFromFile, I need a constructor that
takes a filename as an argument. Is that the right approach?

Of course, I can kluge my way around it by having LoadFromFile create
*another* instance of the same class, then copy all the data, item by item,
into 'this'. But that's inelegant, even though, seen from outside, it
achieves exactly the desired effect. It's also error-prone because I might
add something to the class and forget to copy it.

Ideas?
Jul 20 '06 #1
Share this Question
Share on Google+
9 Replies


P: n/a
Michael,

Why not just take the code that is called in the constructor, refactor
it out to another method, and then have the LoadFromFile method call that
same code?

The only extra code you would have to add would be the code to
initialize the fields to the state they would be when you "new" up the
object (the constructor is called).

Other than that, it's a simple case of having to refactor the code.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Michael A. Covington" <lo**@www.covingtoninnovations.com.for.addresswrot e
in message news:u5**************@TK2MSFTNGP03.phx.gbl...
C# won't let me do something elegant.

I'm creating a class and want to give it "write to file" and "read from
file" methods. I'm actually using the XML serializer, but that's not the
problem.

The problem is that I cannot, after reading the data from a file, assign
it to 'this'. So even though I have a fine method for
foo.WriteToFile(filename), I can't implement foo.LoadFromFile(filename).

One idea is that perhaps instead of LoadFromFile, I need a constructor
that takes a filename as an argument. Is that the right approach?

Of course, I can kluge my way around it by having LoadFromFile create
*another* instance of the same class, then copy all the data, item by
item, into 'this'. But that's inelegant, even though, seen from outside,
it achieves exactly the desired effect. It's also error-prone because I
might add something to the class and forget to copy it.

Ideas?


Jul 20 '06 #2

P: n/a
Um... I'm not sure I understand. How would the LoadFromFile method perform
an assignment to 'this' which is read-only?

I am using the XML deserializer, so I don't have any code of my own that
reads in the fields and sets them one by one.

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.comwrote in
message news:e6**************@TK2MSFTNGP03.phx.gbl...
Michael,

Why not just take the code that is called in the constructor, refactor
it out to another method, and then have the LoadFromFile method call that
same code?

The only extra code you would have to add would be the code to
initialize the fields to the state they would be when you "new" up the
object (the constructor is called).

Other than that, it's a simple case of having to refactor the code.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Michael A. Covington" <lo**@www.covingtoninnovations.com.for.address>
wrote in message news:u5**************@TK2MSFTNGP03.phx.gbl...
>C# won't let me do something elegant.

I'm creating a class and want to give it "write to file" and "read from
file" methods. I'm actually using the XML serializer, but that's not the
problem.

The problem is that I cannot, after reading the data from a file, assign
it to 'this'. So even though I have a fine method for
foo.WriteToFile(filename), I can't implement foo.LoadFromFile(filename).

One idea is that perhaps instead of LoadFromFile, I need a constructor
that takes a filename as an argument. Is that the right approach?

Of course, I can kluge my way around it by having LoadFromFile create
*another* instance of the same class, then copy all the data, item by
item, into 'this'. But that's inelegant, even though, seen from outside,
it achieves exactly the desired effect. It's also error-prone because I
might add something to the class and forget to copy it.

Ideas?



Jul 20 '06 #3

P: n/a
Let me elaborate a little. I'm starting to think LoadFromFile should be a
static method (since it creates something that does not already exist)
whereas WriteToFile can be an instance method.

The following is elegant and unproblematic:
MyType myobj = new MyType();
....
do some computation to fill in values in it
....
myobj.WriteToFile(@"C:\temp\blah.xml");
I was wanting to be able to do the opposite in the following way:

MyType myobj = new MyType();
myobj.LoadFromFile(@"C:\blah\blither.xml");

But that would require the LoadFromFile method to assign to 'this', which it
can't do.
If I used a static method, the syntax would be this:

MyType myobj = MyType.LoadFromFile(@"C:\blah\blither.xml");

which is more concise.
What direction should I be heading in, here?
Jul 20 '06 #4

P: n/a
Michael A. Covington wrote:
Let me elaborate a little. I'm starting to think LoadFromFile should be a
static method (since it creates something that does not already exist)
whereas WriteToFile can be an instance method.

The following is elegant and unproblematic:
MyType myobj = new MyType();
...
do some computation to fill in values in it
...
myobj.WriteToFile(@"C:\temp\blah.xml");
I was wanting to be able to do the opposite in the following way:

MyType myobj = new MyType();
myobj.LoadFromFile(@"C:\blah\blither.xml");

But that would require the LoadFromFile method to assign to 'this', which it
can't do.
If I used a static method, the syntax would be this:

MyType myobj = MyType.LoadFromFile(@"C:\blah\blither.xml");

which is more concise.
What direction should I be heading in, here?

I say: public static MyType LoadFromFile(...

Quite a commonly used "pattern".

JB
Jul 20 '06 #5

P: n/a
Michael A. Covington wrote:
myobj.WriteToFile(@"C:\temp\blah.xml");

I was wanting to be able to do the opposite in the following way:

MyType myobj = new MyType();
myobj.LoadFromFile(@"C:\blah\blither.xml");
To me this just adds a wasted step. You create a NEW MyType object and
then throw it away by trying to deserialize the object and assign it to
this.

I think static WriteToFile and LoadFromFile methods is the most elegant
solution.

Jul 20 '06 #6

P: n/a
Have you thought about using struct instead of class? If I am
not mistaken structs allow <thisassignments.

Michael A. Covington wrote:
Let me elaborate a little. I'm starting to think LoadFromFile should be a
static method (since it creates something that does not already exist)
whereas WriteToFile can be an instance method.

The following is elegant and unproblematic:
MyType myobj = new MyType();
...
do some computation to fill in values in it
...
myobj.WriteToFile(@"C:\temp\blah.xml");
I was wanting to be able to do the opposite in the following way:

MyType myobj = new MyType();
myobj.LoadFromFile(@"C:\blah\blither.xml");

But that would require the LoadFromFile method to assign to 'this', which it
can't do.
If I used a static method, the syntax would be this:

MyType myobj = MyType.LoadFromFile(@"C:\blah\blither.xml");

which is more concise.
What direction should I be heading in, here?

Jul 20 '06 #7

P: n/a
"Chris Dunaway" <du******@gmail.comwrote in message
news:11**********************@h48g2000cwc.googlegr oups.com...
Michael A. Covington wrote:
>myobj.WriteToFile(@"C:\temp\blah.xml");

I was wanting to be able to do the opposite in the following way:

MyType myobj = new MyType();
myobj.LoadFromFile(@"C:\blah\blither.xml");

To me this just adds a wasted step. You create a NEW MyType object and
then throw it away by trying to deserialize the object and assign it to
this.

I think static WriteToFile and LoadFromFile methods is the most elegant
solution.
After thinking about it overnight, I agree. 'this' is readonly because it
refers to an object that already exists, and of course you can't replace it
with another object right in the middle of things. So it makes sense for
LoadFromFile (creating a new object) to be static and WriteToFile (acting on
an object that already exists) to be an instance method.
Jul 20 '06 #8

P: n/a

"Sericinus hunter" <se*****@flash.netwrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...
Have you thought about using struct instead of class? If I am
not mistaken structs allow <thisassignments.
I should try that. It could easily be a struct. Thanks!
Jul 20 '06 #9

P: n/a
Michael A. Covington <lo**@ai.uga.edu.for.addresswrote:
"Sericinus hunter" <se*****@flash.netwrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...
Have you thought about using struct instead of class? If I am
not mistaken structs allow <thisassignments.

I should try that. It could easily be a struct. Thanks!
That's a really bad idea, IMO. Of all the reasons to use a struct
instead of a class, being able to assign to "this" is one of the worst.

Using a static method is exactly the right approach.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jul 20 '06 #10

This discussion thread is closed

Replies have been disabled for this discussion.