473,382 Members | 1,404 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

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

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
9 17023
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
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
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
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
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
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
"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

"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Nachi | last post by:
Hi, Urgent Help appreciated.... I am getting resultset with first condition and when try to get the resutlset from second condition i am getting the above error in SQL200. I know that i am...
4
by: Andy Leszczynski | last post by:
watch this: http://www.turbogears.org.nyud.net:8090/docs/wiki20/20MinuteWiki.mov or read this: http://www.turbogears.org.nyud.net:8090/docs/wiki2 0/page4.html should not it be: 2 def...
6
by: berthelot samuel | last post by:
Hi everyone, This really is a tricky one ! (at least for me:). Actually I haven't found any solution to this problem anywhere... So I'll try to be as clear as possible in my explanations. First,...
0
by: Angel | last post by:
Hi I have a problem with my web applications. I believe the problem is somehow connected to HttpSession. I use form authentication in my project. Once I authenticate the user and redirect to...
4
by: David Lozzi | last post by:
Howdy, I'm using a WYSIWYG editor called TinyMCE. When I edit some text and then save it back to my SQL server using a SQLCommand, all HTML characters are changed to HTML code, i.e. &gt;strong&lt;...
6
by: saunderl | last post by:
Hello Everyone, I'm trying to just play with managed VC++. I just want to draw a box on the form when it is clicked. Here is my click event handler System::Void Test_Click(System::Object^ ...
4
by: lizhuo | last post by:
hi all: I reading "C++ Templates: The Complete Guide " Part II: Templates in Depth he write: template<typename T> class B { public: enumE{e1=6,e2=28,e3=496}; virtual void zero(E e = e1);
7
by: comp.lang.php | last post by:
<? class EditResultGenerator extends MethodGeneratorForActionPerformer { /** * Create an instance of itself if $this does not yet exist as an EditResultGenerator class object * * @access...
4
jlm699
by: jlm699 | last post by:
I've looked at the other articles about maps of maps and am still stuck on this! I'm trying to basically make an enumeration of a data monitoring app. Instead of displaying numbers for certain...
4
by: mark4asp | last post by:
I have an element, report which contains tags which have been transformed. E.g. <pis &lt;p&gt <myXml> <report>This text has html tags in it.&lt;p&gt which but <has been changed to &lt;&gt</report>...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.