PC Review


Reply
Thread Tools Rate Thread

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

 
 
Michael A. Covington
Guest
Posts: n/a
 
      20th Jul 2006
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?


 
Reply With Quote
 
 
 
 
Nicholas Paldino [.NET/C# MVP]
Guest
Posts: n/a
 
      20th Jul 2006
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 Removed)

"Michael A. Covington" <(E-Mail Removed)> wrote
in message news:(E-Mail Removed)...
> 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?
>
>



 
Reply With Quote
 
Michael A. Covington
Guest
Posts: n/a
 
      20th Jul 2006
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]" <(E-Mail Removed)> wrote in
message news:(E-Mail Removed)...
> 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 Removed)
>
> "Michael A. Covington" <(E-Mail Removed)>
> wrote in message news:(E-Mail Removed)...
>> 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?
>>
>>

>
>



 
Reply With Quote
 
Michael A. Covington
Guest
Posts: n/a
 
      20th Jul 2006
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?


 
Reply With Quote
 
John B
Guest
Posts: n/a
 
      20th Jul 2006
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
 
Reply With Quote
 
Chris Dunaway
Guest
Posts: n/a
 
      20th Jul 2006
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.

 
Reply With Quote
 
Sericinus hunter
Guest
Posts: n/a
 
      20th Jul 2006
Have you thought about using struct instead of class? If I am
not mistaken structs allow <this> assignments.

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?
>
>

 
Reply With Quote
 
Michael A. Covington
Guest
Posts: n/a
 
      20th Jul 2006
"Chris Dunaway" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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.


 
Reply With Quote
 
Michael A. Covington
Guest
Posts: n/a
 
      20th Jul 2006

"Sericinus hunter" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
> 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!


 
Reply With Quote
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      20th Jul 2006
Michael A. Covington <(E-Mail Removed)> wrote:
> "Sericinus hunter" <(E-Mail Removed)> wrote in message
> news:%(E-Mail Removed)...
> > 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!


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 - <(E-Mail Removed)>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Assign Read-Only permissions to calendar for all users on one serv Don Pedro Microsoft Outlook Calendar 7 18th Mar 2008 04:16 PM
How can I make Excel read a value from a textfile and assign it to a variable in my VBA code?? haakon Microsoft Excel Programming 2 28th Feb 2007 09:21 AM
Assign a username and read permissions for Excel jeffrey.bergstedt@cox.com Microsoft Access 0 18th Sep 2006 04:04 PM
How do I assign read only access to some users and allow update rights to others =?Utf-8?B?RGF2ZSBXZWF2ZXI=?= Microsoft Access Database Table Design 1 9th Dec 2003 11:38 PM
How to assign a 'read-only' password? Anurag Windows XP Networking 1 12th Oct 2003 05:55 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 02:47 AM.