Getting overflow in vb.net

Recommended Posts

ed209    122
I'm working on a chip 8 emulator and i'm trying to read in a rom into memory but I keep getting an overflow. I'm reading one character at a time into an array of bytes.
Public Class Form1
Dim ram(&HFFF) As Byte

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

End Sub

Dim ofile As IO.File
Dim i As Integer
i = 0

i += 1
End While

End Sub
End Class
[Edited by - ed209 on March 3, 2008 9:31:42 AM]

Share on other sites
benryves    1999
A number of problems here; you open the stream as text (the files you are reading are binary); you do not dispose the opened stream; no bounds checking on i.

Rather than correct those, there are methods in the class library that make your life easier, and so here's a simpler version! [smile]

Sub ReadFile(ByVal filename As String)	' Clear the contents of Ram.	Array.Clear(Ram, 0, Ram.Length)	' Now read the entire source file into an array of bytes.	Dim ProgramFile As Byte() = IO.File.ReadAllBytes(filename)	' The largest program file we can fit in memory is between address &H200 and &HFFF.	' This is a maximum of &HE00 bytes, which is 3584.	' In case the specified file is too large, resize it:	If ProgramFile.Length > 3584 Then		Array.Resize(ProgramFile, 3584)	End If	' Finally, we need to copy our program to &H200 in memory.	Array.Copy(ProgramFile, 0, Ram, &H200, ProgramFile.Length)End Sub