web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics AX (Archived)

Use of VAR type in X++ in new Dynamics AX RTW

(0) ShareShare
ReportReport
Posted on by

Hi all

I'm sure some of you have seen a new VAR type being used in X++. By declaring an object as VAR, the compiler will calculate the type of declared entity.

Also read about it : https://community.dynamics.com/ax/b/newdynamicsax/archive/2016/02/26/using-var-in-x

Other than this, is the VAR type a really good way to program ? Can this be used to good use in all levels of code or in other objects? What are the real benefits ?

Let's discuss and learn together!!!

*This post is locked for comments

I have the same question (0)
  • André Arnaud de Calavon Profile Picture
    300,917 Super User 2025 Season 2 on at

    Hi Manish,

    This is a great question. I think persons with .NET (C#) experience can provide their insights better. Note that there is another blog related to the VAR keyword: blogs.msdn.com/.../x-in-ax7-1-the-var-keyword.aspx

  • Martin Dráb Profile Picture
    237,807 Most Valuable Professional on at

    Usually it's just a shortcut, so you don't have to declare the type, if it would make code less readable. For example, often the type is obvious or not important and specifying it would make code less readable.

    You're welcome to use var where it makes sense, but make sure that you don't make your code less readable by omitting important type information. That the type is obvious to the compiler doesn't meat it's obvious to developers.

    In C#, it's also necessary for variables of anonymous types, because there is no way how to declare such a type. For example, you have a LINQ query returning two fields and the framework returns an object with these two fields, but it doesn't have any named type.

    Anonymous types are going to be supported in X++ in future.

  • Martin Dráb Profile Picture
    237,807 Most Valuable Professional on at

    It's probably important to reiterate that var doesn't represent a dynamic type. It's still a static type and compile-time type control is still in place.

  • Community Member Profile Picture
    on at

    Hi Andre,

    Is there any restrictions or issues if we use VAR type for a field type in a table.  also, is there any conversion functions available to convert VAR to other types?

  • Martin Dráb Profile Picture
    237,807 Most Valuable Professional on at

    You can use it for local variables; nowhere else.

    The type of table fields must be defined at design time, not inferred from how the field it used.

  • Martin Dráb Profile Picture
    237,807 Most Valuable Professional on at

    The question about conversion shows that you missed the point. VAR is not a type. It's a keyword that allows you not to manually write the type of a variable in code, because the compiler already knows it. For example, var x = "abc" it's exactly the same as str x = "abc". The CIL will be identical.

  • Iulian Cordobin Profile Picture
    8,201 on at

    As with any other enhancements that will be brought in the X++ compiler from the C# world, this can also be consider a great addition to the options a true developer will have from the programming language. The only thing I would like to add, since Martin already outlined the technical aspects, (as I also noticed as a discussion thread on a C# blog) would be the idea that the power of the var keyword would lie in the ability of each developer to really know when to use it and when to refrain from it.

  • Iulian Cordobin Profile Picture
    8,201 on at

    To comment further on the idea that the developer should truly understand when to use it or not, is that, as the definition of the VAR keyword becomes more and more obvious, it will be used by some of the lazy developers to really declare their variables as VAR rather than using the type.

    This is really where the problem lies: the VAR should be used when the type of the object we get is unknown, but some will abuse its power and start to write less readable code (we know that's true, and hope we will not get to a point where every declaration will be VAR xyz).

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics AX (Archived)

#1
Martin Dráb Profile Picture

Martin Dráb 4 Most Valuable Professional

#1
Priya_K Profile Picture

Priya_K 4

#3
MyDynamicsNAV Profile Picture

MyDynamicsNAV 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans