I am trying to use Buffer.BlockCopy to copy a portion of an array (data.item) into another. I get a compiler error about invalid stuff (see below) if I try one thing, or a runtime error if I use a cast. But it works fine if I just assign it: row[item.AspecName] = "random chars". What am I wrong doing?
namespace ObjectListViewFramework
{
public partial class ObjectListviewForm : Form
{
public DataTable ListRows { get; private set; }
}
}
public void AddRows2(DLL_conduit data)
{
int columnIndex;
int bufferIndex = 0;
for (int rowIndex = 0; rowIndex < data.rowCount; rowIndex++)
{
DataRow row = ListRows.NewRow();
columnIndex = 0;
foreach (var item in objectListView1.AllColumns)
{
// this works dandy
row[item.AspectName] = "random chars";
// compiler error: the best overloaded method match has some invalid args
Buffer.BlockCopy(data.item, bufferIndex, row[item.AspectName], columnIndex * 256, 256);
// runtime error: invalid cast
Buffer.BlockCopy(data.item, bufferIndex, (char[])row[item.AspectName], columnIndex * 256, 256);
++columnIndex;
bufferIndex += 256;
}
ListRows.Rows.Add(row);
}
objectListView1.AddObjects(ListRows.DefaultView);
}
Initially your row doesn't have any associated data.
Buffer.BlockCopy
doesn't create an array to copy into. You have to pass it an existing array, and you don't have one.
Your working code uses an indexed setter. Your non-working code uses the indexed getter to retrieve the object currently in the DataRow
.
Uses of Buffer.BlockCopy
are few and far between.