Hi wonderful guys,
I am studying “Switch” practise in the course of " Variables, Arguments, and Control Flow".
In the solution, there are 3 array of strings variable “AxArray”, “BxArray”,“CxArray”. and in the Swich case of For each activity, we have assign value to these 3 variables, for example, assign “AxArray” with value “AxArray.Concat({item}).ToArray.”
I’m not understand why should be nessesary there a default value for these 3 variables, the expression in the default value is “new string(){}”.
It will not work if there is no default value in this case. But I saw in other scenarios, the default value of a variable is not madatory. I don’t know why in this case the default value is nessary.
and how the expression come out? Why in this solution, the default value is "“new string(){}”, not “string()”,or others else? Many thanks for your help.
It is not as much a default value that is needed.
The object (in this case an Array of String) needs to be initialized before it can be used.
So the default value of your null-object becomes an empty, but initialised array.
Doing that in the default section is an option to do it (and to not forget it).
Another option is to do it as the first assign action.
If however, your object is filled by a return of another function, activity or workflow, you don’t have to do the initialisation, since the returning activity will have done it for you.
This pretty much counts for all object variables. Default datatypes such as a string or integer do not require this to be done by you.
For more background look into Object Oriented Programming.
Thanks for your quickly reply which is very usefull to help me to understand. And thanks for your suggestion for the way to find its background.
Can I ask a further question, what’s the option meaning of doing it as the first assign action. For this case, how to apply this option which could initialise the array you mentioned at above. The question maybe help me to understand more about the default value. Appreciated!
If however, your object is filled by a return of another function, activity or workflow, you don’t have to do the initialization, since the returning activity will have done it for you
The meaning is like that if you are assign any values during the run time then you no need to initialize
Well, what I do if I have several obect that require initialisation in a single workflow, I just group them together on top. Make a nested sequence block or multi assign and then just wite the assigns. Sort of like an init block.
So grouped together:
Assign: myArray = new string(){}
Assign: myDictionary = new Dictionary(Of String, String)
etc etc.