HasAttribute()

Namespace ›› System ›› MfObject ›› Methods ››
Parent Previous Next

HasAttribute()

OuptupVar := instance.HasAttribute()

HasAttribute(attrib)

Gets if the current instance of class derived from MfSystemException has MfAttribute.

Parameters

attrib

The object instance derived from MfAttribute.

Returns

Returns true if this instance of class has the MfAttribute; Otherwise false.

Related

MfAttribute

Example

ItmLst := new MyItemList()
i := 0
Try
{
   While, i < 100
   {
       ran := Mfunc.Random(1, 100)
       if (Mod(ran, 2)) ; odd numbers
       {
           itm := new MyItem(A_TickCount)
           ItmLst.Add(itm)
       }
       else
       {
           itm := new MyItemCustom(A_TickCount)
           ItmLst.Add(itm)
       }
       i++
       sleep, 5
   }        
}
Catch e
{
   MsgBox, 16, Error, % e.Message
}

MsgBox, 64, Custom Items, % "Custom Item Count: " . ItmLst.CountCustom
MsgBox, 64, Total Items, % "Total Item Count: " . ItmLst.Count

; Now we will remove the first item in the list that has a custom attribute
iFirstCustom := -1
For i, Item in ItmLst
{
   if (Item.HasAttribute(CustomAttribute))
   {
       iFirstCustom := i
       break
   }
}
if (iFirstCustom > -1)
{
   ItmLst.RemoveAt(iFirstCustom)
   MsgBox, 64, Custom Items, % "Custom Item Count: " . ItmLst.CountCustom
   MsgBox, 64, Total Items, % "Total Item Count: " . ItmLst.Count
}

ExitApp

; Create a base Class
class MyItemBase extends MfObject
{
    __New(ID) {
           if (this.__Class = "MyItemBase") {
           throw new MfNotSupportedException(MfEnvironment.Instance.GetResourceString("NotSupportedException_AbstractClass"
               , "MyItemBase"))
       }
       base.__New()
       this.m_isInherited := True

       If (MfNull.IsNull(ID))
       {
               ex := new MfArgumentNullException("ID")
               ex.SetProp(A_LineFile, A_LineNumber, A_ThisFunc)
               Throw ex
       }

       Try
       {
               this.m_ItemID := MfInt64.GetValue(ID)
       }
       Catch e
       {
               ex := new MfArgumentException(MfEnvironment.Instance.GetResourceString("Arg_InvalidCastException"), e)
               ex.SetProp(A_LineFile, A_LineNumber, A_ThisFunc)
               Throw ex
       }
   }

   m_ItemID := Null
   ItemID[]
   {
       get {
           return this.m_ItemID
       }
       set {
           this.m_ItemID := MfInt64.GetValue(value)
           return this.m_ItemID
       }
   }
}

; Represents a General Item
class MyItem extends MyItemBase
{
   __New(ID) {
       base.__New(ID)
       this.m_isInherited := this.__Class != "MyItem"
   }
}

; Represents a Custom Item with Custom Attribute
class MyItemCustom extends MyItemBase
{
   __New(ID) {
       base.__New(ID)
       this.m_isInherited := this.__Class != "MyItemCustom"
       ; Add Custom Attribute Instance
       this.AddAttribute(CustomAttribute.Instance)
   }
}

; List Class that only accepts classes derived from MyItemBase
class MyItemList extends MfGenericList
{
   __New(ID) {
       base.__New(MyItemBase)
       this.m_isInherited := this.__Class != "MyItemList"
   }

   ; overrides MfGenericList Add
   Add(obj) {
           base.Add(obj)
           if (obj.HasAttribute(CustomAttribute))
           {
                   this.m_CountCustom ++
           }
   }

   ; overrides MfGenericList Remove
   Remove(obj) {
           RemovedItem := base.Remove(obj)
           if ((MfNull.IsNull(RemovedItem) = false) && (RemovedItem.HasAttribute(CustomAttribute)))
           {
                   this.m_CountCustom --
           }
   }

   ; overrides MfGenericList RemoveAt
   RemoveAt(index) {
           RemovedItem := base.RemoveAt(index)
           if ((MfNull.IsNull(RemovedItem) = false) && (RemovedItem.HasAttribute(CustomAttribute)))
           {
                   this.m_CountCustom --
           }
   }

   m_CountCustom := 0
   ; property that represents Items in list with CustomAttriube
   CountCustom[]
   {
       get {
           return this.m_CountCustom
       }
       set {
           ex := new MfNotSupportedException(MfEnvironment.Instance.GetResourceString("NotSupportedException_readonly_Property"))
           ex.SetProp(A_LineFile, A_LineNumber, A_ThisFunc)
           Throw ex
       }
   }
}

class CustomAttribute extends MfAttribute
{
   static _instance := "" ; Static var to contain the singleton instance

   __New() {
       base.__New()
       this.m_isInherited := this.__Class != "CustomAttribute"
   }

   ; Overrides MfSingletonBase
   DestroyInstance() {
      CustomAttribute._instance := Null ; Clears current instance and releases memory
   }

   ; Overrides MfSingletonBase
   GetInstance() {
       if (MfNull.IsNull(CustomAttribute._instance)) {
          CustomAttribute._instance := new CustomAttribute()
       }
       return CustomAttribute._instance
   }
}