abbreviated version please

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

hey all,

is there a shorter way to write the following:

System.Data.DataColumn[] keys = new System.Data.DataColumn[1];
// Add the column to the array.
keys[0] = dataTable.Columns[0];
// Set the PrimaryKeys property to the array.
dataTable.PrimaryKey = keys;

thanks,
rodchar
 
rodchar said:
hey all,

is there a shorter way to write the following:

System.Data.DataColumn[] keys = new System.Data.DataColumn[1];
// Add the column to the array.
keys[0] = dataTable.Columns[0];
// Set the PrimaryKeys property to the array.
dataTable.PrimaryKey = keys;

You could put it all on a single line.

dataTable.PrimaryKey = new System.Data.DataColumn[]{dataTable.Columns[0]};
 
thank you very much for the tip. rod.

Tom Porterfield said:
rodchar said:
hey all,

is there a shorter way to write the following:

System.Data.DataColumn[] keys = new System.Data.DataColumn[1];
// Add the column to the array.
keys[0] = dataTable.Columns[0];
// Set the PrimaryKeys property to the array.
dataTable.PrimaryKey = keys;

You could put it all on a single line.

dataTable.PrimaryKey = new System.Data.DataColumn[]{dataTable.Columns[0]};
 
dataTable.Columns[0]
I noticed you specified 0 and in the example i had a 1, is there a difference?

Tom Porterfield said:
rodchar said:
hey all,

is there a shorter way to write the following:

System.Data.DataColumn[] keys = new System.Data.DataColumn[1];
// Add the column to the array.
keys[0] = dataTable.Columns[0];
// Set the PrimaryKeys property to the array.
dataTable.PrimaryKey = keys;

You could put it all on a single line.

dataTable.PrimaryKey = new System.Data.DataColumn[]{dataTable.Columns[0]};
 
rodchar said:
dataTable.Columns[0]
I noticed you specified 0 and in the example i had a 1

Not quite.

Your System.Data.DataColumn[1] explicitly specified the dimensions
DataColumns array, whereas this was implied in Tom's code by virtue of
the fact that only 1 column was passed to the constructor. (Somebody
correct me if I'm using duff terminology!)

Tom still refers to the same dataTable.Columns[0] that you did.
 
rodchar said:
is there a shorter way to write the following:

System.Data.DataColumn[] keys = new System.Data.DataColumn[1];
// Add the column to the array.
keys[0] = dataTable.Columns[0];
// Set the PrimaryKeys property to the array.
dataTable.PrimaryKey = keys;

You could put it all on a single line.

dataTable.PrimaryKey = new
System.Data.DataColumn[]{dataTable.Columns[0]};
dataTable.Columns[0]
I noticed you specified 0 and in the example i had a 1, is there a
difference?

Of course there is, but you're comparing two things that are not the same.

You initialized a new DataColumn array with a length of 1
(System.Data.DataColumn[] keys = new System.Data.DataColumn[1];). You then
set the first element in that array to the first column in your table
(keys[0] = dataTable.Columns[0];).

Since I initialized the array by the DataColumn array by giving it the
actual values to be stored in the array, I could leave the number of
elements off as it will automatically get initialized to the number of
elements between {}.

So the code is exactly the same as yours in what it does, just make sure you
are comparing the correct portion of your code to mine.
 
Thanks once again Tom and everyone for the help.

Tom Porterfield said:
rodchar said:
is there a shorter way to write the following:

System.Data.DataColumn[] keys = new System.Data.DataColumn[1];
// Add the column to the array.
keys[0] = dataTable.Columns[0];
// Set the PrimaryKeys property to the array.
dataTable.PrimaryKey = keys;

You could put it all on a single line.

dataTable.PrimaryKey = new
System.Data.DataColumn[]{dataTable.Columns[0]};
dataTable.Columns[0]
I noticed you specified 0 and in the example i had a 1, is there a
difference?

Of course there is, but you're comparing two things that are not the same.

You initialized a new DataColumn array with a length of 1
(System.Data.DataColumn[] keys = new System.Data.DataColumn[1];). You then
set the first element in that array to the first column in your table
(keys[0] = dataTable.Columns[0];).

Since I initialized the array by the DataColumn array by giving it the
actual values to be stored in the array, I could leave the number of
elements off as it will automatically get initialized to the number of
elements between {}.

So the code is exactly the same as yours in what it does, just make sure you
are comparing the correct portion of your code to mine.
 

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

Back
Top