Home› X++ Programming Language› / Programs / Create a class and its constructor and access it using a job in AX 2012
🚀 Programming Example

Create a class and its constructor and access it using a job in AX 2012

421 Views
Beginner
Code Snippet
Create a class and its constructor and access it using a job in AX 2012

💻 Program Code

 

// Class Declaration
public class Fruit
{
  str name;
  str color;
}

// New Method which act as a constructor
public void new(str _name, str _color)
{
    name = _name;
    color = _color;

}

// Get Color methos inside class Fruit
public str get_color()
{
    return color;
}

// Get Name methos inside class Fruit
public str get_name()
{
    return name;
}
 
</xmp>
</pre>

<h4> Code: Job </h4>
<p> This job will access the class </p>
<pre class="prettyprint">
<xmp>
static void AccessClassFruit(Args _args)
{
    
     Fruit apple;
     apple = new Fruit("apple","red");
     info(strFmt("%1", apple.get_name()));
 
}
 

 

🖥 Program Output

                                apple
                            

📘 Explanation

None
📚 Learning Subject

Master Programming Through Practical Examples

Improve your coding logic, problem-solving skills and programming confidence by practicing real-world examples with explanations.

🎯 How to learn from this example

First understand the algorithm carefully. Then study the program line-by-line and compare it with the output. Finally, review the explanation section to strengthen your logic and programming understanding.

🔥 Practice suggestion

Rewrite the program without looking at the code. Modify values, conditions or logic and run it again. This helps improve confidence and strengthens coding skills much faster.