Results 1 to 29 of 29

Thread: vb 2010 unbalanced stack

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jan 2011
    Posts
    118

    vb 2010 unbalanced stack

    Hi,

    Im getting an error if i try to read the file with 'GetVar' when I click on opentoolstripmenu item.

    How to I fix this?

    this is my error:
    Code:
    A call to PInvoke function 'Map Editor!Map_Editor.SaveMap::GetPrivateProfileString' has unbalanced the stack. This is likely because the managed PInvoke signature does not match the unmanaged target signature. Check that the calling convention and parameters of the PInvoke signature match the target unmanaged signature.
    this is the code:
    Code:
        Private Sub OpenToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles OpenToolStripMenuItem.Click
    
            'LoadMap(InputBox("Please enter the path you want to open.", "Load Map", "\Maps\map1.map"))
    
            Call PutVar("Test.ini", "TEST", "what", "hell5o")
    
            MsgBox(GetVar(Windows.Forms.Application.StartupPath & "/test.ini", "TEST", "what"))
    
        End Sub
    this is the sub:
    Code:
        
    
     ' Text API
        Private Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationname As String, ByVal lpKeyname As String, ByVal lpString As String, ByVal lpfilename As String) As Long
        Private Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationname As String, ByVal lpKeyname As String, ByVal lpdefault As String, ByVal lpreturnedstring As String, ByVal nsize As Long, ByVal lpfilename As String) As Long
    
    Public Function GetVar(File As String, Header As String, Var As String) As String
            Dim sSpaces As String   ' Max string length
            Dim szReturn As String  ' Return default value if not found
            szReturn = vbNullString
            sSpaces = Space$(5000)
            Call GetPrivateProfileString((Header), Var, szReturn, sSpaces, Len(sSpaces), File)
            GetVar = RTrim$(sSpaces)
            GetVar = Left$(GetVar, Len(GetVar) - 1)
        End Function
    
        ' writes a variable to a text file
        Public Sub PutVar(File As String, Header As String, Var As String, Value As String)
            Call WritePrivateProfileString((Header), Var, Value, Windows.Forms.Application.StartupPath & "/" & File)
        End Sub

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: vb 2010 unbalanced stack

    You look like you're trying to write VB6 code in VB.NET. I suggest that you forget VB6 and start using VB.NET "properly". Your specific issue in this case is the fact that you're using API declarations intended for VB6. API functions almost always use 32-bit numbers, which was type Long in VB6 but is type Integer in VB.NET.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jan 2011
    Posts
    118

    Re: vb 2010 unbalanced stack

    Quote Originally Posted by jmcilhinney View Post
    You look like you're trying to write VB6 code in VB.NET. I suggest that you forget VB6 and start using VB.NET "properly". Your specific issue in this case is the fact that you're using API declarations intended for VB6. API functions almost always use 32-bit numbers, which was type Long in VB6 but is type Integer in VB.NET.
    How can I make something like this in vb 2010?
    with the streamwriters I can't make something clear as I can with this functions in vb6 because I can arrange my files with headers.

    my files will look like this:
    Code:
    [MAP]
    name=testmap
    x=20
    y=20
    
    [1]
    default=true
    
    [2]
    default=true
    
    [3]
    default=true

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: vb 2010 unbalanced stack

    You're using a ridiculously outdated method of storing settings. This is from the documentation for the WritePrivateProfileString function:
    This function is provided only for compatibility with 16-bit versions of Windows. Applications should store initialization information in the registry.
    Even using the registry has been outdated for several years now, so using something designed for 16-bit Windows is positively archaic.

    If you must retain that file structure then use StreamReaders and StreamWriters. Read the whole lot in, process it, then write the whole lot out. I'd sooner use XML or simply My.Settings though.

  5. #5
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,600

    Re: vb 2010 unbalanced stack

    What kind of information do you intend to store ? Its it configuration settings ? Data records ?

    Before suggesting an modern alternative, I'd like to know the nature of the data.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Jan 2011
    Posts
    118

    Re: vb 2010 unbalanced stack

    Quote Originally Posted by Niya View Post
    What kind of information do you intend to store ? Its it configuration settings ? Data records ?

    Before suggesting an modern alternative, I'd like to know the nature of the data.
    i want to store map information (for my 2D-game) in the files.

  7. #7
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,600

    Re: vb 2010 unbalanced stack

    Ah....ok, so does the data you intend to save come from objects ? Or an array ? How do you store the information in memory ?
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Jan 2011
    Posts
    118

    Re: vb 2010 unbalanced stack

    With structures;

    Public const max_layers = 4
    Public const max_x = 200
    Public const max_y = 200
    Public tile(max_layers, max_x, max_y) as tilerec

    Public structure tilerec

    Public x as integer
    Public y as integer
    Public data as integer
    Public name as string

    End structure

    The data Should be saved for each tile
    so you'll have loads of information in the file.
    Do you understand?

  9. #9
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,600

    Re: vb 2010 unbalanced stack

    Well my friend the solution is far more simple than you realize:-
    vbnet Code:
    1. Public Class MapInfo
    2.  
    3.     Private _colTiles As New Collections.ObjectModel.Collection(Of tilerec)
    4.  
    5.     Public Property Tiles() As Collections.ObjectModel.Collection(Of tilerec)
    6.         Get
    7.             Return _colTiles
    8.         End Get
    9.         Set(ByVal value As Collections.ObjectModel.Collection(Of tilerec))
    10.             _colTiles = value
    11.         End Set
    12.     End Property
    13.  
    14.  
    15. End Class

    You can create a class like the above and use XML serialization to save and load:-
    vbnet Code:
    1. '
    2.     Public Function LoadMapInfo(ByVal fileName As String) As MapInfo
    3.         Dim ser As New XmlSerializer(GetType(MapInfo))
    4.         Dim fs As New FileStream(fileName, FileMode.Open)
    5.         Dim ret As MapInfo
    6.  
    7.         ret = ser.Deserialize(fs)
    8.  
    9.         fs.Close()
    10.         fs.Dispose()
    11.  
    12.         Return ret
    13.  
    14.     End Function
    15.  
    16.     Public Sub SaveMapInfo(ByVal map As MapInfo, ByVal fileName As String)
    17.         Dim ser As New XmlSerializer(GetType(MapInfo))
    18.         Dim fs As New FileStream(fileName, FileMode.Create)
    19.  
    20.         ser.Serialize(fs, map)
    21.  
    22.         fs.Close()
    23.         fs.Dispose()
    24.     End Sub

    The above functions can load and save the class to a file and load it back. Here is an example of their use:-
    vbnet Code:
    1. '
    2.         Dim mi As New MapInfo
    3.  
    4.         mi.Tiles.Add(New tilerec With {.data = 12, .name = "Unknown", .x = 11, .y = 0})
    5.         mi.Tiles.Add(New tilerec With {.data = 11, .name = "Bing", .x = 12, .y = 4})
    6.         mi.Tiles.Add(New tilerec With {.data = 122, .name = "True", .x = 40, .y = 155})
    7.         mi.Tiles.Add(New tilerec With {.data = 3000, .name = "GRRR...!!", .x = 30, .y = 122})
    8.  
    9.         SaveMapInfo(mi, "c:\MI.TXT")
    10.  
    11.  
    12.         Dim mi2 As MapInfo = LoadMapInfo("C:\MI.TXT")

    The saved data would look like this:-
    xml Code:
    1. <?xml version="1.0"?>
    2. <MapInfo xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    3.   <Tiles>
    4.     <tilerec>
    5.       <x>11</x>
    6.       <y>0</y>
    7.       <data>12</data>
    8.       <name>Unknown</name>
    9.     </tilerec>
    10.     <tilerec>
    11.       <x>12</x>
    12.       <y>4</y>
    13.       <data>11</data>
    14.       <name>Bing</name>
    15.     </tilerec>
    16.     <tilerec>
    17.       <x>40</x>
    18.       <y>155</y>
    19.       <data>122</data>
    20.       <name>True</name>
    21.     </tilerec>
    22.     <tilerec>
    23.       <x>30</x>
    24.       <y>122</y>
    25.       <data>3000</data>
    26.       <name>GRRR...!!</name>
    27.     </tilerec>
    28.   </Tiles>
    29. </MapInfo>

    However, games generally don't save their map data in a text format like Xml. A binary version of the same data would be far smaller in size and much faster in IO operations. You should consider that, its not absolutely necessary, its just more efficient. It should only take a mild alteration to the above code to make it serialize as a binary file instead of Xml.
    Last edited by Niya; Sep 16th, 2012 at 02:25 PM.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  10. #10
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: vb 2010 unbalanced stack

    Quote Originally Posted by Niya View Post
    Well my friend the solution is far more simple than you realize:-
    vbnet Code:
    1. Public Class MapInfo
    2.  
    3.     Private _colTiles As New Collections.ObjectModel.Collection(Of tilerec)
    4.  
    5.     Public Property Tiles() As Collections.ObjectModel.Collection(Of tilerec)
    6.         Get
    7.             Return _colTiles
    8.         End Get
    9.         Set(ByVal value As Collections.ObjectModel.Collection(Of tilerec))
    10.             _colTiles = value
    11.         End Set
    12.     End Property
    13.  
    14.  
    15. End Class

    You can create a class like the above and use XML serialization to save and load:-
    vbnet Code:
    1. '
    2.     Public Function LoadMapInfo(ByVal fileName As String) As MapInfo
    3.         Dim ser As New XmlSerializer(GetType(MapInfo))
    4.         Dim fs As New FileStream(fileName, FileMode.Open)
    5.         Dim ret As MapInfo
    6.  
    7.         ret = ser.Deserialize(fs)
    8.  
    9.         fs.Close()
    10.         fs.Dispose()
    11.  
    12.         Return ret
    13.  
    14.     End Function
    15.  
    16.     Public Sub SaveMapInfo(ByVal map As MapInfo, ByVal fileName As String)
    17.         Dim ser As New XmlSerializer(GetType(MapInfo))
    18.         Dim fs As New FileStream(fileName, FileMode.Create)
    19.  
    20.         ser.Serialize(fs, map)
    21.  
    22.         fs.Close()
    23.         fs.Dispose()
    24.     End Sub

    The above functions can load and save the class to a file and load it back. Here is an example of their use:-
    vbnet Code:
    1. '
    2.         Dim mi As New MapInfo
    3.  
    4.         mi.Tiles.Add(New tilerec With {.data = 12, .name = "Unknown", .x = 11, .y = 0})
    5.         mi.Tiles.Add(New tilerec With {.data = 11, .name = "Bing", .x = 12, .y = 4})
    6.         mi.Tiles.Add(New tilerec With {.data = 122, .name = "True", .x = 40, .y = 155})
    7.         mi.Tiles.Add(New tilerec With {.data = 3000, .name = "GRRR...!!", .x = 30, .y = 122})
    8.  
    9.         SaveMapInfo(mi, "c:\MI.TXT")
    10.  
    11.  
    12.         Dim mi2 As MapInfo = LoadMapInfo("C:\MI.TXT")

    The saved data would look like this:-
    xml Code:
    1. <?xml version="1.0"?>
    2. <MapInfo xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    3.   <Tiles>
    4.     <tilerec>
    5.       <x>11</x>
    6.       <y>0</y>
    7.       <data>12</data>
    8.       <name>Unknown</name>
    9.     </tilerec>
    10.     <tilerec>
    11.       <x>12</x>
    12.       <y>4</y>
    13.       <data>11</data>
    14.       <name>Bing</name>
    15.     </tilerec>
    16.     <tilerec>
    17.       <x>40</x>
    18.       <y>155</y>
    19.       <data>122</data>
    20.       <name>True</name>
    21.     </tilerec>
    22.     <tilerec>
    23.       <x>30</x>
    24.       <y>122</y>
    25.       <data>3000</data>
    26.       <name>GRRR...!!</name>
    27.     </tilerec>
    28.   </Tiles>
    29. </MapInfo>

    However, games generally don't save their map data in a text format like Xml. A binary version of the same data would be far smaller in size and much faster in IO operations. You should consider that, its not absolutely necessary, its just more efficient. It should only take a mild alteration to the above code to make it serialize as a binary file instead of Xml.
    Tiles should be a readonly property

  11. #11
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,600

    Re: vb 2010 unbalanced stack

    The XmlSerializer doesn't save read only properties. It only saves public read/write properties. The BinaryFormatter can though. It serializes private fields unlike the XmlSerializer.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  12. #12

    Re: vb 2010 unbalanced stack

    If you really have to use INI files (which is archaic and outdated when there is XML, binary serialization, and the settings file to store information (which is technically an XML file anyway)), then use a managed library that already handles the work for you.

    I highly suggest you use Niya's code; it's simple, efficient, and does exactly what you need.

  13. #13

    Thread Starter
    Lively Member
    Join Date
    Jan 2011
    Posts
    118

    Re: vb 2010 unbalanced stack

    I will get an error and an implicit conversion

    Name:  Naamloos.png
Views: 1908
Size:  116.3 KB

  14. #14
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: vb 2010 unbalanced stack

    1/ where is the TileRec structure? it's a scope error
    2/ try: directcast(ser.deserialize(fs), MapInfo)

  15. #15
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: vb 2010 unbalanced stack

    1/ where is the TileRec structure? it's a scope error
    2/ try: directcast(ser.deserialize(fs), MapInfo)

  16. #16

    Thread Starter
    Lively Member
    Join Date
    Jan 2011
    Posts
    118

    Re: vb 2010 unbalanced stack

    Quote Originally Posted by .paul. View Post
    1/ where is the TileRec structure? it's a scope error
    2/ try: directcast(ser.deserialize(fs), MapInfo)
    my tilerec structure is in the 'module structures' so I can access it from everywhere.

    Code:
      Private Sub picMap_MouseMove(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles picMap.MouseMove
    
            Dim X As Integer = CInt(Math.Floor(e.X / 32))
            Dim Y As Integer = CInt(Math.Floor(e.Y / 32))
    
            If X < 0 Or Y < 0 Then Exit Sub
    
            Select Case GetEditMode()
    
                Case EditMode.Paint
    
                    If e.Button = Windows.Forms.MouseButtons.Left Then
    
                        With Tile(GetLayer, X, Y)
                            .TileSet = CInt(Me.nudTileset.Value)
                            .X = Mouse.tX
                            .Y = Mouse.tY
                            .Draw = True
                        End With
    
                    End If
    
                    If e.Button = Windows.Forms.MouseButtons.Right Then
    
                        With Tile(GetLayer, X, Y)
                            .TileSet = 0
                            .X = 0
                            .Y = 0
                            .Draw = False
                        End With
    
                    End If
    
                Case EditMode.Data
    
            End Select
    
        End Sub
    If I put the TileRec structure in the MapInfo class Ill get other errors because the TileRec does not exist

  17. #17
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,600

    Re: vb 2010 unbalanced stack

    The error you're getting about the type being exposed is a scope error as paul suggests. My guess is that you declared the TileRec structure as Friend and MapInfo as Public which would cause that error. Changing TileRec to a Public declaration would fix that problem.

    Paul already told you how to fix the implicit conversion error.

    EDIT

    Actually, I just remembered that modules default to Friend access in VB.Net which means that anything declared in it cannot be exposed outside the assembly. Declare your module as Public.
    Last edited by Niya; Sep 17th, 2012 at 03:10 PM.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  18. #18

    Thread Starter
    Lively Member
    Join Date
    Jan 2011
    Posts
    118

    Re: vb 2010 unbalanced stack

    Quote Originally Posted by Niya View Post
    The error you're getting about the type being exposed is a scope error as paul suggests. My guess is that you declared the TileRec structure as Friend and MapInfo as Public which would cause that error. Changing TileRec to a Public declaration would fix that problem.

    Paul already told you how to fix the implicit conversion error.

    EDIT

    Actually, I just remembered that modules default to Friend access in VB.Net which means that anything declared in it cannot be exposed outside the assembly. Declare your module as Public.
    I'm sorry I didnt really get it.
    this is my ClassMapinfo and my module structures. What do I need to change in the Module structures -> TileRec so it will work?


    Code:
    Imports System.IO
    Imports System.Xml.Serialization
    
    Public Class MapInfo
    
        Private _colTiles As New Collections.ObjectModel.Collection(Of TileRec)
    
        Public Property Tiles() As Collections.ObjectModel.Collection(Of TileRec)
            Get
                Return _colTiles
            End Get
            Set(ByVal value As Collections.ObjectModel.Collection(Of TileRec))
                _colTiles = value
            End Set
        End Property
    
        Public Function LoadMapInfo(ByVal fileName As String) As MapInfo
            Dim ser As New XmlSerializer(GetType(MapInfo))
            Dim fs As New FileStream(fileName, FileMode.Open)
            Dim ret As MapInfo
    
            ret = DirectCast(ser.Deserialize(fs), MapInfo)
    
            fs.Close()
            fs.Dispose()
    
            Return ret
    
        End Function
    
        Public Sub SaveMapInfo(ByVal map As MapInfo, ByVal fileName As String)
            Dim ser As New XmlSerializer(GetType(MapInfo))
            Dim fs As New FileStream(fileName, FileMode.Create)
    
            ser.Serialize(fs, map)
    
            fs.Close()
            fs.Dispose()
        End Sub
    
    End Class
    Code:
    Module Structures
    
        Public MapEditor As MapEditorOptions
    
        Public IsRunning As Boolean = False
    
        Public Tile(MAX_LAYERS, 0 To 200, 0 To 200) As TileRec
        Public Map As MapRec
    
        Public Mouse As MouseRec
    
        Public View As ViewRec
    
        Public Structure ViewRec
    
            Public Ground As Boolean
            Public Mask As Boolean
            Public Mask2 As Boolean
            Public Fringe As Boolean
            Public Fringe2 As Boolean
    
            Public Data As Boolean
    
        End Structure
    
        Public Structure MouseRec
    
            'Mouse Down
            Public tX As Integer
            Public tY As Integer
    
            'Mouse Hover
            Public thX As Integer
            Public thY As Integer
    
            'Mouse Up
            Public tuX As Integer
            Public tuY As Integer
    
        End Structure
    
        Public Structure MapRec
    
            Public X As Integer
            Public Y As Integer
    
        End Structure
    
        Public Structure TileRec
    
            Public X As Integer
            Public Y As Integer
            Public TileSet As Integer
    
            Public Draw As Boolean
    
            Public Data As TileType
    
        End Structure
    
        Public Structure MapEditorOptions
    
            Public Map_Grid As Boolean
            Public Tile_Grid As Boolean
    
        End Structure
    
    End Module
    edit:

    ah I need to change 'Module Structures' to 'Public Module Structures'

  19. #19
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,600

    Re: vb 2010 unbalanced stack

    Quote Originally Posted by jimpie View Post
    ah I need to change 'Module Structures' to 'Public Module Structures'
    Yep.......
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  20. #20

    Thread Starter
    Lively Member
    Join Date
    Jan 2011
    Posts
    118

    Re: vb 2010 unbalanced stack

    Quote Originally Posted by jimpie View Post
    I'm sorry I didnt really get it.
    this is my ClassMapinfo and my module structures. What do I need to change in the Module structures -> TileRec so it will work?


    Code:
    Imports System.IO
    Imports System.Xml.Serialization
    
    Public Class MapInfo
    
        Private _colTiles As New Collections.ObjectModel.Collection(Of TileRec)
    
        Public Property Tiles() As Collections.ObjectModel.Collection(Of TileRec)
            Get
                Return _colTiles
            End Get
            Set(ByVal value As Collections.ObjectModel.Collection(Of TileRec))
                _colTiles = value
            End Set
        End Property
    
        Public Function LoadMapInfo(ByVal fileName As String) As MapInfo
            Dim ser As New XmlSerializer(GetType(MapInfo))
            Dim fs As New FileStream(fileName, FileMode.Open)
            Dim ret As MapInfo
    
            ret = DirectCast(ser.Deserialize(fs), MapInfo)
    
            fs.Close()
            fs.Dispose()
    
            Return ret
    
        End Function
    
        Public Sub SaveMapInfo(ByVal map As MapInfo, ByVal fileName As String)
            Dim ser As New XmlSerializer(GetType(MapInfo))
            Dim fs As New FileStream(fileName, FileMode.Create)
    
            ser.Serialize(fs, map)
    
            fs.Close()
            fs.Dispose()
        End Sub
    
    End Class
    Code:
    Module Structures
    
        Public MapEditor As MapEditorOptions
    
        Public IsRunning As Boolean = False
    
        Public Tile(MAX_LAYERS, 0 To 200, 0 To 200) As TileRec
        Public Map As MapRec
    
        Public Mouse As MouseRec
    
        Public View As ViewRec
    
        Public Structure ViewRec
    
            Public Ground As Boolean
            Public Mask As Boolean
            Public Mask2 As Boolean
            Public Fringe As Boolean
            Public Fringe2 As Boolean
    
            Public Data As Boolean
    
        End Structure
    
        Public Structure MouseRec
    
            'Mouse Down
            Public tX As Integer
            Public tY As Integer
    
            'Mouse Hover
            Public thX As Integer
            Public thY As Integer
    
            'Mouse Up
            Public tuX As Integer
            Public tuY As Integer
    
        End Structure
    
        Public Structure MapRec
    
            Public X As Integer
            Public Y As Integer
    
        End Structure
    
        Public Structure TileRec
    
            Public X As Integer
            Public Y As Integer
            Public TileSet As Integer
    
            Public Draw As Boolean
    
            Public Data As TileType
    
        End Structure
    
        Public Structure MapEditorOptions
    
            Public Map_Grid As Boolean
            Public Tile_Grid As Boolean
    
        End Structure
    
    End Module
    edit:

    ah I need to change 'Module Structures' to 'Public Module Structures'
    The code can save the tilerec now, that's right.. But it can't save the data for each diffrent tile. All my tiles will have the same data now




    Code:
            For x = 0 To Map.X
                For y = 0 To Map.Y
                    mi.Tiles.Add(New TileRec With {.X = 1, .Y = 1, .TileSet = 1, .Draw = True, .Data = 0})
                Next
            Next
    it should specially save the Tile(Layer type, X of the map, Y of the map) variable. Now it just saves the same data for each layer type, X of the map and Y of the map.


    Code:
            For x = 0 To Map.X
                For y = 0 To Map.Y
                    Tile(LAYER_GROUND, x, y).X = 1
                    Tile(LAYER_GROUND, x, y).Y = 1
                    Tile(LAYER_GROUND, x, y).Data = 1
                Next
            Next

  21. #21
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,600

    Re: vb 2010 unbalanced stack

    Ummm...I'm really confused now...could you simplify that explanation.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  22. #22

    Thread Starter
    Lively Member
    Join Date
    Jan 2011
    Posts
    118

    Re: vb 2010 unbalanced stack

    Quote Originally Posted by Niya View Post
    Ummm...I'm really confused now...could you simplify that explanation.
    Each map has a MapRec (Map Structure). Each map also have tile's.

    A tile is the coordinate of the map (example tile x=5, y=5). Each Tile hold its own information (the X IN the image it should draw, the Y IN the image it should draw, Data with information about the tile e.g. is it a walkable tile or not).

    The tile information is hold in the TileRec structure.

    Code:
    Public Map as MapRec
    public Tile(0 To MAX_LAYERS, 0 To MAX_X, 0 to MAX_Y)
    
    public CONST MAX_LAYERS = 1 'the ammount of layers
    public const MAX_X = 200 'the maximum size of the map (each map can have its own size with a maximum of x = 200)
    public const MAX_Y = 200 'the maximum size of the map (each map can have its own size with a maximum of y = 200)
    
    public CONST LAYER_GROUND = 0 'the first layer
    public const LAYER_MASK = 1 'the second layer
    
    Public Structure MapRec
    
    Public X as integer 'The max X of the map
    public Y as itneger 'The max Y of the map
    public Name as string 'The name of the map
    
    End Structure
    
    public structure TileRec
    
    Public X as integer
    public Y as integer
    public Data as Integer
    
    end structure
    The Map AND Tile information should be saved to a file. Something like this

    Code:
      
    public sub SaveMap(Byval FileName as string)
    
    'first it should save the map data for when loading the map back
    
    Map.X 'here it should write the Map.X to the file
    Map.Y 'here it should write  the Map.Y to the file
    Map.Name 'here it should write  the Map.Name to the file
    
    
    'here it should save the tiledata (the tilerec)
     For x = 0 To Map.X '0 to the max x of the map so it will save ALL tiles
                For y = 0 To Map.Y '0 to the max y of the map so it will save ALL tiles
    For L = 0 to Max_Layers '0 to the max layers to save each layer
    
    tile(L, X, Y).X 'here it should write the variable X of tile(<layer>, <x>, <y>).X to the file
    tile(L, X, Y).Y 'here it should write the variable Y of tile(<layer>, <x>, <y>).Y to the file
    tile(L, X, Y).Data 'here it should write the variable Data of tile(<layer>, <x>, <y>).Data to the file
    
    Next
                Next
            Next
    
    end sub
    
    public sub LoadMap(Byval FileName as string)
    
    'first it should read the Map.X and the Map.Y to know the size so:
    Map.X = Read... 'read the file for the Map.X
    Map.Y = Read... 'read the file for the Map.Y
    
    'now it should load all tile data and put all the data in the correct tile
    for x = 0 to map.x
    for y = 0 to map.y
    for l = 0 to max_layers
    
    tile(l, x, y).X = read.. 'read the X variable of tile(<layer>, <x>, <y>).X
    tile(l, x, y).Y = read.. 'read the Y variable of tile(<layer>, <x>, <y>).Y
    tile(l, x, y).Data = read.. 'read the Data variable of tile(<layer>, <x>, <y>).Data
    
    next
    next
    next
    
    end sub
    Do you understand it now?

  23. #23
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,600

    Re: vb 2010 unbalanced stack

    Ok...so MapRec is meant to hold all the information about a map including the tiles that make it up ? If so then why isn't the TileRec array defined inside the MapRec structure ?
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  24. #24

    Thread Starter
    Lively Member
    Join Date
    Jan 2011
    Posts
    118

    Re: vb 2010 unbalanced stack

    Quote Originally Posted by Niya View Post
    Ok...so MapRec is meant to hold all the information about a map including the tiles that make it up ? If so then why isn't the TileRec array defined inside the MapRec structure ?
    because structures can't have initial sizes

    Code:
        Public Structure MapRec
    
            Public X As Integer
            Public Y As Integer
    
            Public Tile(MAX_LAYERS, max_x, Max_y) As TileRec
    
        End Structure
    "Error 1 Arrays declared as structure members cannot be declared with an initial size."

  25. #25
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,600

    Re: vb 2010 unbalanced stack

    The solution is simple then...make it a class. As a matter of fact, I would have never made it a structure in the first place.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  26. #26

    Thread Starter
    Lively Member
    Join Date
    Jan 2011
    Posts
    118

    Re: vb 2010 unbalanced stack

    Quote Originally Posted by Niya View Post
    The solution is simple then...make it a class. As a matter of fact, I would have never made it a structure in the first place.

    Can you give me an example of a class?

  27. #27
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: vb 2010 unbalanced stack

    here's a class:

    vb.net Code:
    1. Public Class MapRec
    2.  
    3.     Public X As Integer
    4.     Public Y As Integer
    5.  
    6.     Public Tile(MAX_LAYERS, max_x, Max_y) As TileRec
    7.  
    8. End Class

  28. #28

    Thread Starter
    Lively Member
    Join Date
    Jan 2011
    Posts
    118

    Re: vb 2010 unbalanced stack

    Quote Originally Posted by .paul. View Post
    here's a class:

    vb.net Code:
    1. Public Class MapRec
    2.  
    3.     Public X As Integer
    4.     Public Y As Integer
    5.  
    6.     Public Tile(MAX_LAYERS, max_x, Max_y) As TileRec
    7.  
    8. End Class
    and how will I save/load the class now?

  29. #29
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,600

    Re: vb 2010 unbalanced stack

    Use the save function I gave you. Adapt it to use the the MapRec type.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

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