Original Post
I'm trying to read data from a file into an array of bytes, but I keep getting the following error: ----------- An unhandled exception of type 'System.ArgumentException' occurred in mscorlib.dll Additional information: Offset and length were out of bounds for the array or count is greater than the number of elements from index to the end of the source collection. ----------- Here's the code:
// Menu data structure
struct SEntry
{
public byte[] entryData;
public int tabStop;
public string buttonName;
}
// Menu data structure array
SEntry[] menuData = new SEntry[40];
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
// Initialize swap button
swap1 = new Button();
// Initialize menu data array
for (int i = 0; i < 40; i++)
{
menuData.entryData = new byte[7524];
menuData.tabStop = 0;
menuData.buttonName = " ";
}
}
// LoadMenuData //
private void LoadMenuData()
{
FileStream fileData;
BinaryReader readFile;
byte[] tempBytes = new byte[30];
Button tempButton = new Button();
// Display the open file dialog
openFileDialog.ShowDialog();
fileData = new FileStream(openFileDialog.FileName, FileMode.Open);
readFile = new BinaryReader(fileData);
for (int i = 0; i < 40; i++)
{
tempButton = (Button)this.GetNextControl(button1, true);
readFile.Read(menuData.entryData,i*7524,7524); // <-- error here
menuData.tabStop = i+1;
menuData.buttonName = tempBytes.ToString();
tempButton = (Button)this.GetNextControl(button1,true);
tempButton.Text = menuData.buttonName;
}
fileData.Close();
}
I've checked the array sizes using menuData.GetUpperBound() and menuData.entryData.GetUpperBound(), both gave me the right numbers. Why is it giving me a bounds error?