none
from array of structures to list of structures, help asked RRS feed

  • Question

  • Hello reader,

    I have the following code, declaring a structure steObject and declaring and initializing an array arrObject of this structure. This works. How do I do the same, but now using a List(Of steObject)? 

    Best regards

    Henk Koetzier.

        Structure steObject
            Dim strDescription As String
            Dim strExamine As String
            Dim intLocation As Integer
            Dim boVisible As Boolean
            Dim boTaken As Boolean
    
            Sub New(strD As String, strE As String, intL As Integer, boV As Boolean, boT As Boolean)
                strDescription = strD
                strExamine = strE
                intLocation = intL
                boVisible = boV
                boTaken = boT
            End Sub
    
            Sub Examine()
                If Me.intLocation = intCurLoc And Me.boVisible Then
                    Console.WriteLine(Me.strExamine & ".")
                Else
                    Console.WriteLine("You can't examine the " & Me.strExamine & "!")
                End If
            End Sub
    
            Sub Take()
                If Me.intLocation = intCurLoc And Me.boVisible And Not Me.boTaken Then
                    Me.boTaken = True
                    Console.WriteLine("You pick up the " & Me.strDescription & ".")
                Else
                    If Me.AlreadyTaken Then
                        Console.WriteLine("You have already taken the " & Me.strDescription & "!")
                    Else
                        Console.WriteLine("You can't take the " & Me.strDescription & "!")
                    End If
                End If
            End Sub
    
            Sub Drop()
                If Me.boTaken = True Then
                    Me.boTaken = False
                    Me.intLocation = intCurLoc
                    Console.WriteLine("You drop the " & Me.strDescription & ".")
                Else
                    If Not Me.AlreadyTaken Then
                        Console.WriteLine("You don't have the " & Me.strDescription & "!")
                    Else
                        Console.WriteLine("You can't drop the" & Me.strDescription & ".")
                    End If
                End If
            End Sub
    
            Function AlreadyTaken() As Boolean
                AlreadyTaken = Me.boTaken
            End Function
    
            Function InInventory() As Boolean
                If Me.boTaken Then
                    InInventory = True
                Else
                    InInventory = False
                End If
            End Function
    
            Function AtLocation() As Boolean
                If Me.intLocation = intCurLoc Then
                    AtLocation = True
                Else
                    AtLocation = False
                End If
            End Function
    
        End Structure
    
        Dim arrObject() As steObject = {New steObject("Gold coin", "It features the head of an ogre", 1, True, False),
                                        New steObject("Dagger", "It is made of dark, cold metal", 2, True, False),
                                        New steObject("Potion", "It looks ruby", 3, True, False),
                                        New steObject("Empty Bottle", "It used to contain the potion", 3, False, False)}
    



    Friday, February 23, 2018 4:21 PM

All replies

  • Henk,

    I don't know if this is a school assignment, in that case don't ask it in a forum. The code you use can be an intro to a better way,. 

    If that is not then use a better way. To give a sample from your code

    Public Class MySampleClass
        Class Ste
            Public Property Description As String
            Public Property Examine As String
            Public Property Location As Integer
            Public Property Visible As Boolean
            Public Property Taken As Boolean
        End Class
        Public Stes As New List(Of Ste)
        Sub Add(Description As String, Examine As String, Location As Integer, Visible As Boolean, Taken As Boolean)
            Stes.Add(New Ste With {.Description = .Description, .Examine = Examine, .Location = Location, .Visible = .Visible, .Taken = Taken})
    
        End Sub
    End Class
    

    I used classes, structures are expensive (if the value part has more than 16 bytes). Use modern conventions. Use property initializers. Try to make my code maintainable by making it readable without all kind of 1985 type prefixes.


    Success
    Cor

    Friday, February 23, 2018 4:46 PM
  • Dear Cor,

    This is not a school assignment, and I did learn programming around 1985. I am trying to get my head around object oriented programming. I programmed your example, but i have trouble in creating and initializing an object based on your class definition. I get a declaration expected error in the last line of code (below).

    regards

    Henk Koetzier

    Public Class gameObjects
            Class gmObject
                Public Property Description As String
                Public Property Examine As String
                Public Property Location As Integer
                Public Property Visible As Boolean
                Public Property Taken As Boolean
    
                Sub Examine()
                    If Me.intLocation = intCurLoc And Me.boVisible Then
                        Console.WriteLine(Me.strExamine & ".")
                    Else
                        Console.WriteLine("You can't examine the " & Me.strExamine & "!")
                    End If
                End Sub
    
                Sub Take()
                    If Me.intLocation = intCurLoc And Me.boVisible And Not Me.boTaken Then
                        Me.boTaken = True
                        Console.WriteLine("You pick up the " & Me.strDescription & ".")
                    Else
                        If Me.AlreadyTaken Then
                            Console.WriteLine("You have already taken the " & Me.strDescription & "!")
                        Else
                            Console.WriteLine("You can't take the " & Me.strDescription & "!")
                        End If
                    End If
                End Sub
    
                Sub Drop()
                    If Me.boTaken = True Then
                        Me.boTaken = False
                        Me.intLocation = intCurLoc
                        Console.WriteLine("You drop the " & Me.strDescription & ".")
                    Else
                        If Not Me.AlreadyTaken Then
                            Console.WriteLine("You don't have the " & Me.strDescription & "!")
                        Else
                            Console.WriteLine("You can't drop the" & Me.strDescription & ".")
                        End If
                    End If
                End Sub
    
                Function AlreadyTaken() As Boolean
                    AlreadyTaken = Me.boTaken
                End Function
    
                Function InInventory() As Boolean
                    If Me.boTaken Then
                        InInventory = True
                    Else
                        InInventory = False
                    End If
                End Function
    
                Function AtLocation() As Boolean
                    If Me.intLocation = intCurLoc Then
                        AtLocation = True
                    Else
                        AtLocation = False
                    End If
                End Function
            End Class
    
            Public gmObjects As New List(Of gmObject)
    
            Sub Add(Description As String, Examine As String, Location As Integer, Visible As Boolean, Taken As Boolean)
                gmObjects.Add(New gmObject With {.Description = .Description, .Examine = Examine, .Location = Location, .Visible = .Visible, .Taken = Taken})
            End Sub
        End Class
    
        Dim myGameObjects As GameObjects
    
        myGameObjects.Add("Dummy", "Dummy", 0, False, False)
    


    Friday, February 23, 2018 7:17 PM
  •  I get a declaration expected error in the last line of code (below).

    Your code is outside the class definition.  In .Net all code must be within an object definition.  Move that code to a method in your form class, such as form load.

    Declaration expected means that the only statement that would be valid at that location would be a statement that declares an object, such as a class or a structure.

    Friday, February 23, 2018 9:42 PM
  • Based upon how the GameObjects Class is defined it looks like you need to create an instance:

    Dim myGameObjects As New GameObjects


    Paul ~~~~ Microsoft MVP (Visual Basic)

    Friday, February 23, 2018 10:06 PM
  • Henk,

    4 things,

    1. don't use a console project to learn around OOP. Use for learning a Windows Forms project that is the most simple in .Net. 
    2. don't put in/output in a class which is in fact describing the data
    3. don't make from a program one whole class, that was 1985. Make classes from things you need to use. Before your sub examine I would set that End class. 
    4. don't make methods before you need them. Mostly you make then more then needed. 

    In my sample is before your Sub Examine I end that data description class. 


    Success
    Cor


    Friday, February 23, 2018 10:19 PM
  • ...I used classes, structures are expensive (if the value part has more than 16 bytes)...

    Success
    Cor

    Agreed.

    Some other info.

    https://docs.microsoft.com/en-us/dotnet/visual-basic/programming-guide/language-features/data-types/structures-and-classes


    "Those who use Application.DoEvents() have no idea what it does and those who know what it does never use it" - MSDN User JohnWein

    Friday, February 23, 2018 10:23 PM
  • Thanks all, your input helped a lot!

    Henk

    Sunday, February 25, 2018 12:37 PM
  • Thanks all, your input helped a lot!

    Henk


    If your issue is resolved, can you please close the thread by clicking the Mark as answer link at the bottom of one or more appropriate posts?  Thanks!

    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Sunday, February 25, 2018 4:17 PM
    Moderator