When do you have to put a 'new <type>' default value when defining arguments or variables

Could someone please explain when is it necessary to include a default value of type ‘new’

e.g.

Is this only necessary for datatables or is it also necessary for other datatypes and if so what is the rule.

I’ve been having a problem with my main workflow which is generating an error message when it completes the main workflow (and so should have finished).

The error message seems to imply that a DataRow item requires a default

However I have gone through all my datarow items and if I put a default of new System.Data.DataRow as a default I get the following error.

Saying that the DataRow item is a ‘Protected Friend’ so that seems to be the wrong default value for a datarow variable.

If someone could please explain when the ‘new’ default is required and if you have any insights into why my main is erroring that would be amazing.

Many thanks,

That error regarding the constructor doesn’t look like the typical error for when a variable needs to be initialized. Are you certain that the workflow is feeding that variable a single data row? Could it be trying to feed a datatable, or a collection of data rows? It may be that you need a list of data rows rather than just the data row type.

1 Like

here is the reason :
https://forums.asp.net/t/444453.aspx?DataRow+not+accessible+because+it+is+Protected+

you can remove new keyword from this and that will work

1 Like

Thank you both for your help. Good UiPathing!

Hi I’m not sure that removing the new keyword works.

For example I’m not sure which DataRow variable/Argument is causing my issue so in main if I go to my TransactionItem DataRow variable and as it’s default put System.Data.DataRow

Which complains that DataRow is a type and cannot be used as an expression. This kind of makes sense to me, why would you put a type as a default value, which is what also confuses me about why you have to put new System.Data.Datatable when defining a DataTable variable/Argument.

I may however have not followed the instructions properly (but I think I have), if I have, I’m not sure what I should do to sort things out but if I haven’t then I’m also not sure what to do to sort things out.

Thanks for the advice (if you could give it at idiot level that would be ideal)

@charliefik

Try like this, DataTable_Variable.NewRow

Regards,
Mahesh

Hi Mahesh1,

So I basically have 2 issues

  1. When/Why do you need to put new System.Data.DataTable as a default value when defining a DataTable and do you need to do this for other types of variables and if so when and why (what are the rules)

  2. In my Level 3 training Hash Code project my workflow runs until the very end (so it’s created tables of transactions and updated them etc. ) but as it leaves the ‘main’ workflow it generates an error message

and I can’t figure out why. My WorkItems Data table is created from a web scrape and then an array of datarows is generated from a select on that datatable, (I don’t even know which DataRow the error message is relating to or how to work it out)

I’m not really sure how the NewRow method will help with these things (because I don’t really need to add extra rows to my data table as it is converted into an array of datarows, however if I am missing something please clarify)

Many thanks,

@charliefik

If the DataTable is is null, and if you try to access it, it will throw an exception, Object Reference not set to an instance of an object, For that you have to initialise.

What is your requirement.

Regards,
Mahesh

1 Like

@charliefik

If you want to add empty row to a datatable Dta
then you have to initailse Dta.NewRow. This will initialise the DataRow with the Schema of that DataTable

Regards,
Mahesh

I don’t think that I’m trying to access the datatable I’m leaving the main workflow as the whole process is done when the error pops up so I’m not trying to add a new data row.

I’ve really no idea what is going on.

@charliefik

Can you please tell what is your requirement and what you have done.

Regards,
Mahesh

@charliefik why do you want to have a default value? in case you need to get a default value my recomendation is to use “nothing” in the argument and then do somehting like if (isnothing) {create a data row}

data row creation is not exactly a simple problem… what columns do you want? type? etc…

In my opinion, using datarow as an input for a workflow is not the best option i would use array to evade this problem. You can use dataRow.ItemArray and use the type array[object] as the input datatype then you can just do {“1”,“2”} as the default and you can add to a datatable just with add data row item.

Hi,

I don’t want to necessarily have a default value I just want to understand why it is necessary to include for instance ‘new System.Data.DataTable’ as a default value when creating a DataTable variable (my confusion is that this seems to be a type rather than a default value but apparently it is necessary to include it). My first question is why does the ‘new ’ have to be included and does it have to be included when initialising any other variables/arguments and what is the rule for that. The datarow issue relates to the second question below as the error message mentions datarows and defaults.

My second issue is the error message that I am receiving that is generated when leaving my main workflow from the Hash Table excercise after everything has been completed.

This mentions Datarows and default values

My Main workflow is

Main.xaml (75.0 KB)

Mahesh1 suggested that if a data table is null and you reference it, it will throw an exception.

I don’t think this is the case here.

  1. The exception seems to be concerned with datarows.
  2. In the project the webpage is scraped into a datatable. This is then filtered using a select into an array of datarows. Everything then runs absolutely fine until the program has basically finished everything and leaves the main (absolutely outer) workflow which doesn’t make sense to me as everything should be done at that point.