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

restrict field access and force property access

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
Jun 10 '07 #1
4 3438

"sklett" <s@s.comwrote in message
news:O3**************@TK2MSFTNGP04.phx.gbl...
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

Jun 10 '07 #2
"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 [C++ MVP]" <rb*@nospam.nospamwrote in message
news:40**********************************@microsof t.com...
>
"sklett" <s@s.comwrote in message
news:O3**************@TK2MSFTNGP04.phx.gbl...
>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


Jun 10 '07 #3

"sklett" <s@s.comwrote in message
news:ur**************@TK2MSFTNGP04.phx.gbl...
"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 [C++ MVP]" <rb*@nospam.nospamwrote in message
news:40**********************************@microsof t.com...
>>
"sklett" <s@s.comwrote in message
news:O3**************@TK2MSFTNGP04.phx.gbl...
>>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


Jun 10 '07 #4

"Ben Voigt [C++ MVP]" <rb*@nospam.nospamwrote in message
news:ed**************@TK2MSFTNGP02.phx.gbl...
>
"sklett" <s@s.comwrote in message
news:ur**************@TK2MSFTNGP04.phx.gbl...
>"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.
Yes, right you are! ;0)

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

"Ben Voigt [C++ MVP]" <rb*@nospam.nospamwrote in message
news:40**********************************@microso ft.com...
>>>
"sklett" <s@s.comwrote in message
news:O3**************@TK2MSFTNGP04.phx.gbl...
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


Jun 10 '07 #5

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

Similar topics

5
by: OneDay | last post by:
I've got a field that has some old data with text in it, but all forward data will be a 3 digit number. But many of the numbers are still only 2 digits. I would like to force the leading zero in...
18
by: Dixie | last post by:
Can I set the Format property in a date/time field in code? Can I set the Input Mask in a date/time field in code? Can I set the Format of a Yes/No field to Checkbox in code? I am working on...
4
by: Neil Coleclough | last post by:
I am constructing a database to process product returns for my Company. I have a number of toggle buttons to identify the stage to which each return has been processed. For example, clicking the...
5
by: toddles666 | last post by:
Hi- Is there any way of restricting access to a database by application & account? For example, I only want the application APP1 to access the database using the USER1 account. I've tried to...
2
by: den 2005 | last post by:
Hi everybody, How do restrict entering these characters <>\"%';()& and telling user these caharcters are not allowed to be enter in the textbox field using RegularExpression Validator? I put in...
8
by: sara | last post by:
I have a table where a few of the users entered vendor names ALL IN UPPER CASE. I have created forms to edit the data, but I can't seem to allow changing JOE SMITH to Joe Smith. What to I have...
9
by: Ecohouse | last post by:
I have a main form with two subforms. The first subform has the child link to the main form identity key. subform1 - Master Field: SK Child Field: TrainingMasterSK The second subform has a...
9
by: Tom_F | last post by:
To comp.databases.ms-access -- I just discovered, to my more than mild dismay, that some tables in my Microsoft Access 2003 database have duplicate numbers in the "AutoNumber" field. (Field...
5
by: Dakrat | last post by:
Allow me to preface this post by saying that this is my first database project, and while I have learned a lot, any concepts I have learned are hit and miss as I have found new requirements and...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
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...
1
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...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
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
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.