When is a default value for a variable nessesary

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.

1 Like

@winnie.liu

Welcome to the Community

For rare senarios we need to intialize the array like new string(){} it Creating an empty array of strings.

you need not to use String()

cheers

1 Like

Hi @Shiva_Nikhil

Thanks for your quikly reply. Your answer has helped me understand easily. Thank you.

1 Like

@winnie.liu

Thanks and Happy Automation

Cheers

1 Like

hi @Jeroen_van_Loon ,

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!

@winnie.liu

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

Cheers

1 Like

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.

1 Like

@Jeroen_van_Loon

Got it. Thank you so much for your generous and proffesional help.

Hi @Shiva_Nikhil

Thank you so much! I got the answer.

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.