Array question

  • Thread starter Thread starter Michael Rodriguez
  • Start date Start date
M

Michael Rodriguez

What is the proper way to declare and initialize an array of datasets?
Would it go like this:

private DataSet[] myDataSets;

DataSet ds1 = new DataSet(); // initialize the dataset
DataSet ds2 = new DataSet(); // initialize the dataset

myDataSets = new DataSet[2]; // does this create the array, the datasets,
or both?

myDataSets[0] = ds1;
myDataSets[1] = ds2;

// what is now the difference between these two lines?

ds1.Dispose();
myDataSets[0].Dispose();

TIA,

Mike Rodriguez
 
Michael,

When you do something like this:

// Create the array.
private DataSet[] myDataSets;

All that does is declare the array. For an array of reference types
like this, the elements are all initialized to null. It isn't until you
assign the specific elements that there is something in there to work with.

As for the following statements:

ds1.Dispose();
myDataSets[0].Dispose();

They both do the same thing, since they both point to the same instance
(assigning to an array element only copies the reference to the instance, it
doesn't copy the instance itself).

Hope this helps.
 
Nicholas Paldino said:
Michael,

When you do something like this:

// Create the array.
private DataSet[] myDataSets;

All that does is declare the array. For an array of reference types
like this, the elements are all initialized to null. It isn't until you
assign the specific elements that there is something in there to work
with.

Hi Again, Nicholas <g>

So are you saying this code is the correct form for creating the array and
the datasets:

DataSet ds1 = new DataSet();
DataSet ds2 = new DataSet();

myDataSets = new DataSet[2];

myDataSets[0] = ds1;
myDataSets[1] = ds2;


Thanks!

Mike Rodriguez
 
Michael,

Well, if you wanted to create a two element array and populate each
element with an empty dataset, yes! =)

If you wanted to do it for N number of elements, you could do:

public static DataSet[] CreateAndPopulateDataSetArray(int elements)
{
// If elements is less than zero, then throw an exception.
if (elements < 0)
{
// Throw an exception.
throw new ArgumentException("The elements parameter must be a
non-negative value.", "elements");
}

// Create the array.
DataSet[] retVal = new DataSet[elements];

// Cycle through the elements, and create a new instance.
for (int index = 0; index <= retVal.Length; ++index)
{
// Create a new instance.
retVal[index] = new DataSet();
}

// That's all folks.
return retVal;
}

Michael Rodriguez said:
Nicholas Paldino said:
Michael,

When you do something like this:

// Create the array.
private DataSet[] myDataSets;

All that does is declare the array. For an array of reference types
like this, the elements are all initialized to null. It isn't until you
assign the specific elements that there is something in there to work
with.

Hi Again, Nicholas <g>

So are you saying this code is the correct form for creating the array and
the datasets:

DataSet ds1 = new DataSet();
DataSet ds2 = new DataSet();

myDataSets = new DataSet[2];

myDataSets[0] = ds1;
myDataSets[1] = ds2;


Thanks!

Mike Rodriguez
 
Nicholas Paldino said:
Michael,

public static DataSet[] CreateAndPopulateDataSetArray(int elements)
{
// If elements is less than zero, then throw an exception.
if (elements < 0)
{
// Throw an exception.
throw new ArgumentException("The elements parameter must be a
non-negative value.", "elements");
}

// Create the array.
DataSet[] retVal = new DataSet[elements];

// Cycle through the elements, and create a new instance.
for (int index = 0; index <= retVal.Length; ++index)
{
// Create a new instance.
retVal[index] = new DataSet();
}

// That's all folks.
return retVal;
}

That's what I needed to know! Thanks, Nicholas.
 
Michael,

This will give you an array with two empty DataSets. If that's what you
want, then this would be more efficient:

private DataSet[] myDataSets = new DataSet[2];
....
myDataSets[0] = new DataSet();
myDataSets[1] = new DataSet();

There is generally no point in housing them in an array *and* then having
separate variables pointing to the same objects.

--Bob


Michael Rodriguez said:
Nicholas Paldino said:
Michael,

When you do something like this:

// Create the array.
private DataSet[] myDataSets;

All that does is declare the array. For an array of reference types
like this, the elements are all initialized to null. It isn't until you
assign the specific elements that there is something in there to work
with.

Hi Again, Nicholas <g>

So are you saying this code is the correct form for creating the array and
the datasets:

DataSet ds1 = new DataSet();
DataSet ds2 = new DataSet();

myDataSets = new DataSet[2];

myDataSets[0] = ds1;
myDataSets[1] = ds2;


Thanks!

Mike Rodriguez
 
Bob Grommes said:
Michael,

This will give you an array with two empty DataSets. If that's what you
want, then this would be more efficient:

private DataSet[] myDataSets = new DataSet[2];
...
myDataSets[0] = new DataSet();
myDataSets[1] = new DataSet();

There is generally no point in housing them in an array *and* then having
separate variables pointing to the same objects.

--Bob

Hi Bob,

You make a valid point. However, in my case the array will hold datasets
that have already been created elsewhere.

Thanks,

Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Comparing Dataset Question 5
about using BindingSource 3
Event not firing 6
Simple Binding 2
serialize DataSet and use WriteXml 0
XML and Dataset 2
Memory question 11
TableAdapter Merge issue 2

Back
Top