473,326 Members | 2,805 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,326 software developers and data experts.

Constructor good for loading file contents?

This may be a question of taste, but if there is a best practice, I want
to know. I have two choises:

SomeClass s = new SomeClass(file); // Load the file

or

SomeClass s = new SomeClass();
s.Load(file); // Load the file

The object 's' is constructed from contents of a file (or maybe a
stream). Is the contructor the natural place for reading file/stream
contents into an object, or is it better done separately, in a method?

I can think of one case when there's a need for a separate Load()
method. It's when you need to change properties of the object before
feeding data into it, like in:

XmlDocument x = new XmlDocument();
x.EntityResolver = null;
x.Load(file);

--
Gustaf
Nov 16 '05 #1
4 3337
I guess it has to do more with what a class does. If your object manipulates
the same file through its existence, then put it in the constructor. After
all, constructor is where you "construct" your object -- where you
initialize the member variables, etc.
--
Manohar Kamath
Editor, .netWire
www.dotnetwire.com
"Gustaf Liljegren" <gu*****@algonet.se> wrote in message
news:uU**************@TK2MSFTNGP11.phx.gbl...
This may be a question of taste, but if there is a best practice, I want
to know. I have two choises:

SomeClass s = new SomeClass(file); // Load the file

or

SomeClass s = new SomeClass();
s.Load(file); // Load the file

The object 's' is constructed from contents of a file (or maybe a
stream). Is the contructor the natural place for reading file/stream
contents into an object, or is it better done separately, in a method?

I can think of one case when there's a need for a separate Load()
method. It's when you need to change properties of the object before
feeding data into it, like in:

XmlDocument x = new XmlDocument();
x.EntityResolver = null;
x.Load(file);

--
Gustaf

Nov 16 '05 #2
"Gustaf Liljegren" <gu*****@algonet.se> wrote:
SomeClass s = new SomeClass(file); // Load the file
or
SomeClass s = new SomeClass();
s.Load(file); // Load the file


If your object is meaningless without a file loaded into it, make the
filename a parameter to the constructor. If you could feasibly do
something with the object *without* it having a file loaded, just give
it the Load method. (Of course, you could still have the extra
constructor for anyone who wants to open a file automatically.)

P.
Nov 16 '05 #3
Gustaf Liljegren wrote:
SomeClass s = new SomeClass(file); // Load the file
or
SomeClass s = new SomeClass();
As Paul explained, the choice of whether to include a no-argument
constructor depends on whether meaningful operations can be done on an
object constructed in such a manner. If every method throws an exception
until the Load call is made, for example, it's better to make it a
constructor so these errors cannot occur.
I can think of one case when there's a need for a separate Load()
method. It's when you need to change properties of the object before
feeding data into it, like in:

XmlDocument x = new XmlDocument();
x.EntityResolver = null;
x.Load(file);


Actually, in this case it may be better to supply a set of initial
properties to the constructor, in addition to the filename. You can wrap
these in an object which had the default values by default:

XmlDocumentProperties p = new XmlDocumentProperties();
p.EntityResolver = null;
XmlDocument x = new XmlDocument(file, p);

Now you can use custom properties as well as prevent the document from
being used without a file being specified.
--
Derrick Coetzee, Microsoft Speech Server developer
This posting is provided "AS IS" with no warranties, and confers no
rights. Use of included code samples are subject to the terms
specified at http://www.microsoft.com/info/cpyright.htm
Nov 16 '05 #4
Everyone has a different style.

I prefer to not perform any actions that might throw an exception in a ctor.
I use a Create/Load design (a two phase construction), where all the ctor
does is set state fields to known values, and a separate method performs the
operation. This makes cleanup logic much easier to implement and avoids side
effects. For example, if a static ctor throws an exception it renders that
type unusable for the life of the appdomain.

It also avoids ctors that take a l-o-n-g time to execute (which can create
its own set of problems). For example, what if the file is located on a
network where you are having intermittent connection problems? I prefer to
minimize my headaches by isolating operations. I pass arguments to ctors
when these values become part of the intial state of the object, not because
they force it to start an operation.

I recognize that many other people prefer doing it the other way...aint
choice grand.
"Gustaf Liljegren" <gu*****@algonet.se> wrote in message
news:uU**************@TK2MSFTNGP11.phx.gbl...
This may be a question of taste, but if there is a best practice, I want
to know. I have two choises:

SomeClass s = new SomeClass(file); // Load the file

or

SomeClass s = new SomeClass();
s.Load(file); // Load the file

The object 's' is constructed from contents of a file (or maybe a stream).
Is the contructor the natural place for reading file/stream contents into
an object, or is it better done separately, in a method?

I can think of one case when there's a need for a separate Load() method.
It's when you need to change properties of the object before feeding data
into it, like in:

XmlDocument x = new XmlDocument();
x.EntityResolver = null;
x.Load(file);

--
Gustaf

Nov 16 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Kristjan Sander | last post by:
Hello I am trying to write a #C windows desktop application that loads an ascx file contents (user control) from file, initiates it and then registers to database everything it finds. I tried that...
1
by: Chris K | last post by:
I am relatively new to C++ and hope that this question is relevant. I have spent some time at the local library and some time on dejanews, but have no decided to go ahead with my question, since...
8
by: RonHiler | last post by:
My copy constructor is crashing my program, and I can't figure out why. I'll try to make the code listing as short as I can. Here are the two headers: class BreakthroughClass { public:...
18
by: Chad A. Beckner | last post by:
I am (and have been trying!) to find a good way to do the following few things: 1. When a user requests a .aspx/.htm/.html file, the file needs to be automatically integrated into a page...
7
by: Anbu | last post by:
Hi, I want to load contents of a remote HTML file and to display it on the ASP ..NET Web Page. The remote HTML file is not under any virtual folder and the content & location may vary for each...
13
by: salad | last post by:
Hi Guys: I was stuck. I needed to send a report to a file. My beautiful report(s) in Access were going to require loss of formatting with RTFs, a PITA in WordMailMerge, sending it as a text...
11
by: fourfires.d | last post by:
Dear All, I am new to c++ and when write below code that try to call copy constructor in "=" operator overloading, it can not compile. Can anyone point out for me the reason? thanks !! ...
2
by: nick.taylor | last post by:
Hi, I've been trying for weeks to figure out this problem. I'm developing a simple Javascript app that loads an XML file from a server, parses the contents, and displays them. But I am...
74
by: Zytan | last post by:
I have a struct constructor to initialize all of my private (or public readonly) fields. There still exists the default constructor that sets them all to zero. Is there a way to remove the...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
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...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
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...

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.