Objects in code

Objects in code

Everything I read about object oriented code gives an example, like an animal, that has features such as a number of legs, and a breed, and any number of other, entirely-irrelevant-to-real-life-examples. These examples are good at describing the very, very high-level concept, but they fail to provide the concrete examples that a user might genuinely need in their coding life.

A module I have written pulls data from Active Directory, and has functions that can change that data. I believe this is a much clearer example than one that uses imaginary animals.

public class AD
{
     public static string domain;

     public AD(DirectoryEntry de)
     {
          DirectoryEntryI = de;
          //Other methods
     }

     private DirectoryEntry _DirEntry {get;set;}
     public DirectoryEntry DirEntry
     {
          get
          {
               UpdateDN();
               if (this.DN is string objectDn)
               {
                    _DirEntry = new DirectoryEntry("LDAP://" + objectDn);
                    return _DirEntry;
               }
               return null;
           }
     }
}

This class contains a constructor, uses Microsoft APIs [it would need “using System.DirectoryServices” above the namespace declaration], has member variables [DirEntry and _DirEntry] which have different accessibility, has a function [UpdateDN() which I have not included in the code to make it easier to read] and has a non-member variable to store the domain of this Active Directory object.

Personally, I think this is a far more interesting example.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.