Android Question Array of Classes

jfranz

Member
Licensed User
Longtime User
I have what should be a very easy thing to do. I have searched the site using terms like "array of classes" and "class array" etc but find nothing.

Here is what I am trying to do. I have a class that basically has attributes (Team, Player#). Basically just lots of get/set methods. Lets call this class Point

Next I have another class that has a private instance of an array of 200 Point classes. Call this class Game.

In the Game class I have something like this

B4X:
sub Class_Globals
    Private mPoints(200) as Point
end sub

public sub Reset()
      For x = 0 To mPoints.Length-1
        mPoints(x).Team= 0
        mPoints(x).Player = 0
    Next

I of course initialize the Game object and then call the Reset method. I get a uninitialized object error when trying to set Team to 0.

What simple thing am I missing and how would I find an example of this using the web search function?

Thanks
 

margret

Well-Known Member
Licensed User
Longtime User
Look at the Type function. See below:

B4X:
'Declare the settings
Type dbFilters (FieldName As String, Comparator As String, FilterValue As String, Xor As String)
Private Filters(4) As dbFilters
Filters(0).Initialize
Filters(1).Initialize
Filters(2).Initialize
Filters(3).Initialize
 
 'Set or change the values
Filters(0).FieldName = "field name"
Filters(0).Comparator = "="
Filters(0).FilterValue = ">"
Filters(0).Xor = "And"
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
If this is the first time you have accessed the point classes, you will need to initialize them too.

B4X:
public sub Reset()
      For x = 0 To mPoints.Length-1
        mPoints(x).Initialize
        mPoints(x).Team= 0
        mPoints(x).Player = 0
    Next

Although you won't want to do this every time you reset, so you may want an Initialize Points sub too just to do that.
 
Upvote 0

jfranz

Member
Licensed User
Longtime User
Stevel05 - Thanks for pointing out my error in this. Added it to the Initialize and worked like I had expected.

Margret - thanks for the help. I guess types and classes work exactly the same and I have used types in the past but just never thought about it in this case.

In either case I didn't get an answer about the web search functionality. When I searched I didn't get a helpful result back. The first few pages had lots of listview and other things but limited if any list items. This happens a lot and is now making searching very difficult as this tool ages. Guess time for a different request on the search.

Thanks for the help.
 
Upvote 0
Top