October 17, 2013

Abstract Class and Abstract Method in AXapta

     Abstract Class and Abstract Method

Abstract Class:
When we declare a class as abstract, this class cannot initiate in X++ code. To use this class or its method we have to first extend this class than only we are able to use this class or its method. To understand the abstract class consider following example
We have three classes
     1.      absClass  (it’s an abstract class)
     2.      normalClass (an another class which will use the absClass methods)
     3.      extendAbsClass (this class will extends the absClass)

    1.      abstract class absClass
     {
     }

    void printName()
   {
    ;    info("AbsClass... Deepak"); 
   }


    2.      class extendAbsClass extends absClass
{
}

    3.      class normalClass
{
}

  void accessAbsClass()
{
    absClass        absClass;
    extendAbsClass  extendAbsClass;
    ;
 //   absClass = new absClass();    // this declaration will throw error “Object could not be created because class absClass is abstract” so first we extend absClass into extendsAbsClass and further use extendsAbsClass to access absClass methods.
    extendAbsClass  = new extendAbsClass();
    extendAbsClass.printName();
}


Abstract Method:

When we declare a method as abstract , this method should be overload in child class or we can say , this method  should be declare/initiate in child class, than only we can use this class or its method.
Note:
a.      Abstract methods may only be declared in abstract classes.
b.      No code or declarations are allowed in abstract methods.

We have three classes
i.                    absMethodClass
ii.                  extendAbsClass
iii.                NormalClass

1.      abstract class absMethodClass
{
}

abstract void absPrintName()
{
                        // we cannot declare any code in abstract method
}

2.      class extendAbsClass extends absMethodClass
{
}

void absPrintName()
{
    ; // we have to initiate abstract method here as this class extends the abstract class.
    info("abstract method declaration in derived class");
}
3.      class childClass_1
{
}

void accessAbsClass()
{
    extendAbsClass  extendAbsClass;
    ;
    extendAbsClass  = new extendAbsClass();
    extendAbsClass.absPrintName();

}


- Harry

October 02, 2013

Connecting to Databases through X++ PART -IV

Connecting to Databases through X++ PART -IV


Connection Class:
Connection class is mainly used for accessing the database in which a user has logged into AX i.e. Current Database and carry out the operations. This class is exetensively used in ReleaseUpdateDB classes, the classes used in data upgrades. This class cannot be run on client and should always be run on server. One more unique thing that I noticed is that the statements that you want to execute should be asserted first for permissions and then passed on to other method where they are executed. Create a class with following methods and set its RunOn property to Server.
class TestSQLExecuteClass
{
}




//This method tests the permissions for statement and then calls the method that will execute the statement
static void dbConnectionClass()
{
    ResultSet   rs;
    SqlStatementExecutePermission perm;
    ;
    perm = new SQLStatementExecutePermission("select * from CustTable where DATAAREAID = ‘CEU’");
    perm.assert();
    rs = TestSQLExecuteClass::statementExeQuery("select * from CustTable where DATAAREAID = ‘CEU’");
    while (rs.next())
    {
        info(rs.getString(1));
    }
    CodeAccessPermission::revertAssert();
}
//Executes the passed statement
private static ResultSet statementExeQuery(str _sql, Connection _con = null)
{
    ResultSet   resultSet;
    Statement   statement;
    ;
    try
    {
        if(!_con)
        {
            _con = new Connection();
        }
        statement = _con.createStatement();
// Do not call assert() here, do it in the caller
        // BP deviation documented
        resultSet = statement.executeQuery(_sql);
    }
    catch (Exception::Error)
    {
        throw error("@SYS99562");
    }
    return resultSet;
}
Now you can call the method in a job as shown below:
static void dbConnectionClass(Args _args)
{
    ;
    TestSQLExecuteClass::dbConnectionClass();
}
These examples shown here are pretty simple and easy to understand and start with. Hope it helps you in building ‘connections’

Related posts, 


-Harry