Return Value & Pass by reference

navuser1
Member Posts: 1,334
Dear Sir,
I have Created a recursive Function called SetLevel which has two Parameter one is
Para1 ( Datatype Code) and another is
Para2 ( Datatype Integer), and Para2 is using as Pass by Reference.
I Want this function should return the Para2 as Return Value (Return Type Integer).
Is it Possible as the way I want ?
Here SetLevel does not give the value where value2 gives.
What is the reason ?
Kindly reply.
Thank you.
I have Created a recursive Function called SetLevel which has two Parameter one is
Para1 ( Datatype Code) and another is
Para2 ( Datatype Integer), and Para2 is using as Pass by Reference.
I Want this function should return the Para2 as Return Value (Return Type Integer).
Is it Possible as the way I want ?
Tab.Field :=SetLevel (Value1,Value2) Tab.Modify;
Function SetLevel(HierarchyNo,Level) Level +=1; Cust2.SETRANGE("Hierarchy No.",HierarchyNo); IF Cust2.FINDFIRST THEN BEGIN HLine2.SETRANGE("Customer No.",Cust2."No."); IF HLine2.FINDFIRST THEN BEGIN SetLevel(HLine2."Hierarchy No.",Level); END ELSE EXIT(Level); END;
Here SetLevel does not give the value where value2 gives.
What is the reason ?
Kindly reply.
Thank you.
Now or Never
0
Comments
-
SetLevel (Value1,Value2); Tab.Field := Value2; Tab.Modify;
There are no bugs - only undocumented features.0 -
it can only return the value, not a reference. Just use the TAB "return value"0
-
navuser1 wrote:Para2 ( Datatype Integer), and Para2 is using as Pass by Reference.navuser1 wrote:I Want this function should return the Para2 as Return Value (Return Type Integer).navuser1 wrote:Is it Possible as the way I want ?
The function SetLevel needs to define a return type. The way you gave it to us, a compile error will result.
Moreover make sure the desired return value is defined on any way out.
And then, this code is VERY liekely to produce endless loops. You need to saveguard against it.Function SetLevel(HierarchyNo,Level) Level +=1; Cust2.SETRANGE("Hierarchy No.",HierarchyNo); IF Cust2.FINDFIRST THEN BEGIN HLine2.SETRANGE("Customer No.",Cust2."No."); IF HLine2.FINDFIRST THEN BEGIN SetLevel(HLine2."Hierarchy No.",Level); // No return value defined here! END ELSE EXIT(Level); // This statement only compiles when the function defines a return value (type) END; // No return value defined here!
0 -
Be careful, recursive funcions with VAR parameters will almost cerainly byte you!, especially so when you reuse the parameter for the recursive call.Use EXIT(Para2) or assign Para2 to the name you have given the return parameter.From your code sample it is unclear "the way I want".
I want that The function should return the desired result not the variable only.The function SetLevel needs to define a return type.The way you gave it to us, a compile error will result.
Moreover make sure the desired return value is defined on any way out.
And then, this code is VERY liekely to produce endless loops. You need to saveguard against it.Function SetLevel(HierarchyNo,Level) Level +=1; Cust2.SETRANGE("Hierarchy No.",HierarchyNo); IF Cust2.FINDFIRST THEN BEGIN HLine2.SETRANGE("Customer No.",Cust2."No."); IF HLine2.FINDFIRST THEN BEGIN SetLevel(HLine2."Hierarchy No.",Level); EXIT(Level); // I have added this line and my problem seems to solve // No return value defined here! END ELSE EXIT(Level); // This statement only compiles when the function defines a return value (type) END; // Ok // No return value defined here![ not necessary, I feel]
Now or Never0
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