Skip to main content
GameDev.net gamedev.net
🔒 Locked

C# Array bounds issue

Started by Dissolution Feb 7, 2006 at 1:46 PM 1 replies 6.4k views
Original Post
Dissolution
Dissolution
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?
Agent1
Agent1
The documentation is a bit confusing. The "offset" is the offset in the array that you supply, not the file you're reading.
Dissolution
Dissolution
Ah, that helps a lot, thank you.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.