473,508 Members | 2,351 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Instatiating an object in the get property method

Is it bad form to instatiate a private variable in the get method of a
property declaration?

ie)

private objA _someVar;

public objA SomeVar
{
get { if( _someVar == null) _someVar = new objA();
return _someVar;
}
}
Nov 16 '05 #1
6 1067
Jack,

No, this is not a bad idea at all. It's referred to as "lazy-loading"
and is done when the resource is expensive to create.

The only problem with this is that it is not thread safe. If this is
the case, then you will have to use a lock statement (I recommend the double
check lock pattern) to create the instance if it is not created already.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Jack Addington" <ja********@shaw.ca> wrote in message
news:OI****************@tk2msftngp13.phx.gbl...
Is it bad form to instatiate a private variable in the get method of a
property declaration?

ie)

private objA _someVar;

public objA SomeVar
{
get { if( _someVar == null) _someVar = new objA();
return _someVar;
}
}

Nov 16 '05 #2
Hi, Jack.
As long as the design is solid, I don't see any systematic problem with
this technic. For example, it could be used to perform a delayed instantiate
a singleton object.

Ming Chen

"Jack Addington" <ja********@shaw.ca> wrote in message
news:OI****************@tk2msftngp13.phx.gbl...
Is it bad form to instatiate a private variable in the get method of a
property declaration?

ie)

private objA _someVar;

public objA SomeVar
{
get { if( _someVar == null) _someVar = new objA();
return _someVar;
}
}

Nov 16 '05 #3
Nicholas Paldino [.NET/C# MVP] <mv*@spam.guard.caspershouse.com> wrote:
No, this is not a bad idea at all. It's referred to as "lazy-loading"
and is done when the resource is expensive to create.

The only problem with this is that it is not thread safe. If this is
the case, then you will have to use a lock statement (I recommend the double
check lock pattern) to create the instance if it is not created already.


The double-check lock pattern doesn't work in .NET unless the variable
is also volatile. There's no guarantee that the object will have
completed being constructed before the value in the variable is set to
the new reference, for example.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #4
Daniel Jin <Da*******@discussions.microsoft.com> wrote:
but if you are running the code on a x86 machine, double locking should be
ok, correct? though it could potentially be ported to a system with very
weak memory model.


I don't know whether it's definitely correct, even on x86. It may well
be, but I certainly wouldn't like to write a load of code assuming it.
You never know where your code will end up...

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #5
On Tue, 21 Sep 2004 18:40:03 +0100, Jon Skeet [C# MVP]
<sk***@pobox.com> wrote:
Daniel Jin <Da*******@discussions.microsoft.com> wrote:
but if you are running the code on a x86 machine, double locking should be
ok, correct? though it could potentially be ported to a system with very
weak memory model.


I don't know whether it's definitely correct, even on x86. It may well
be, but I certainly wouldn't like to write a load of code assuming it.
You never know where your code will end up...


CLI I 12.6.8 says:

"It is explicitly not a requirement that a conforming implementation
of the CLI guarantee that all state updates performed within a
constructor be uniformly visible before the constructor completes.
CIL generators may ensure this requirement themselves by inserting
appropriate calls to the memory barrier or volatile write
instructions. "

When I read this it tends to make me want to err on the safe side and
avoid the situation altogether. You never know what conforming
implementation your code might end up on...

--
Scott
http://www.OdeToCode.com
Nov 16 '05 #6
but if you are running the code on a x86 machine, double locking should be
ok, correct? though it could potentially be ported to a system with very
weak memory model.

"Jon Skeet [C# MVP]" wrote:
Nicholas Paldino [.NET/C# MVP] <mv*@spam.guard.caspershouse.com> wrote:
No, this is not a bad idea at all. It's referred to as "lazy-loading"
and is done when the resource is expensive to create.

The only problem with this is that it is not thread safe. If this is
the case, then you will have to use a lock statement (I recommend the double
check lock pattern) to create the instance if it is not created already.


The double-check lock pattern doesn't work in .NET unless the variable
is also volatile. There's no guarantee that the object will have
completed being constructed before the value in the variable is set to
the new reference, for example.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #7

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

Similar topics

54
4491
by: tshad | last post by:
I have a function: function SalaryDisplay(me) { var salaryMinLabel = document.getElementById("SalaryMin"); salaryMinLabel.value = 200; alert("after setting salaryMinLabel = " +...
5
14407
by: kuvpatel | last post by:
Hi I want to refer a class called LogEvent, and use one of its methods called WriteMessage without actually having to create an instance of Logevent. I have tried using the word sealed with...
3
2754
by: User1014 | last post by:
A global variable is really just a property of the "Global Object", so what does that make a function defined in the global context? A method of the Global Object? ...
2
2621
by: Ralph | last post by:
Hi I don't understand why it's not working: function schedule(imTop){ this.tdImagesTop = imTop; } schedule.prototype.selectEl = function() { alert(this.tdImagesTop);
0
7225
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7123
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
7042
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5627
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5052
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
4707
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3193
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1556
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
766
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.