HurleyShanabarger Posted August 10, 2022 Posted August 10, 2022 Hello, I was wondering, what you think is the best practice / good choice. I often have one datapoint and depending on the value of that datapoint a set of variables has to be assigned. In the following snippet I have four example, but maybe there is a completely different approach for this? expandcollapse popup#Region ;**** Directives created by AutoIt3Wrapper_GUI **** #Tidy_Parameters=/reel /sf /ri #EndRegion ;**** Directives created by AutoIt3Wrapper_GUI **** #Region Variables/Opt Opt("MustDeclareVars", 1) Global Enum $g_eName, $g_eType, $g_eDescription, $g_eMax Global Const $tagDATA = "struct;wchar sgName[256];wchar sgType[256];wchar sgDescription[256];endstruct" #EndRegion Variables/Opt #Region Main _Main() Func _Main() Local $sgData = "Test" ; Example 1: Using array Local $arResult_v1 = _ExtractData_v1($sgData) ConsoleWrite("Example 1 / Name: " & $arResult_v1[0] & @CRLF) ; Example 2: Using array with enum Local $arResult_v2 = _ExtractData_v2($sgData) ConsoleWrite("Example 2 / Name: " & $arResult_v2[0] & @CRLF) ; Example 3: Using function parameter with ByRef Local $sgName, $sgType, $sgDescription _ExtractData_v3($sgName, $sgType, $sgDescription, $sgData) ConsoleWrite("Example 3 / Name: " & $sgName & @CRLF) ; Example 4: Using struct Local $tResult $tResult = _ExtractData_v4($sgData) ConsoleWrite("Example 4 / Name: " & $tResult.sgName & @CRLF) EndFunc ;==>_Main #EndRegion Main #Region Functions Func _ExtractData_v1($sgData) Local $arData[3] Select Case $sgData = 1 $arData[0] = "x" $arData[1] = "y" $arData[2] = "z" Case StringRegExp($sgData, "some pattern") $arData[0] = "a" $arData[1] = "b" $arData[2] = "c" Case Else $arData[0] = "Some other Name" $arData[1] = "Some other Type" $arData[2] = "Some other Description" EndSelect Return $arData EndFunc Func _ExtractData_v2($sgData) Local $arData[$g_eMax] Select Case $sgData = 1 $arData[$g_eName] = "x" $arData[$g_eType] = "y" $arData[$g_eDescription] = "z" Case StringRegExp($sgData, "some pattern") $arData[$g_eName] = "a" $arData[$g_eType] = "b" $arData[$g_eDescription] = "c" Case Else $arData[$g_eName] = "Some other Name" $arData[$g_eType] = "Some other Type" $arData[$g_eDescription] = "Some other Description" EndSelect Return $arData EndFunc Func _ExtractData_v3(ByRef $sgName, ByRef $sgType, ByRef $sgDescription, $sgData) Select Case $sgData = 1 $sgName = "x" $sgType = "y" $sgDescription = "z" Case StringRegExp($sgData, "some pattern") $sgName = "a" $sgType = "b" $sgDescription = "c" Case Else $sgName = "Some other Name" $sgType = "Some other Type" $sgDescription = "Some other Description" EndSelect EndFunc Func _ExtractData_v4($sgData) Local $tStruct = DllStructCreate($tagDATA) Select Case $sgData = 1 $tStruct.sgName = "x" $tStruct.sgType = "y" $tStruct.sgDescription = "z" Case StringRegExp($sgData, "some pattern") $tStruct.sgName = "a" $tStruct.sgType = "b" $tStruct.sgDescription = "c" Case Else $tStruct.sgName = "Some other Name" $tStruct.sgType = "Some other Type" $tStruct.sgDescription = "Some other Description" EndSelect Return $tStruct EndFunc #EndRegion Functions
iAmNewbe Posted August 13, 2022 Posted August 13, 2022 (edited) You are asking the best way to return values from a function? The best way depends on what you are intending to do with the values after the function does whatever it does. Do you intend to display the value? Insert it into a database or external file of some sort to be used later? Use the output of the function to complete an operation or algorithm? Or you may not even need to return a value and instead just export it within the function. If you are going to pass it to another function for processing or return back to a calling function then you do need to use 'return.' Usually if you are concerned with best practices then modular thinking needs to be used and keep functions to ONE purpose and one purpose only. Not do something THEN also do something else. You do one thing then return the data to somewhere or send the data to another function to do the next thing. You can RETURN or use Global variables when passing values out of a function or send directly to an external file or database. Pretty much what I can think of to answer your question. The variables can be string, numeric, array etc... the type matters to what you are doing with the processing of the value within your application. Edited August 13, 2022 by iAmNewbe
junkew Posted August 19, 2022 Posted August 19, 2022 I would say returning an object is most logical as a return value UDF and comparison for that approach https://github.com/genius257/AutoItObject-Internal FAQ 31 How to click some elements, FAQ 40 Test automation with AutoIt, Multithreading CLR .NET Powershell CMDLets
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now