0

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.

asked Jan 2, 2022 at 19:46

2 Answers 2

3

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);
answered Jan 2, 2022 at 19:49
1
  • new didn't make sense to me at first because the tables already exist but then I realized what was being created new was the array itself. The params solution looks even better. Commented Jan 2, 2022 at 22:22
1

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

answered Jan 2, 2022 at 21:26

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.