Using := Operator with Records

ta5
Member Posts: 1,164
Hi
Sometimes C/AL code like this is used:
The question: (category "what you always wanted to know but never dared to ask")
Is this the same like the following code?
Curious about your answers, thanks a lot.
Thomas
Sometimes C/AL code like this is used:
Rec2 := Rec1(Rec2 and Rec1 are record variable of the same type, for example "Item".)
The question: (category "what you always wanted to know but never dared to ask")
Is this the same like the following code?
Rec2.GET(Rec1."No.")Any advantages/disadvantages using this kind of stile?
Curious about your answers, thanks a lot.
Thomas
0
Best Answer
-
It is not the same.
Rec2.GET(Rec1."No.") is a database operation while Rec2 := Rec1 is only in-memory copy.
If between Rec1.GET(something) and Rec2.GET(Rec1."No.") the record got changed the Rec2.GET(Rec1."No.") will obviously refresh its content.
A side effect of this is that if between Rec1.GET(something) and Rec2.GET(Rec1."No.") the record gets locked then Rec2.GET(Rec1."No.") will wait for the oher session to release the lock.
I'd say that Rec2 := Rec1 will be faster as it does not involve any database roundtrips, but some might argue that after Rec1.GET(something) or whatever command was used to get the Rec1 it is already cached so there should be not much difference.
My personal preference is to use Rec2 := Rec1 whenever I can, except in cases when I need to be sure to have a fresh copy of the record from the database.
Slawek Guzek
Dynamics NAV, MS SQL Server, Wherescape RED;
PRINCE2 Practitioner - License GR657010572SG
GDPR Certified Data Protection Officer - PECB License DPCDPO1025070-2018-037
Answers
-
It is not the same.
Rec2.GET(Rec1."No.") is a database operation while Rec2 := Rec1 is only in-memory copy.
If between Rec1.GET(something) and Rec2.GET(Rec1."No.") the record got changed the Rec2.GET(Rec1."No.") will obviously refresh its content.
A side effect of this is that if between Rec1.GET(something) and Rec2.GET(Rec1."No.") the record gets locked then Rec2.GET(Rec1."No.") will wait for the oher session to release the lock.
I'd say that Rec2 := Rec1 will be faster as it does not involve any database roundtrips, but some might argue that after Rec1.GET(something) or whatever command was used to get the Rec1 it is already cached so there should be not much difference.
My personal preference is to use Rec2 := Rec1 whenever I can, except in cases when I need to be sure to have a fresh copy of the record from the database.
Slawek Guzek
Dynamics NAV, MS SQL Server, Wherescape RED;
PRINCE2 Practitioner - License GR657010572SG
GDPR Certified Data Protection Officer - PECB License DPCDPO1025070-2018-037 -
Hi Slawek
Thanks a lot. This brings some light to my question.
Thomas0
Categories
- All Categories
- 73 General
- 73 Announcements
- 66.6K Microsoft Dynamics NAV
- 18.7K NAV Three Tier
- 38.4K NAV/Navision Classic Client
- 3.6K Navision Attain
- 2.4K Navision Financials
- 116 Navision DOS
- 851 Navision e-Commerce
- 1K NAV Tips & Tricks
- 772 NAV Dutch speaking only
- 617 NAV Courses, Exams & Certification
- 2K Microsoft Dynamics-Other
- 1.5K Dynamics AX
- 320 Dynamics CRM
- 111 Dynamics GP
- 10 Dynamics SL
- 1.5K Other
- 990 SQL General
- 383 SQL Performance
- 34 SQL Tips & Tricks
- 35 Design Patterns (General & Best Practices)
- 1 Architectural Patterns
- 10 Design Patterns
- 5 Implementation Patterns
- 53 3rd Party Products, Services & Events
- 1.6K General
- 1.1K General Chat
- 1.6K Website
- 83 Testing
- 1.2K Download section
- 23 How Tos section
- 252 Feedback
- 12 NAV TechDays 2013 Sessions
- 13 NAV TechDays 2012 Sessions