Android Question Need help with structures

KKmac

New Member
I have been using Visual Studio for 12+ years. This is my first go around with B4A. I can't seem to find the right answer for creating structures. For example, in Visual Studio

Structure PersonDefinition
Dim Name as String
Dim Address as String
Dim City as String
Dim Age as Integer
End Structure

Dim Person() as PersonDefinition

So I can have Person(0) as a person, Person(1) as a different person, etc.
Person(0).Name = "Kevin"or Person(1).Age = 30.
If I want to sort that array, I can create keys for the sorting by column, such as age.

The only way I found (so far) to create this structure in B4A is:

Dim PersonDefinition(Name as String, Address as String, City as String, Age as Int)
Dim Person() as PersonDefinition

If this is the right way, then how would I sort this by age, or alphabetically by name?

I found one place that said to put it in a list, then sort the list. I did that, and it worked, to a degree.
However, I couldn't just use MySortList(0).Name to get the info I need.

It's very clear to me that the terminology is vastly different for the same thing, such as in VS, they are Forms, where B4A is Pages.
I can't seem to locate the proper term for structures. I have read about arrays, collections, and maps. But they aren't very clear. I guess it's just not clicking yet.

Also, my example is shortened. In my "structure", I have far more declaration, such as First Name, Last Name, Address, City, State, Zip, Email, Phone, and so on.

While there are a number of coding similarities, this really isn't visual basic. At least not the one I know.

One more obstacle, My Person declaration itself is multi-array.

Dim Person(5, 20) as PersonDefinition

Kinda hard to explain why I am doing this. I have an event with contestants, and the first array represents each round of competition.
People get eliminated each round, so the second declaration (20) will decrease each round.

I need to be able to sort this mess. I could do this all day long in VS, but things are different here.
I almost never ask for help in a forum, but I am stumped.

Can anyone shed some light on what I have right, wrong, and what I should be doing instead.

Thanks
 

walt61

Active Member
Licensed User
Longtime User
This might get you started:

B4X:
Sub Globals
    'These global variables will be redeclared each time the activity is created.

    ' A Type is like a Structure in VS; add as many members as needed; members can also be
    ' complex variables like Lists, Maps, Types, or views like Labels, Buttons, ...
    Type PersonDefinition(Name As String, Address As String, City As String, Age As Int)
    Dim PersonsList As List ' Forget arrays, use Lists ! You don't need to declare a dimension for Lists.

End Sub

Sub Activity_Create(FirstTime As Boolean)

    Dim i As Int
    Dim j As Int

    'Activity.LoadLayout("Layout")

    PersonsList.Initialize

    ' Create PersonsList with 5 items.
    ' You could also use a Map instead of a List; the Map's keys
    ' would be e.g. 1 through 5, and the values would be lists
    ' containing variables of type 'PersonDefinition'.
    For i = 0 To 4
        ' Initialise PersonsListSub
        Dim personsListSub As List
        personsListSub.Initialize
        ' Create PersonsListSub with 20 items
        For j = 0 To 19
            Dim person As PersonDefinition = CreatePersonDefinition(j, j, j, j) ' Pass the real values here
            personsListSub.add(person)
        Next
        ' Sort PersonsListSub by 'Name', case-insensitive
        personsListSub.SortTypeCaseInsensitive("Name", True)
        ' Alternatively, sort PersonsListSub by 'Age'
        'personsListSub.SortType("Age", True)
        ' Add PersonsListSub to PersonsList
        PersonsList.Add(personsListSub)
    Next

    ' Fetch the 'PersonsListSub' for the 2nd round
    Dim secondRound As List = PersonsList.Get(1) ' The first item has index zero

    ' Fetch the Persons from list 'SecondRound'
    For i = 0 To (secondRound.Size - 1)
        Dim onePerson As PersonDefinition = secondRound.Get(i)
        Log(i & ": " & onePerson.Name)
    Next

End Sub

' This sub is generated by hovering the mouse over PersonDefinition in
' sub Globals, and then clicking the "Generate 'Create Type' Sub" link.
Public Sub CreatePersonDefinition (Name As String, Address As String, City As String, Age As Int) As PersonDefinition

    Dim t1 As PersonDefinition
    t1.Initialize
    t1.Name = Name
    t1.Address = Address
    t1.City = City
    t1.Age = Age
    Return t1

End Sub
 
Last edited:
Upvote 0

aeric

Expert
Licensed User
Longtime User
Another option is to use my MinimaListUtils library.
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
I suggest you use a SQL database.
The above is absolutely the right choice, in your case.


I found one place that said to put it in a list, then sort the list. I did that, and it worked, to a degree.
However, I couldn't just use MySortList(0).Name to get the info I need.
MySortList.Get(Index).As([PersonType]).Name (where PersonType is your custom type, created with"Type PersonType(...))
or (better)
Dim Person As PersonType = MySortList.Get(Index)
Person.Name = ...
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…