restrict field access and force property access

  • Thread starter Thread starter sklett
  • Start date Start date
S

sklett

This might be a weird one ;0)

I want to lazy load some member fields in a class. I had planned on
handling this in the Property for the field, think; if(field == null) //
load field data.

I'd like to know if there is ANY way to restrict access to a field. I found
a code sample that looked like it *might* do what I want (NOTE the
AccessedThroughProperty attribute):
<code>
class SomeClass
{
[AccessedThroughProperty("GlobalSettings")]
private string _globalSettings = null;

protected string GlobalSettings
{
get
{
if(_globalSettings == null)
{
// load data
}
return _globalSettings;
}
}

public void SomeMethod()
{
string s = _globalSettings; // I would LOVE it if this would
throw a compiler error
}
}
</code>

Of course if I'm just careful and don't access the field directly everything
will be fine, but I need to do this is many classes and it's my instinct to
access fields directly if I can.

One solution would be to put the lazy loaded stuff in a base class, but this
isn't ideal and wouldn't make much sense to anyone looking at the code.

If anyone knows of any obscure attributes that might help me accomplish this
I would really appreciate it.
Thanks,
Steve
 
sklett said:
This might be a weird one ;0)

I want to lazy load some member fields in a class. I had planned on
handling this in the Property for the field, think; if(field == null) //
load field data.

I'd like to know if there is ANY way to restrict access to a field. I
found a code sample that looked like it *might* do what I want (NOTE the
AccessedThroughProperty attribute):
<code>
class SomeClass
{
[AccessedThroughProperty("GlobalSettings")]
private string _globalSettings = null;

protected string GlobalSettings
{
get
{
if(_globalSettings == null)
{
// load data
}
return _globalSettings;
}
}

public void SomeMethod()
{
string s = _globalSettings; // I would LOVE it if this would
throw a compiler error
}
}
</code>

Of course if I'm just careful and don't access the field directly
everything will be fine, but I need to do this is many classes and it's my
instinct to access fields directly if I can.

One solution would be to put the lazy loaded stuff in a base class, but
this isn't ideal and wouldn't make much sense to anyone looking at the
code.

A nested class could work as well, and might make more sense.
 
"A nested class could work as well, and might make more sense."
You're right, that would work and would make a little more sense as I could
comment it well in the context of the containing class.
Still.... I'd like to find another way if possible that didn't require an
additional abstraction of the data from it's logical layer.

Thanks for the suggestion, it's my number one option as of this moment :)

Ben Voigt said:
sklett said:
This might be a weird one ;0)

I want to lazy load some member fields in a class. I had planned on
handling this in the Property for the field, think; if(field == null) //
load field data.

I'd like to know if there is ANY way to restrict access to a field. I
found a code sample that looked like it *might* do what I want (NOTE the
AccessedThroughProperty attribute):
<code>
class SomeClass
{
[AccessedThroughProperty("GlobalSettings")]
private string _globalSettings = null;

protected string GlobalSettings
{
get
{
if(_globalSettings == null)
{
// load data
}
return _globalSettings;
}
}

public void SomeMethod()
{
string s = _globalSettings; // I would LOVE it if this would
throw a compiler error
}
}
</code>

Of course if I'm just careful and don't access the field directly
everything will be fine, but I need to do this is many classes and it's
my instinct to access fields directly if I can.

One solution would be to put the lazy loaded stuff in a base class, but
this isn't ideal and wouldn't make much sense to anyone looking at the
code.

A nested class could work as well, and might make more sense.
If anyone knows of any obscure attributes that might help me accomplish
this I would really appreciate it.
Thanks,
Steve
 
sklett said:
"A nested class could work as well, and might make more sense."
You're right, that would work and would make a little more sense as I
could comment it well in the context of the containing class.
Still.... I'd like to find another way if possible that didn't require an
additional abstraction of the data from it's logical layer.

Ahh, but a lazy initialization scheme is a detail that ought to be hidden
behind encapsulation.
Thanks for the suggestion, it's my number one option as of this moment :)

Ben Voigt said:
sklett said:
This might be a weird one ;0)

I want to lazy load some member fields in a class. I had planned on
handling this in the Property for the field, think; if(field == null) //
load field data.

I'd like to know if there is ANY way to restrict access to a field. I
found a code sample that looked like it *might* do what I want (NOTE the
AccessedThroughProperty attribute):
<code>
class SomeClass
{
[AccessedThroughProperty("GlobalSettings")]
private string _globalSettings = null;

protected string GlobalSettings
{
get
{
if(_globalSettings == null)
{
// load data
}
return _globalSettings;
}
}

public void SomeMethod()
{
string s = _globalSettings; // I would LOVE it if this would
throw a compiler error
}
}
</code>

Of course if I'm just careful and don't access the field directly
everything will be fine, but I need to do this is many classes and it's
my instinct to access fields directly if I can.

One solution would be to put the lazy loaded stuff in a base class, but
this isn't ideal and wouldn't make much sense to anyone looking at the
code.

A nested class could work as well, and might make more sense.
If anyone knows of any obscure attributes that might help me accomplish
this I would really appreciate it.
Thanks,
Steve
 
Ben Voigt said:
Ahh, but a lazy initialization scheme is a detail that ought to be hidden
behind encapsulation.

Yes, right you are! ;0)

Thanks for the suggestion, it's my number one option as of this moment :)

Ben Voigt said:
This might be a weird one ;0)

I want to lazy load some member fields in a class. I had planned on
handling this in the Property for the field, think; if(field == null)
// load field data.

I'd like to know if there is ANY way to restrict access to a field. I
found a code sample that looked like it *might* do what I want (NOTE
the AccessedThroughProperty attribute):
<code>
class SomeClass
{
[AccessedThroughProperty("GlobalSettings")]
private string _globalSettings = null;

protected string GlobalSettings
{
get
{
if(_globalSettings == null)
{
// load data
}
return _globalSettings;
}
}

public void SomeMethod()
{
string s = _globalSettings; // I would LOVE it if this would
throw a compiler error
}
}
</code>

Of course if I'm just careful and don't access the field directly
everything will be fine, but I need to do this is many classes and it's
my instinct to access fields directly if I can.

One solution would be to put the lazy loaded stuff in a base class, but
this isn't ideal and wouldn't make much sense to anyone looking at the
code.

A nested class could work as well, and might make more sense.


If anyone knows of any obscure attributes that might help me accomplish
this I would really appreciate it.
Thanks,
Steve
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top