What does this statement do?

MisConsultng
Member Posts: 17
Hi all,
Sometimes I see a line of code that seems incomplete but it's not, and I am just wondering what it means and why it's written this way. Below is an example:
IF ShipmentMethod.GET("Sales Shipment Header"."Shipment Method Code") THEN BEGIN
END;
My question is specifically related to the THEN BEGIN ?? END part.
Thanks in advance,
MisConsultng
Sometimes I see a line of code that seems incomplete but it's not, and I am just wondering what it means and why it's written this way. Below is an example:
IF ShipmentMethod.GET("Sales Shipment Header"."Shipment Method Code") THEN BEGIN
END;
My question is specifically related to the THEN BEGIN ?? END part.
Thanks in advance,
MisConsultng
0
Best Answer
-
It's lazy programming, done to prevent the error message. When you do ShipmentMethod.GET, and the record does not exist, the system will generate an error. Some programmers will then put the IF statement around the GET, so that it will never generate an error message, even if the record does not exist. The IF statement returns FALSE, and no error is generated.
Programming around error message is a good thing of course, but then you need to also program what the system should do if the record does not exist, like set default values.6
Answers
-
If shipment method which is mentioned in Sales shipment header is found in shipment method table then execute steps in BEGIN END part.
Is it answer to your question? or please provide more details.0 -
Just to add on what Mohana Said -
BEGIN and END allow to execute Multiple Statement if the IF statement Condition Met, without Begin and End only First statement executes.
Hope it answers your query.
Thanks & Regards,
Saurav Dhyani
Do you Know this About NAV?
Connect - Twitter | Facebook | Google + | YouTube
Follow - Blog | Facebook Page | Google + Page0 -
MisConsultng wrote: »IF ShipmentMethod.GET("Sales Shipment Header"."Shipment Method Code") THEN BEGIN
END;
My question is specifically related to the THEN BEGIN ?? END part.
The BEGIN END; part, without any statements in between BEGIN and END does nothing at all. BEGIN ... END is called a compound statement.
C/AL usually allows for just one statement for it's control statements likeIF <condition> THEN ... ELSE ... REPEAT ... UNTIL <condition> WHILE <condition> DO ...
In any place where there is an ... above, exactly one statement may go. If you need to do more, you can use the compound statement, which allows for a list of statements between BEGIN and END, separated by semicolons.
Most programming languages use the concept of code blocks. C/AL does not, thus the need for a compound statement.
In your example, the IF statement around GET as a condition may well be used for it's side effect on GET. If you use GET as a statement it causes an error, if it fails; if GET is used as an expression, it instead returns FALSE.
So, your code sample tries to get the record, but does not handle the case where it can or can't be found (assuming there is indeed no code between BEGIN and END). It is unusual to use BEGIN END; in this case though. Usually just the semicolon is written (which is the empty statement, and makes the subsequent statement separator redundant):IF ShipmentMethod.GET("Sales Shipment Header"."Shipment Method Code") THEN;
1 -
Thank you all for your reply. I understand the normal syntax and usage of this statement which basically says if the statement is true, then do something. The do something part is between Begin and End. I just don't understand what is the purpose of not having a statement between Begin and End in the above example.0
-
It's lazy programming, done to prevent the error message. When you do ShipmentMethod.GET, and the record does not exist, the system will generate an error. Some programmers will then put the IF statement around the GET, so that it will never generate an error message, even if the record does not exist. The IF statement returns FALSE, and no error is generated.
Programming around error message is a good thing of course, but then you need to also program what the system should do if the record does not exist, like set default values.6 -
Sorry I didn't see the first response which basically is the answer I was looking for: lazy programming to prevent error if the GET statement returns false. It's same as ShipmentMethod.GET but with a lazy twist. :-)
Thanks again guys.0
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