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

  • Thread starter Thread starter Michael A. Covington
  • Start date Start date
M

Michael A. Covington

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?
 
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.
 
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 said:
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]
- (e-mail address removed)

Michael A. Covington said:
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?
 
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?
 
Michael said:
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
 
Michael said:
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.
 
Have you thought about using struct instead of class? If I am
not mistaken structs allow <this> assignments.
 
Chris Dunaway said:
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.
 
Sericinus hunter said:
Have you thought about using struct instead of class? If I am
not mistaken structs allow <this> assignments.

I should try that. It could easily be a struct. Thanks!
 
Michael A. Covington said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top