oreodeluxe.blogg.se

Downcast to c
Downcast to c












downcast to c

The Dialog class does not inherit from any class, yet the assignment and call statements in the following code example all work. Some classes inherit from the Object class, some inherit from another class, and some do not inherit from any class. You could misinterpret some X++ statements if you are unaware of this special case. The X++ compiler has special logic to bypass type checking for assignments to and from variables that are declared of type Object. The Object class can appear as a special case in inheritance functionality. Test 2: (!(basec IS DerivedClass)) is true. Info("Test 2: (!(basec IS DerivedClass) is true. Info("Test 1: (basec IS DerivedClass) is true. static void IsKeywordJob46(Args _args) // X++īasec = new DerivedClass() // An upcast. The is and as keywords are uppercase to make them more visible in this example. The as keyword is used after the is keyword verifies that the as keyword will the object. The following code example contains a common use of the is keyword. This is keyword is often used to safely test whether the as keyword will work. The type of the underlying object is what matters in the is expression, not the type of the variable that references the object. Info(strFmt("%1", (myForm is TreeNode))) The Infolog displays 1 during run time, where 1 means true.Īn object is an instance of its own class type. No supertype object can be considered to also be of its subtype class.Ī null reference causes the is expression to return false. The Infolog displays 0 during run time, where 0 means false. Info(strFmt("%1", (myTreeNode is Form))) Both the Form class and the Query extends the TreeNode class, but neither Form nor Query is a subtype of the other. The compiler ascertains that the Form class and the Query class are not part of the same inheritance hierarchy. The code examples rely on the fact that the Form class and the Query class both extend the TreeNode class. The following code examples illustrate the conditions that control whether an is expression returns true or false. The is keyword causes the system to check the object that the variable references, not the declared type of the variable that references the object. Is Keyword at Run TimeĪt run time the type of variable that references the underlying object is irrelevant to the is keyword. The compiler reports a similar error for any plain assignment statement between two types where neither is a subtype of the other, and they are not of the same type. The X++ compiler reports an error if an is keyword expression compares two types where neither is a subtype of the other, and they are not of the same type. The is expression returns true if the object is a subtype of the class, or if the object is the same type as the class. The is keyword ascertains whether an object is a subtype of a specified class. AS causes this invalid downcast to assign null.

downcast to c downcast to c

AS is needed for a downcast assignment like this. AS is not needed for an upcast assignment like this. The following code would compile and run without errors. The upcast assignment to basec does not need the as keyword, but the downcast assignment to derivedc does need the as keyword. The code example contains two valid assignments between its basec and derivedc variables. In the following code example, the DerivedClass class extends the BaseClass class. As Keyword at Run TimeĪt run time, the as keyword causes the downcast assignment statement to assign null if the downcast is invalid. Starting in Microsoft Dynamics AX 2012 or in a release to follow, the X++ compiler reports an error for downcast assignment statements that lack the as keyword. The as keyword tells other programmers and the compiler that you believe the downcast will be valid during run time. You can use the as keyword for assignments that downcast from a base class variable to a derived class variable. You can use the is keyword to test whether a downcast is valid at run time. You can use the as keyword to make your downcasts explicit. For information about the terminology of inheritance, see Inheritance Terminology.Īssignment statements that implicitly downcast can cause errors that are hard for the programmer to predict or diagnose.














Downcast to c