Re: [2005] File IO Problem
Please post the .NET code that you have, the error being generated and what line is causing it.
Re: [2005] File IO Problem
that is the .net code, it is the equilent of the vb6.
Code:
Public type MapRec
Public Name As String
Public Moral As Byte
Public Up As Long
Public Down As Long
Public Left As Long
Public right As Long
Public Music As String
Public BootMap As Long
Public BootX As Byte
Public BootY As Byte
Public Shop As Long
Public Indoors As Byte
Public Tile(0 To Max_mapwidth, 0 To Max_mapheight) As TileRec
Public Npc(0 To MAX_MAP_NPCS) As Long
Public Owner As String
Public Weather As Long
Public lights As Long
End type
Module MapData
Public Map(0 To MAX_MAPS) As MapRec
Sub SaveLocalMap(ByVal MapNum As Long)
Dim FileName As String
Dim f As Long
FileName = App.Path & "\maps\map" & MapNum & ".dat"
f = FreeFile
Open FileName For Binary As #f
Put #f, , Map(MapNum)
Close #f
End Sub
Sub LoadMap(ByVal MapNum As Long)
Dim FileName As String
Dim f As Long
FileName = App.Path & "\maps\map" & MapNum & ".dat"
If FileExist("maps\map" & MapNum & ".dat") = False Then Exit Sub
f = FreeFile
Open FileName For Binary As #f
Get #f, , Map(MapNum)
Close #f
End Sub
Re: [2005] File IO Problem
Ok, but what is the error and what line is causing it?
Re: [2005] File IO Problem
Error: Variable uses an Automation type not supported in Visual Basic.
Sub: LoadMap(ByVal MapNum As Long)
Line: FileGetObject(f, Map(MapNum))
Re: [2005] File IO Problem
That is not VB.NET code at all. It is VB 6. Are you using VB 6 or VB.NET?
Re: [2005] File IO Problem
the first post is vb.net code.
Re: [2005] File IO Problem
Oops, ok, but its still using allot of VB 6 code in it.
Do you really need the argument as a Long? In .NET a Integer is now what a VB 6 Long was.
Also Freefile and Basic File I/O should not be used, its VB 6 code.
Use the System.IO class instead.
Re: [2005] File IO Problem
how would i do it using System.IO, i tried using the binary reader/writer but that didn't work.
Re: [2005] File IO Problem
What do you mean it didnt work? Post the code you were using and what the error message is.
Re: [2005] File IO Problem
Ohhh man i dont think i saved that code...uhhh let me get it together again...
Re: [2005] File IO Problem
i only did loadfile
Code:
Sub LoadMap(ByVal MapNum As Integer)
Dim FileName As String
FileName = Application.StartupPath & "\maps\map" & MapNum & ".dat"
If FileExist(FileName) = False Then Exit Sub
Dim Stream As New IO.FileStream(FileName, IO.FileMode.Open)
Dim Reader As New IO.BinaryReader(Stream, System.Text.Encoding.Unicode)
* Map(MapNum) = Reader.ReadBytes(Stream.Length) *
End Sub
I get an error saying cannot convert 1 dimensional array of bytes to maprec
i have also tried
Code:
Map(MapNum) = Reader.Read
but that dont work either
Re: [2005] File IO Problem
Code:
If System.IO.File.Exist(FileName) = False Then Exit Sub
Re: [2005] File IO Problem
thats not the line i have the error on but ya, thats true. Its hard converting 10,000 lines of code from vb6 to .net so there are some things like that.
Re: [2005] File IO Problem
Well I just want to start from the code execution on down. So the next line will be ....
Code:
Map(MapNum) = Reader.ReadBytes(Stream.Length)
So to convert this, its basically just a class with some variables. Seems like you are trying to read a file and populate the class at the same time?
Re: [2005] File IO Problem
exactly, i'm trying to read the file straight into the class, and vise-versa for the savelocalmap method.
I read around the internet about this and I think i have it, i should use serialization with my classes.
Code:
Sub LoadMap(ByVal MapNum As Integer)
Dim FileName As String
FileName = Application.StartupPath & "\maps\map" & MapNum & ".dat"
If FileExist(FileName) = False Then Exit Sub
Dim input As IO.FileStream
Dim reader As Runtime.Serialization.Formatters.Binary.BinaryFormatter = New Runtime.Serialization.Formatters.Binary.BinaryFormatter()
input = New IO.FileStream(FileName, IO.FileMode.Open, IO.FileAccess.Read)
Try
Map(MapNum) = CType(reader.Deserialize(input), MapRec)
Catch serializableException As Runtime.Serialization.SerializationException
input.Close()
End Try
input.Close()
End Sub
thats my code now, but im getting an error, let me bring it up again...just a sec...
The input stream is not a valid binary format. The starting contents (in bytes) are: 20-20-20-20-20-20-20-20-20-20-20-20-20-20-20-20-20 ...
for line
Code:
Map(MapNum) = CType(reader.Deserialize(input), MapRec)