PlanetSquires Forums

Please login or register.

Login with username, password and session length
Advanced search  

Author Topic: TIP: CWSTR - Dynamic unicode strings in UDTs  (Read 260 times)

José Roca

  • Moderator
  • Master FireFly Member
  • *****
  • Posts: 2732
    • José Roca Software
TIP: CWSTR - Dynamic unicode strings in UDTs
« on: June 11, 2017, 03:17:08 PM »

With CWSTR we can have dynamic unicode strings in an UDT.

Code: [Select]
   DIM _myudt AS MyUDT
   DIM cws1 AS CWSTR PTR = NEW CWSTR("First stting")
   DIM cws2 AS CWSTR PTR = NEW CWSTR("Second stting")
   _myudt.cws1 = cws1
   _myudt.cws2 = cws2

   print *_myudt.cws1
   print *_myudt.cws2

   *_myudt.cws1 = "qwerty"
   *_myudt.cws2 += " abcde"

   print *_myudt.cws1
   print *_myudt.cws2
   print MID(*_myudt.cws1, 3, 3)

   ' // Once finished, delete the CWSTR classes
   IF cws1 THEN Delete cws1
   IF cws2 THEN Delete cws2

José Roca

  • Moderator
  • Master FireFly Member
  • *****
  • Posts: 2732
    • José Roca Software
Re: TIP: CWSTR - Dynamic unicode strings in UDTs
« Reply #1 on: June 11, 2017, 03:29:35 PM »

If we don't use NEW, then we don't need to clean the memory used by the CWSTRins (the destructor of the class will dot it), but, of course, the pointer stored in the UDT won't we valid once a CWSTR goes out of scope.

Code: [Select]
TYPE MyUDT
   cws1 AS CWSTR PTR
   cws2 AS CWSTR PTR
END TYPE

Code: [Select]
   DIM _myudt AS MyUDT
   DIM cws1 AS CWSTR = "First stting"
   DIM cws2 AS CWSTR = "Second stting"
   _myudt.cws1 = @cws1
   _myudt.cws2 = @cws2

   print *_myudt.cws1
   print *_myudt.cws2

   *_myudt.cws1 = "qwerty"
   *_myudt.cws2 += " abcde"

   print *_myudt.cws1
   print *_myudt.cws2
   print MID(*_myudt.cws1, 3, 3)
« Last Edit: June 12, 2017, 10:39:09 AM by José Roca »
Logged

Richard Kelly

  • FireFly3 Registered User
  • Senior FireFly Member
  • *
  • Posts: 318
Re: TIP: CWSTR - Dynamic unicode strings in UDTs
« Reply #2 on: June 12, 2017, 10:00:59 AM »

Can you also do this with the dictionary object?

Rick
Logged

José Roca

  • Moderator
  • Master FireFly Member
  • *****
  • Posts: 2732
    • José Roca Software
Re: TIP: CWSTR - Dynamic unicode strings in UDTs
« Reply #3 on: June 12, 2017, 10:51:47 AM »

Of course.

Code: [Select]
TYPE MyUDT
   pDic AS CWstrDic PTR
END TYPE

Code: [Select]
   DIM _myudt AS MyUDT
   DIM pDic AS CWstrDic PTR = NEW CWstrDic

   ' // Adds some key, value pairs
   pDic->Add "a", "Athens"
   pDic->Add "b", "Belgrade"
   pDic->Add "c", "Cairo"

   ' // Assign the class pointer to the pDic member of the UDT
   _myudt.pDic = pDic
   ' // Number of items
   print "Count: "; _myudt.pDic->Count
   ' // Check if the key "a" exists
   print _myudt.pDic->Exists("a")
   ' // Change key "b" to "m" and "Belgrade" to "México"
   _myudt.pDic->Key("b") = "m"
   _myudt.pDic->Item("m") = "México"
   print _myudt.pDic->Item("m")
   ' --or--
   ' print pDic->Item("m")
   ' // Remove key "m"
   _myudt.pDic->Remove "m"
   IF _myudt.pDic->Exists("m") THEN PRINT "Key m exists" ELSE PRINT "Key m doesn't exists"

   ' // Once finished, delete the CwstrDic class
   IF pDic THEN Delete pDic

Also note that as both pDic and _myudt.pDic point to the same class, you can use both at the same time. And you can detele the class using Delete pDic or Delete _myudt.pDic, but don't delete the class twice!
« Last Edit: June 12, 2017, 11:13:19 AM by José Roca »
Logged

Richard Kelly

  • FireFly3 Registered User
  • Senior FireFly Member
  • *
  • Posts: 318
Re: TIP: CWSTR - Dynamic unicode strings in UDTs
« Reply #4 on: June 12, 2017, 12:52:16 PM »

This gives me some pause as multi dimensional arrays takes on a new meaning.

Rick
Logged