473,385 Members | 1,356 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,385 software developers and data experts.

General Question about Object Creation

As a matter of practice, where would people put the following elements of
object creation/initialisation:

Create shared member objects
Initialise shared member objects
Create non-shared member objects
Initialise non-shared member objects
Initialise peripherals

The places where these could take place would appear to be

Constructor
Initialise method (or Load method)
.... any where else?

Is there any convention for naming an initialise method?

The scenario I have in mind is where an object communicates with some
external equipment. This object needs to load its configuration from some
xml files before it can do anything sensible, but I am not sure whether the
constructor is the right place to do this. The object does not always have
to communicate with the external equipment (it might not be attached, for
example). In this case, I do not want to insist that the user connect the
equipment and power it on just so that they can use some features of this
object. It's a bit like the WebBrowser control and mshtml (loosely). The
user can load the browser and render html, or just use the mshtml bit for
parsing and stuff.

In my case, the object can be used to control the external equipment, or
used to provide general configuration information about the equipment.

Any and all thoughts welcome.

TIA

Charles
Nov 21 '05 #1
6 1182
This is what makes sense to me:
"Charles Law" wrote:
As a matter of practice, where would people put the following elements of
object creation/initialisation:

Create shared member objects Shared constructor Initialise shared member objects Shared constructor Create non-shared member objects constructor Initialise non-shared member objects constructor Initialise peripherals Load
The places where these could take place would appear to be

Constructor
Initialise method (or Load method)
.... any where else?

Is there any convention for naming an initialise method?

The scenario I have in mind is where an object communicates with some
external equipment. This object needs to load its configuration from some
xml files before it can do anything sensible, but I am not sure whether the
constructor is the right place to do this. The object does not always have
to communicate with the external equipment (it might not be attached, for
example). In this case, I do not want to insist that the user connect the
equipment and power it on just so that they can use some features of this
object. It's a bit like the WebBrowser control and mshtml (loosely). The
user can load the browser and render html, or just use the mshtml bit for
parsing and stuff.

In my case, the object can be used to control the external equipment, or
used to provide general configuration information about the equipment.

Any and all thoughts welcome.

TIA

Charles

Nov 21 '05 #2
Hi Amiram

Thanks for the reply.

The xml files (three of them) are used to initialise shared objects, so
would you include them in the shared constructor?

The reason for my hesitation is that I usually try to avoid performing
operations in the constructor that could fail, and if one or more of the
files is missing then the constructor would fail.

Charles
"Amiram Korach" <Am**********@discussions.microsoft.com> wrote in message
news:91**********************************@microsof t.com...
This is what makes sense to me:
"Charles Law" wrote:
As a matter of practice, where would people put the following elements of
object creation/initialisation:

Create shared member objects

Shared constructor
Initialise shared member objects

Shared constructor
Create non-shared member objects

constructor
Initialise non-shared member objects

constructor
Initialise peripherals

Load

The places where these could take place would appear to be

Constructor
Initialise method (or Load method)
.... any where else?

Is there any convention for naming an initialise method?

The scenario I have in mind is where an object communicates with some
external equipment. This object needs to load its configuration from some
xml files before it can do anything sensible, but I am not sure whether
the
constructor is the right place to do this. The object does not always
have
to communicate with the external equipment (it might not be attached, for
example). In this case, I do not want to insist that the user connect the
equipment and power it on just so that they can use some features of this
object. It's a bit like the WebBrowser control and mshtml (loosely). The
user can load the browser and render html, or just use the mshtml bit for
parsing and stuff.

In my case, the object can be used to control the external equipment, or
used to provide general configuration information about the equipment.

Any and all thoughts welcome.

TIA

Charles

Nov 21 '05 #3
If you put code that initialize shared objects in a nonshared constructor,
the shared objects will be initialized each time you create an object, and
you probably don't want that to happen.
If you worried about a failure in the constructor, and the code that might
fail is not critical to the main program, put a try catch block.

"Charles Law" wrote:
Hi Amiram

Thanks for the reply.

The xml files (three of them) are used to initialise shared objects, so
would you include them in the shared constructor?

The reason for my hesitation is that I usually try to avoid performing
operations in the constructor that could fail, and if one or more of the
files is missing then the constructor would fail.

Charles
"Amiram Korach" <Am**********@discussions.microsoft.com> wrote in message
news:91**********************************@microsof t.com...
This is what makes sense to me:
"Charles Law" wrote:
As a matter of practice, where would people put the following elements of
object creation/initialisation:

Create shared member objects

Shared constructor
Initialise shared member objects

Shared constructor
Create non-shared member objects

constructor
Initialise non-shared member objects

constructor
Initialise peripherals

Load

The places where these could take place would appear to be

Constructor
Initialise method (or Load method)
.... any where else?

Is there any convention for naming an initialise method?

The scenario I have in mind is where an object communicates with some
external equipment. This object needs to load its configuration from some
xml files before it can do anything sensible, but I am not sure whether
the
constructor is the right place to do this. The object does not always
have
to communicate with the external equipment (it might not be attached, for
example). In this case, I do not want to insist that the user connect the
equipment and power it on just so that they can use some features of this
object. It's a bit like the WebBrowser control and mshtml (loosely). The
user can load the browser and render html, or just use the mshtml bit for
parsing and stuff.

In my case, the object can be used to control the external equipment, or
used to provide general configuration information about the equipment.

Any and all thoughts welcome.

TIA

Charles


Nov 21 '05 #4
It would actually be fatal if the shared constructor code failed, so I guess
I let the exception happen. The only other reason that I have put it into a
method in the past is that it takes time to execute; something else that I
try to avoid in constructors, but perhaps not a good enough reason.

Charles
"Amiram Korach" <Am**********@discussions.microsoft.com> wrote in message
news:E6**********************************@microsof t.com...
If you put code that initialize shared objects in a nonshared constructor,
the shared objects will be initialized each time you create an object, and
you probably don't want that to happen.
If you worried about a failure in the constructor, and the code that might
fail is not critical to the main program, put a try catch block.

"Charles Law" wrote:
Hi Amiram

Thanks for the reply.

The xml files (three of them) are used to initialise shared objects, so
would you include them in the shared constructor?

The reason for my hesitation is that I usually try to avoid performing
operations in the constructor that could fail, and if one or more of the
files is missing then the constructor would fail.

Charles
"Amiram Korach" <Am**********@discussions.microsoft.com> wrote in message
news:91**********************************@microsof t.com...
> This is what makes sense to me:
> "Charles Law" wrote:
>
>> As a matter of practice, where would people put the following elements
>> of
>> object creation/initialisation:
>>
>> Create shared member objects
> Shared constructor
>> Initialise shared member objects
> Shared constructor
>> Create non-shared member objects
> constructor
>> Initialise non-shared member objects
> constructor
>> Initialise peripherals
> Load
>>
>> The places where these could take place would appear to be
>>
>> Constructor
>> Initialise method (or Load method)
>> .... any where else?
>>
>> Is there any convention for naming an initialise method?
>>
>> The scenario I have in mind is where an object communicates with some
>> external equipment. This object needs to load its configuration from
>> some
>> xml files before it can do anything sensible, but I am not sure
>> whether
>> the
>> constructor is the right place to do this. The object does not always
>> have
>> to communicate with the external equipment (it might not be attached,
>> for
>> example). In this case, I do not want to insist that the user connect
>> the
>> equipment and power it on just so that they can use some features of
>> this
>> object. It's a bit like the WebBrowser control and mshtml (loosely).
>> The
>> user can load the browser and render html, or just use the mshtml bit
>> for
>> parsing and stuff.
>>
>> In my case, the object can be used to control the external equipment,
>> or
>> used to provide general configuration information about the equipment.
>>
>> Any and all thoughts welcome.
>>
>> TIA
>>
>> Charles
>>
>>
>>


Nov 21 '05 #5
In article <u2**************@TK2MSFTNGP10.phx.gbl>, Charles Law wrote:
It would actually be fatal if the shared constructor code failed, so I guess
I let the exception happen. The only other reason that I have put it into a
method in the past is that it takes time to execute; something else that I
try to avoid in constructors, but perhaps not a good enough reason.

Charles


Charles - the only thing to be warry of is that if you let an exception
be thrown form the shared constructor then the client code is going to
actually receive a TypeInitializationException. They can get at the
actual exception by looking at the InnerException property, but it is
something that you might want to document if this code is meant to be
used by developers other then your self.

--
Tom Shelton [MVP]
OS Name: Microsoft Windows XP Professional
OS Version: 5.1.2600 Service Pack 2 Build 2600
System Up Time: 7 Days, 14 Hours, 56 Minutes, 22 Seconds
Nov 21 '05 #6
Hi Tom

Thanks for the pointer. I have actually gone away from the shared
constructor idea, because I need to give my class a parameter to tell it
where to find the config files, and shared constructors can't take
parameters; unless there is another way?

I have put the file load into a standard constructor, but it does mean that
a client has to create an instance of my class in order to use shared
properties. I am not sure how to get round this.

Charles
"Tom Shelton" <to*@YOUKNOWTHEDRILLmtogden.com> wrote in message
news:Ob**************@tk2msftngp13.phx.gbl...
In article <u2**************@TK2MSFTNGP10.phx.gbl>, Charles Law wrote:
It would actually be fatal if the shared constructor code failed, so I
guess
I let the exception happen. The only other reason that I have put it into
a
method in the past is that it takes time to execute; something else that
I
try to avoid in constructors, but perhaps not a good enough reason.

Charles


Charles - the only thing to be warry of is that if you let an exception
be thrown form the shared constructor then the client code is going to
actually receive a TypeInitializationException. They can get at the
actual exception by looking at the InnerException property, but it is
something that you might want to document if this code is meant to be
used by developers other then your self.

--
Tom Shelton [MVP]
OS Name: Microsoft Windows XP Professional
OS Version: 5.1.2600 Service Pack 2 Build 2600
System Up Time: 7 Days, 14 Hours, 56 Minutes, 22 Seconds

Nov 21 '05 #7

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

Similar topics

11
by: cfchou | last post by:
hi, all, i'm reading ch.20 -smart pointers- of . and i'm tring the trule.hpp test. but there's something different than i expect, and i found that's about temp object. so i simplified the...
4
by: Mike | last post by:
Please help this is driving me nuts. I have 2 forms, 1 user class and I am trying to implement a singleton class. Form 1 should create a user object and populate some properties in user. Form2...
3
by: Tom | last post by:
I think I'm still a little rough on the principle and understanding of Marshal by value and Marshal by reference after reading various materials. my understanding of Marshal by value is that the...
8
by: Anthony Munter | last post by:
I have a web application with impersonate=”true” in Web.config and on my own logon page I allow the user to either - specify a userid/password for the app to impersonate when calling legacy...
6
by: Andy | last post by:
Someone posted this official proposal to create comp.databases.postgresql.general again. He wrote his own charter. As far as I know, he did not consult any of the postgresql groups first. There...
105
by: Christoph Zwerschke | last post by:
Sometimes I find myself stumbling over Python issues which have to do with what I perceive as a lack of orthogonality. For instance, I just wanted to use the index() method on a tuple which does...
15
by: Sam Kong | last post by:
Hello! I got recently intrigued with JavaScript's prototype-based object-orientation. However, I still don't understand the mechanism clearly. What's the difference between the following...
1
by: UJ | last post by:
I am doing development on a machine and everything was working fine. The name of the project was ECS to I made all my references as ~/ECS/... Worked great. Put it on the final server running...
7
by: RvGrah | last post by:
I'm using the Activated event of my form to run a database query in a backroundWorker, and want to dismiss the call after the first time the form is activated. Based on some excellent advice in an...
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
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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.