Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / June 2007

Tip: Looking for answers? Try searching our database.

restrict field access and force property access

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
sklett - 10 Jun 2007 02:39 GMT
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
Ben Voigt [C++ MVP] - 10 Jun 2007 03:07 GMT
> This might be a weird one ;0)
>
[quoted text clipped - 38 lines]
> 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 - 10 Jun 2007 04:30 GMT
"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 :)

>> This might be a weird one ;0)
>>
[quoted text clipped - 45 lines]
>> Thanks,
>> Steve
Ben Voigt [C++ MVP] - 10 Jun 2007 05:23 GMT
> "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 :)
>
[quoted text clipped - 47 lines]
>>> Thanks,
>>> Steve
sklett - 10 Jun 2007 08:53 GMT
>> "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
[quoted text clipped - 4 lines]
> 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 :)
>>
[quoted text clipped - 47 lines]
>>>> Thanks,
>>>> Steve

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.