Home > Back-end >  How to pass literal object array to function?
How to pass literal object array to function?

Time:01-03

I have a function with signature thus:

public void LoadLookups(DataTable[] tables)

It works fine when I call it like this:

DataTable[] tables = { timeTrackDataSet.Users, timeTrackDataSet.TaskTypes, timeTrackDataSet.Clients };
timeTrackDataSet.LoadLookups(tables);

but I can't figure out how to call the method with just one line. I've tried:

timeTrackDataSet.LoadLookups({ timeTrackDataSet.Users, timeTrackDataSet.TaskTypes, timeTrackDataSet.Clients });

and

timeTrackDataSet.LoadLookups(DataTable[] = { timeTrackDataSet.Users, timeTrackDataSet.TaskTypes, timeTrackDataSet.Clients });

but nothing compiles and the error messages don't get me there.

CodePudding user response:

Option #1

You can create an array in-line and pass it immediately:

timeTrackDataSet.LoadLookups(new[] { timeTrackDataSet.Users, timeTrackDataSet.TaskTypes, timeTrackDataSet.Clients });

Option #2

Or if you can change the original method's signature, you can add params:

public void LoadLookups(params DataTable[] tables)

In that case you either pass an array or pass comma-separated parameters:

DataTable[] tables = { timeTrackDataSet.Users, timeTrackDataSet.TaskTypes, timeTrackDataSet.Clients };
timeTrackDataSet.LoadLookups(tables);

timeTrackDataSet.LoadLookups(timeTrackDataSet.Users, timeTrackDataSet.TaskTypes, timeTrackDataSet.Clients);

CodePudding user response:

Like this:

LoadLookups(new DataTable[] { timeTrackDataSet.Users, timeTrackDataSet.TaskTypes, timeTrackDataSet.Clients });

It looks to me like timeTrackDataSet is a strongly typed dataset, namely that in your project you have a designer and a TimeTrackDataset that you can double click on and see a GUI that looks something like a database table designer. As such, those props you mention (users, tasktypes etc) are derived from DataTable - they're a specific type, but they have a common parent

When you do this:

DataTable[] tables = { timeTrackDataSet.Users, timeTrackDataSet.TaskTypes, timeTrackDataSet.Clients };

The compiler can know "user wants to create an array of DataTable, and sees it with those three things which do all inherit from (and are hence assignable to) DataTable so I will permit it"

And it unrolls the code for you, to something like:

DataTable[] dt = new DataTable[3];
dt[0] = timeTrackDataSet.Users;
dt[1] = ...

However, the compiler can't cope with an attempt where it can't determine the type:

//not valid syntax 
LoadLookups({ timeTrackDataSet.Users, timeTrackDataSet.TaskTypes, timeTrackDataSet.Clients });

//no best type for implicit typed array - 
//because the members are not the same type and there is no conversion
LoadLookups(new [] { timeTrackDataSet.Users, timeTrackDataSet.TaskTypes, timeTrackDataSet.Clients });

So you have to help it out by minimally specifying the type


That (immediately above) said, if you really have derived DataSet yourself and given your subclass 3 props of Users, TaskTypes and Clients that really do all return DataTable, then you can indeed just do LoadLookups(new [] {.

Footnote 2; if Users was of type DataTable then it would also work even if the other entries were derived; implicit typing from an array initializer looks at the type of the first element in the {} and if all the others can be implicitly converted to the type of the first then the creation also succeeds

  •  Tags:  
  • c#
  • Related