Search This Blog

Monday, September 19, 2011

IS and AS Keywords Introduced in AX2012

In Microsoft Dynamics AX2012, the X++ language provides the as and is expression operators to control downcast assignments. Downcast assignments involve class or table inheritance.

Assignment statements that implicitly downcast can cause errors that are hard for the programmer to predict or diagnose.

 You can use the as keyword to make your downcasts explicit.
You can use the is keyword to test whether a downcast is valid at run time.
as Example
static void AsTestJob33(Args _args)
    BaseClass basec; 
    DerivedClass derivedc;

    BottomClass bottomc; ;

    derivedc = new DerivedClass();

    basec = derivedc;

    derivedc = basec as DerivedClass;

    bottomc = new BottomClass();
    bottomc = basec as DerivedClass;

is Example

static void IsKeywordJob46(Args _args) 
    DerivedClass derivedc;
    BaseClass basec;

    basec = new DerivedClass();  // An upcast.
    if (basec IS DerivedClass)
        info("Test 1: (basec IS DerivedClass) is true. Good.");
        derivedc = basec AS DerivedClass;

    basec = new BaseClass();
    if (!(basec IS DerivedClass))
        info("Test 2: (!(basec IS DerivedClass) is true. Good."));


  1. Hi,

    Will u please give some other example. I can't able to understand the way you explained. Would you please explain it with further clear example?


  2. Hi Kannan,
    In simple words
    Is- means validating against another
    normal ex:- krishna is son of Veera(true)
    so is keyword always validates as this objects is that type or not
    Abstract absbase base class
    have derived class derived A,derivedb
    so when object create i can do like this as
    absbase=new DerivedA();
    now if(absbase is DerivedA) then true

    AS keyword is always use to convert ex
    Dervivedb _dervivedB;
    absbase=new DerivedB();

    derivedb=absbase as DerivedB;

    Hope you understand now.

  3. Hi Krishna.
    still no answer for the question "Why should we use keyword 'as'?" Without it we can downcast successfully.

    Is it only required for commenting or just making code explicitly clear purpose? Why MSDN says: "AS is needed for a downcast assignment"


Thanks for visiting my blog,
I will reply for your comment within 48 hours.