Results 1 to 16 of 16

Thread: [2005] File IO Problem

  1. #1

    Thread Starter
    Addicted Member Gonegaming12's Avatar
    Join Date
    Jun 2006
    Location
    USA
    Posts
    131

    [2005] File IO Problem

    In VB6 you could do something like this

    Code:
    Public Class 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
    
            Public Sub New()
                Name = ""
                Moral = 0
                Up = -1
                Down = -1
                Left = -1
                right = -1
                Music = -1
                BootMap = -1
                BootX = 0
                BootY = 0
                Shop = -1
                Indoors = 0
                Owner = ""
                Weather = -1
                lights = -1
            End Sub
    
        End Class
    
    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 = Application.StartupPath & "\maps\map" & MapNum & ".dat"
    
            f = FreeFile()
            FileOpen(f, FileName, OpenMode.Binary)
            FilePutObject(f, Map(MapNum))
            FileClose(f)
        End Sub
    
        Sub LoadMap(ByVal MapNum As Long)
            Dim FileName As String
            Dim f As Long
    
            FileName = Application.StartupPath & "\maps\map" & MapNum & ".dat"
    
            If FileExist("maps\map" & MapNum & ".dat") = False Then Exit Sub
            f = FreeFile()
            FileOpen(f, FileName, OpenMode.Binary)
            FileGetObject(f, Map(MapNum))
            FileClose(f)
        End Sub
    
    End Module
    But this gives me an error in .net, any idea how I can do this. Load a binary file directly to a class? I'll find the error if you guys need.

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: [2005] File IO Problem

    Please post the .NET code that you have, the error being generated and what line is causing it.

  3. #3

    Thread Starter
    Addicted Member Gonegaming12's Avatar
    Join Date
    Jun 2006
    Location
    USA
    Posts
    131

    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

  4. #4
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: [2005] File IO Problem

    Ok, but what is the error and what line is causing it?

  5. #5

    Thread Starter
    Addicted Member Gonegaming12's Avatar
    Join Date
    Jun 2006
    Location
    USA
    Posts
    131

    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))

  6. #6
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    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?
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  7. #7

    Thread Starter
    Addicted Member Gonegaming12's Avatar
    Join Date
    Jun 2006
    Location
    USA
    Posts
    131

    Re: [2005] File IO Problem

    the first post is vb.net code.

  8. #8
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    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.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  9. #9

    Thread Starter
    Addicted Member Gonegaming12's Avatar
    Join Date
    Jun 2006
    Location
    USA
    Posts
    131

    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.

  10. #10
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: [2005] File IO Problem

    What do you mean it didnt work? Post the code you were using and what the error message is.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  11. #11

    Thread Starter
    Addicted Member Gonegaming12's Avatar
    Join Date
    Jun 2006
    Location
    USA
    Posts
    131

    Re: [2005] File IO Problem

    Ohhh man i dont think i saved that code...uhhh let me get it together again...

  12. #12

    Thread Starter
    Addicted Member Gonegaming12's Avatar
    Join Date
    Jun 2006
    Location
    USA
    Posts
    131

    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

  13. #13
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: [2005] File IO Problem

    Code:
    If System.IO.File.Exist(FileName) = False Then Exit Sub
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  14. #14

    Thread Starter
    Addicted Member Gonegaming12's Avatar
    Join Date
    Jun 2006
    Location
    USA
    Posts
    131

    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.

  15. #15
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    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?
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  16. #16

    Thread Starter
    Addicted Member Gonegaming12's Avatar
    Join Date
    Jun 2006
    Location
    USA
    Posts
    131

    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)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width