Questions about Polymorphism

So I understand the importance of Polymorphism, including how vital it is. But something I don’t quite understand is what about the Constructor and any inherited Class the initial Base Class may have.

For example, I have a class to test some criteria- Level of account, Windows Version, and some other checks.

// Base Class
public class CheckInstalled : CheckUserLevel
{
       public CheckInstalled()
       {
             // Check verify the CheckUserLevel token is true, if it isn't throw an exception and close.
             if (!(_level == true))
             {
                    // The above implementation would technically go here.
             }
        }

        public virtual void IsInstalled()
        {
             // Implementation of initial check of general software.
        }
}

So that would in theory be the Base Class which will do a Generic check; but there are other applications I need to test if they are installed. So now I would create a Class for:

  1. Sql
  2. IIs
  3. Dnn

Or whatever you may need to ensure is checked. So for each of those Classes I’d inherit the CheckInstalled Class. In each one I’d have this:

public override void IsInstalled()
{
      // Implementation to check for that specific Class Item is installed.
}

So I see how Polymorphism will really simplify and keep each one of these Classes separate. But will provide that flexibility for Object Orientation.

The root of my question, the Base Class Constructor contains that inherited token value. But do all other Classes that inherit the value? Essentially that Base Class Constructor consistently execute? Or does the _level token have to be called in each?

Hopefully that makes sense. If not please let me know and I’ll try and rephrase it.


Update:

The _level is invoked here:

class CheckUserLevel
    {

        // Token Bool:
        private bool _level = false;

        #region Constructor:

        protected CheckUserLevel()
        {

            // Invoke Method On Creation:
            Elevate();

        }

        #endregion

        public void Elevate()
        {

            // Get Identity:
            WindowsIdentity user = WindowsIdentity.GetCurrent();

            // Set Principal
            WindowsPrincipal role = new WindowsPrincipal(user);

            #region Test Operating System for UAC:

            if (Environment.OSVersion.Platform != PlatformID.Win32NT || Environment.OSVersion.Version.Major < 6)
            {

                // False:
                _level = false;

                // Todo: Exception/ Exception Log

            }

            #endregion

            else
            {

                #region Test Identity Not Null:

                if (user == null)
                {

                    // False:
                    _level = false;

                    // Todo: "Exception Log / Exception"

                }

                #endregion

                else
                {

                    #region Ensure Security Role:

                    if (!(role.IsInRole(WindowsBuiltInRole.Administrator)))
                    {

                        // False:
                        _level = false;

                        // Todo: "Exception Log / Exception"

                    }

                    else
                    {

                        // True:
                        _level = true;

                    }

                    #endregion


                } // Nested Else 'Close'

            } // Initial Else 'Close'

        } // End of Class.

    }

The separate class actually generates the boolean value; as anytime that class is invoked it will assign a value to it. I didn’t want to expose too much with this class mostly because of its role in testing for Administration.

The problem:

Several methods will require Administrative Permission; my thought was to inherit on the base CheckInstalled. Which in it’s creation
will automatically check the boolean state of _level. Then from
that point; this CheckInstalled can be inherited to other classes;
then I can simply override IsInstalled to bend to the desired task
for that item.

Is that a bad thought on my part?

2

Two points; Firstly the constructor should generally be used for initial setup only. In your case it would loading any dependencies and suchlike, not doing the actual check.

Secondly (and more importantly) inheritance should be used in a case when you can share code between similar classes. This case dosent promote code reuse. Its a very good candidate for Interface usage, you can define an interface with CheckInstalled() then you can decouple the specific implementation. (for more reading have a look at the strategy pattern)

3

// Base Class
internal class CheckInstalled : CheckUserLevel
{        
    public CheckInstalled()
    {
        // Check verify the CheckUserLevel token is true, if it isn't throw an exception and close.
        if (!Verified)
         {
                // The above implementation would technically go here.
         }
    }

    public virtual void IsInstalled()
    {
        // Implementation of initial check of general software.
    }
}

internal class CheckUserLevel
{
    //Derived classes inherit the Verified property
    public bool Verified {get;set;}//Verified is more indicative of the action.  _level implies an enumeration of sorts        

    public CheckUserLevel()//the base constructor is called prior to the derived constructor.          {
        Verify();//Caution: long running operations should not go in constructors
    }

    private void Verify()
    {
        bool verified = true;
        //do work

        Verified = verified;
    }
}

The base constructor is called anytime a derived class is instantiated. For this and other reasons, CheckUserLevel should be a has-a relationship. In other words, CheckuserLevel should be a Member of CheckInstalled.

However, because it appears that you do not want to bind verification to construction…

Or does the _level token have to be called in each?

…a static Member makes more sense.

In other words convert to:

// Base Class
internal class CheckInstalled
{        
    public CheckInstalled()
    {
        // Check verify the CheckUserLevel token is true, if it isn't throw an exception and close.
        if (!CheckUserLevel.Verified)
         {
                // The above implementation would technically go here.
         }
    }

    public virtual void IsInstalled()
    {
        // Implementation of initial check of general software.
    }
}

internal class CheckUserLevel
{        
    public static bool Verified {get;set;}        

    static CheckUserLevel()
    {
        Verify();
    }

    private static void Verify()
    {
        bool verified = true;
        //do work

        Verified = verified;
    }
}

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật