Android Question how to define a new type as a list of words? TYPE tHeading = (N, E, S, W)

B4X:
class course


TYPE theading = (North,East,South,West);

TYPE tDegree = (0..360 as int);

type compassvalue as (dir as theading, deg as tDegree, dist as int)

private comp as compassvalue


initialize (dir, deg, dist){

comp.dir = dir

comp.deg = deg

comp.dist = dist

}

how can i define a type like tHeading shown above, and run the constructor using dim x as course; x.initialize(North, 100, 10);
 

aeric

Expert
Licensed User
Longtime User
or are you looking for something like this?

B4XMainPage:
Sub Class_Globals
    Private Root As B4XView
    Private x As course
End Sub

Public Sub Initialize
    x.Initialize("North", 100, 10)
End Sub

course:
' course class
Sub Class_Globals
    Type CompassValue (Dir As String, Deg As Int, Dist As Int)
    Private pComp As CompassValue
End Sub

Public Sub Initialize (Dir As String, Deg As Int, Dist As Int)
    pComp = CreateCompassValue(Dir, Deg, Dist)
End Sub

Public Sub setComp (mComp As CompassValue)
    pComp = mComp
End Sub

Public Sub getComp As CompassValue
    Return pComp
End Sub

Public Sub CreateCompassValue (Dir As String, Deg As Int, Dist As Int) As CompassValue
    Dim t1 As CompassValue
    t1.Initialize
    t1.Dir = Dir
    t1.Deg = Deg
    t1.Dist = Dist
    Return t1
End Sub
 
Upvote 0

emexes

Expert
Licensed User
TYPE tDegree = (0..360 as int);

Probably better for Int degrees to be constrained to 0..359 so that there is not confusion at 360 = 0 degrees.

You can convert degrees to 0/1/2/3 = north/east/south/west with:

B4X:
Dim QuadrantToCompass() As String = Array As String("North", "East", "South", "West")
Dim QuadrantHeading As Int = Floor(Degrees / 360 * 4 + 0.5) Mod 4
Log(Degrees & Tab & QuadrantHeading & Tab & QuadrantToCompass(QuadrantHeading))
 
Last edited:
Upvote 0

emexes

Expert
Licensed User
For more compass points, perhaps something like:

B4X:
Dim SixteenthToCompass() As String = Array As String( _
    "North", "NNE", "NE", "ENE", _
    "East" , "ESE", "SE", "SSE", _
    "South", "SSW", "SW", "WSW", _
    "West" , "WNW", "NW", "NNW"  _
)

Dim SixteenthHeading = Floor(Degrees / 360 * 16 + 0.5) Mod 16
 
Upvote 0
Oh, i thought it must be possible like in old Pascal times.
The Const map to integers is a bit confusing to me, but the String solution seems to be nice. I have to figure this out.

OT:
During this i thought it was nice to have a .toCamelCase or .toPascalCase function for converting "low budget" to "Low Budget" or "low Budget" (or w/o spaces).
Yes, there is something with regex in the forum, but would be a handy small function anyway..

Is B4X a kind of own language which is defined by erel based on visual basic or is there a strict language model behind?

thanks anyway for help
 
Upvote 0
is it wrong to mass create a key value database with objects?
and if not, how? forum said dont use arrays, use lists (collections) instead like:

Pseudocode for adding multiple classes to a list without making a variable per class:
Dim Courses as List

Courses.Add As CompassClass.Initialize(N,2,3000)
Courses.Add As CompassClass.Initialize(W,180,1000)
Courses.Add As CompassClass.Initialize(E,20,3242)

the idea is, that i have a set of courses each in one class.
in prior times i just would use an array of own types.
 
Upvote 0
seems to work, but not sure, as iam figuring out how to access the properties of a class inside the list

B4X:
Dim Courses as List
Courses.Initialize
Dim x as CompassCourses
Lib.Add(x.Initialize("N",100,2000))
Lib.Add(x.Initialize("S",200,10000))

Courses(2).getDistance should return 10000 from second entry.
but i fear i did not initialize a reference but just a return value.
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
seems to work, but not sure, as iam figuring out how to access the properties of a class inside the list

B4X:
Dim Courses as List
Courses.Initialize
Dim x as CompassCourses
Lib.Add(x.Initialize("N",100,2000))
Lib.Add(x.Initialize("S",200,10000))

Courses(2).getDistance should return 10000 from second entry.
but i fear i did not initialize a reference but just a return value.

You need to Dim a new variable everytime you want to instantiated as a new object.
B4X:
    Dim Courses As List
    Courses.Initialize
    
    Dim Compass As CompassValue
    Compass.Initialize
    Compass.Dir = "N"
    Compass.Deg = 100
    Compass.Dist = 2000
    Courses.Add(Compass)
    
    Dim Compass As CompassValue
    Compass.Initialize
    Compass.Dir = "S"
    Compass.Deg = 200
    Compass.Dist = 10000
    Courses.Add(Compass)

or better, use a new sub routine to instantiate a new object.

It is easy to create the sub. Hover your mouse pointer to the Type and click the blue link. A new sub will be added at the bottom of the code editor.

1705735959591.png


Now you will get this:
B4X:
Public Sub CreateCompassValue (Dir As String, Deg As Int, Dist As Int) As CompassValue
    Dim t1 As CompassValue
    t1.Initialize
    t1.Dir = Dir
    t1.Deg = Deg
    t1.Dist = Dist
    Return t1
End Sub

To use the code, write:
B4X:
    Dim Courses As List
    Courses.Initialize
    Courses.Add(CreateCompassValue("N", 100, 2000))
    Courses.Add(CreateCompassValue("S", 200, 10000))
 
Upvote 0
Understanding B4X Datastructures is a bit tricky, if i want to do some kind of SQL question:

quests(type,score) as List
where this should be somekind of array to choose the right list, means inside the list i have a custom type like xtype
now i want to have these xtypes sorted while adding means i add them to type: "hiking" or "biking" and for earch type there is one score which holds all xtypes of same type and score, means score is the value how much worthy this xtype is.
i dont know how to describe it well so i try again with an example:

quests(walk,1) consists of a list which has a customtype as item.
quests(walk,2) is another list with same content type
quests(bike,5) is same as above


means its just a sorted table of customtypes which are first sorted by kind of activity and then sorted by the score you get for doing it.

the idea behind this construct is to easily access the customtypes without sorting the list first.

use case:
user says, hey i want to bike and need a task which is 5 score points worth.
so program access the list at quests(bike,5).Get(i) where i is random.

previously i had all types and scores in one list, but this was not good to get a random item sorted by the type and score.
and no, i dont want to use sql for this :)
in the doc i have read, that two dimensional arrays need same length for both dimensions hm.
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
Understanding B4X Datastructures is a bit tricky, if i want to do some kind of SQL question:

quests(type,score) as List
where this should be somekind of array to choose the right list, means inside the list i have a custom type like xtype
now i want to have these xtypes sorted while adding means i add them to type: "hiking" or "biking" and for earch type there is one score which holds all xtypes of same type and score, means score is the value how much worthy this xtype is.
i dont know how to describe it well so i try again with an example:

quests(walk,1) consists of a list which has a customtype as item.
quests(walk,2) is another list with same content type
quests(bike,5) is same as above


means its just a sorted table of customtypes which are first sorted by kind of activity and then sorted by the score you get for doing it.

the idea behind this construct is to easily access the customtypes without sorting the list first.

use case:
user says, hey i want to bike and need a task which is 5 score points worth.
so program access the list at quests(bike,5).Get(i) where i is random.

previously i had all types and scores in one list, but this was not good to get a random item sorted by the type and score.
and no, i dont want to use sql for this :)
in the doc i have read, that two dimensional arrays need same length for both dimensions hm.
Before exploring Type, it helps if you have a good understanding of Lists and Maps or more precisely B4XCollections.
You may also want to use Class instead of custom Type.
If you want to ask about SQL, you can create a new thread. Maybe SQL is a better solution... or maybe KeyValueStore is good to go.
 
Upvote 0
I think i should try the Key Value collection "map" instead.
so i have an PK per value.
maybe there should be a much simpler sqlite integration than the current one, actually i did not manage to just sync an sqlite db between B4A Studio and the Android device. maybe i should post a wish, to just simple sync a file into the app dir or somewhere, so i can edit the sqlite here on computer and it will be synced on every debug test and published with apk.
 
Upvote 0
Top