B4R Question Strings not matching

barx

Well-Known Member
Licensed User
Longtime User
Hi guys,

This is bending my mellon a bit.

Trying to use globalStore to save a bit of text. It basically keeps track of the stage of a particular operation.

In one section of code I do

B4X:
...
SetMode("init")
...


Sub SetMode(Data As String)
   GlobalStore.Put(0, Data)
End Sub

And then in another sub (a timer tick) I have

B4X:
Sub tmr0_Tick
    Log("Ticking - ", GetMode)
    Select GetMode
        Case "init"
            Log("Goto Login")
            astream_NewData(Array As Byte())
        Case "Closing"
            serial.Close
        Case Else
            Log("'", GetMode, "'")
    End Select
End Sub

Note the Log()'s are to try help me see what is going on.

GetMode is as follows

B4X:
Sub GetMode() As Byte()
    Return GlobalStore.Slot0
End Sub

The strange thing is the Case "init" never happens. The log shows as follows

Ticking - init
'init'
Ticking - init
'init'

So, it always goes to the Case Else, which logs the mode as init :confused:

I have tried setting the return type of the GetMode sub to String and the log then looks like

Ticking - 1073669680
'1073669680'
Ticking - 1073669680
'1073669680'

I have tried changing the Case to this number, both as an Int and a String. Still no joy

So, the big question is, what am I missing, why does "init" not equal "init"?

p.s. Wemos D1 mini
 

Cableguy

Expert
Licensed User
Longtime User
Your Data string seems to be " init " rather than "init".
When I work with strings, specially single word ones, to avoid any ambiguity I do a trim before saving the string so that it can actually be compared without fear of trailing spaces.
My guess is, in your case, that you are trying to pass a 6 character long string " init " that is being automatically trimmed to a 4 character one "init" during the write/read process, so they don't match!
 
Upvote 0

barx

Well-Known Member
Licensed User
Longtime User
Your Data string seems to be " init " rather than "init".
When I work with strings, specially single word ones, to avoid any ambiguity I do a trim before saving the string so that it can actually be compared without fear of trailing spaces.
My guess is, in your case, that you are trying to pass a 6 character long string " init " that is being automatically trimmed to a 4 character one "init" during the write/read process, so they don't match!

That is not the case @Cableguy, I thought that could have been the issue myself hence I wrapped the result in single quotes like this

B4X:
Log("'", GetMode, "'")

To try to high light that. There are no spaces in either case.

Any other ideas?
 
Upvote 0

barx

Well-Known Member
Licensed User
Longtime User
What is very interesting here is if I do

B4X:
Log(Getmode = "init")

the log returns a 1 which typically would resemble True.

So why wouldn't the Select Case statement pick it up?:confused:?
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
B4X:
Sub GetMode() As Byte()
    Return GlobalStore.Slot0
End Sub

should be

B4X:
Sub GetMode() As String
    Return GlobalStore.Slot0
End Sub

You are storing a string (see SetMode), yet you are retrieving a byte array.
 
Upvote 0

barx

Well-Known Member
Licensed User
Longtime User
B4X:
Sub GetMode() As Byte()
    Return GlobalStore.Slot0
End Sub

should be

B4X:
Sub GetMode() As String
    Return GlobalStore.Slot0
End Sub

You are storing a string (see SetMode), yet you are retrieving a byte array.


I appreciate the reply @OliverA but if you see the bottom half of the original post you will see I tried that already with no success ;)
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
1) Reading comprehension fail
2) Wrong forum for me (no B4R experience). Thought this was B4J. 2nd reading comprehension fail.
Getting more coffee...
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
I'm gonna guess that case switch does not work they way you think. My thinking, it does not work with strings. Just a guess. B4R Beginners Guide says the following about strings: "B4R doesn’t support string manipulations like other Basic languages." In this case I think it includes the compare you seem to be looking for of the case structure.
 
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
I'm gonna guess that case switch does not work they way you think. My thinking, it does not work with strings. Just a guess. B4R Beginners Guide says the following about strings: "B4R doesn’t support string manipulations like other Basic languages." In this case I think it includes the compare you seem to be looking for of the case structure.
I would say you are in the right track... Strings in B4R are a b**ch to work with, so this may be one of those cases...
Why not use a "string to int" lookup table?
something like a type, and you set the int value that corresponds to a string... and then you compare the int value...
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
Once more, not a B4R anything, but remember having to write "small" programs (not quite this constraint though).

Create global (yeah, I know) variables of the smallest type you can get away with with meaningful names, sorta like
B4X:
Dim Mode_Init as Byte = 0
Dim Mode_Closing as Byte = 1
' etc.

then do
B4X:
Sub SetMode(Data As Byte)
   GlobalStore.Put(0, Data)
End Sub

Sub GetMode() As Byte
    Return GlobalStore.Slot0
End Sub

Sub tmr0_Tick
    Log("Ticking - ", GetMode)
    Select GetMode
        Case Mode_Init
            Log("Goto Login")
            astream_NewData(Array As Byte())
        Case Mode_Closing
            serial.Close
        Case Else
            Log("'", GetMode, "'")
    End Select
End Sub
Should save a tad bit on memory too since you have less strings in the program.
Warning: This has not been tested. Implementation may need to iron out bugs in the post.

If you still want to use strings, could switch to using IF-Then-Else structure instead. Again, only if I'm guessing correctly about the original select/case issue.
 
Upvote 0

barx

Well-Known Member
Licensed User
Longtime User
Thanks for the replies @OliverA @Cableguy.

I will play with the options a bit tomorrow. I think it's either going to be If statements or track by number (Int).

The Select statement just made for nice easy readable code. There will be about 9 or 10 different mode states by the time i finish so either method could get messy.

Cheers
 
Upvote 0
Top