Shop OBEX P1 Docs P2 Docs Learn Events
How to declare a fixed length string — Parallax Forums

How to declare a fixed length string

I'm trying to figure-out how to declare a fixed length string in 5.9.9. What I'm after is something like this:

class tempType
   Var1 as ulong
   Var2 as ushort
   Var3 as string(128)
end class
dim NewType as tempType
NewType.Var3 = "This is a test"
print NewType.Var3

The goal here is to always have 128 bytes reserved for Var3. Ideally I'd also like it never to be garbage-collected... just a static array of bytes that "looks like" a regular string. Any thoughts about how to make this work?

Comments

  • how about byte(128) ?

    Mike

  • pik33pik33 Posts: 2,350
    edited 2022-04-13 10:51

    You can alias the string as a pointer to the ubyte array, but then you cannot assign to it as every assign creates a new string with a new pointer. In the class you can write your own methods for such a "string"

    dim a(128) as ubyte
    dim b as ubyte pointer
    declare a$ alias b as string
    
    a(0)=65
    b=@a
    a(1)=0
    print a$
    
    a$="b"
    print a(0)
    

    The result:

    A
    65
    

    After aliasing a$ as the pointer to a and updating a, you can print a$.
    After updating a$ the pointer to a$ is no longer the pointer to a, so after assigning the new value, the pointer also changed (and was allocated, etc...)

    And string(128) is an array of 128 strings (= 128 pointers)

Sign In or Register to comment.