VB like types

New to FreeBASIC? Post your questions here.
Post Reply
nuwan
Posts: 33
Joined: Jan 12, 2008 7:37
Location: srilanka
Contact:

VB like types

Post by nuwan »

In vb i can create types like this

Code: Select all

type cool
dim abc() as string
end type
also how can i dim namespaces (ok i know it gives an error),is there any alternative solution, i've posted similer question already[/quote]
stylin
Posts: 1253
Joined: Nov 06, 2005 5:19

Post by stylin »

Variable-length array class members are not supported at this time.

Namespaces are declared like this:
http://www.freebasic.net/wiki/wikka.php ... gNamespace
vdecampo
Posts: 2992
Joined: Aug 07, 2007 23:20
Location: Maryland, USA
Contact:

Post by vdecampo »

This isn't exactly dynamic arrays in types but it works! :-)

Code: Select all

#Define strWidth  255

Dim StringArray() As ZString * strWidth

Type DynamicType
   StringArry As zString Ptr  'Arraypointer
End Type

Dim DT As DynamicType

   ReDim StringArray(5)
   
   StringArray(0)="Hello"
   StringArray(1)="There!"
   StringArray(2)="This is"
   StringArray(3)="a HACK!"
   
   DT.StringArry = @StringArray(0)
   
   Print DT.StringArry[0*strWidth]
   Print DT.StringArry[1*strWidth]
   Print DT.StringArry[2*strWidth]
   Print DT.StringArry[3*strWidth]
   
Sleep
Cheers!
-Vince
nuwan
Posts: 33
Joined: Jan 12, 2008 7:37
Location: srilanka
Contact:

thankx

Post by nuwan »

thankx i'll use your code[vdecampo]
counting_pine
Site Admin
Posts: 6323
Joined: Jul 05, 2005 17:32
Location: Manchester, Lancs

Post by counting_pine »

Be sure to update the pointer each time you redim the array though - because there's a good chance redimming will change the address.
nuwan
Posts: 33
Joined: Jan 12, 2008 7:37
Location: srilanka
Contact:

pointers

Post by nuwan »

please tell me how to do that , i'cant understand pointers
notthecheatr
Posts: 1759
Joined: May 23, 2007 21:52
Location: Cut Bank, MT
Contact:

Post by notthecheatr »

In this case you would simply do

Code: Select all

DT.StringArry = @StringArray(0)
But in this case I would also wonder if it's safe to assume you can even do that, since I would expect the strings themselves to move around every time you modify them (if they get larger, that is).

I prefer to use byte arrays inside an object, something like

Code: Select all

Type myString
  Public:
    Declare Constructor ()
    Declare Destructor ()
    
    Declare Operator Cast () As String
    Declare Operator Let (newval As String)
  
  Private:
    As uByte Ptr _str_data
    As uInteger _str_len
    As Any Ptr _mutex
    As Byte _valid
End Type

Constructor myString ()
  this._mutex = MutexCreate()
  
  MutexLock(this._mutex)
  this._str_data = Allocate(1)
  this._str_len = 0
  this._valid = -1
  MutexUnlock(this._mutex)
End Constructor

Destructor myString ()
  DeAllocate(this._str_data)
  this._str_len = 0
  this._valid = 0
  MutexDestroy(this._mutex)
End Destructor

Operator myString.Cast () As String
Dim tmpStr As String
  If this._valid = 0 Then Return ""
  If this._str_len = 0 Then Return ""
  For i As Integer = 0 to this._str_len-1
    tmpStr = tmpStr + Chr(this._str_data[i])
  Next i
  Return tmpStr
End Operator

Operator myString.Let (newval As String)
  If Len(newval) = 0 Then
    this._str_len = 0
    Exit Operator
  Else
    MutexLock(this._mutex)
    this._str_len = Len(newval)
    this._str_data = ReAllocate(this._str_data, this._str_len)
    For i As Integer = 0 To Len(newval)-1
      this._str_data[i] = newval[i]
    Next i
    MutexUnlock(this._mutex)
  End If
End Operator

Code: Select all

Dim myStr As myString

myStr = "Hi!"

Print myStr

Sleep
Which you can store a pointer to since its own location will not change if the string size changes (instead the internal pointer will change).
counting_pine
Site Admin
Posts: 6323
Joined: Jul 05, 2005 17:32
Location: Manchester, Lancs

Post by counting_pine »

notthecheatr wrote:I would also wonder if it's safe to assume you can even do that, since I would expect the strings themselves to move around every time you modify them (if they get larger, that is).
There's no problem with that. The actual string data isn't stored in the array. The array just holds the string descriptors - a descriptor, being a small, fixe-size structure that contains the length of the string, and a pointer to the actual data. This information is automatically updated whenever you change the string. It works just the same as when you're using a normal array, or even just a single string variable.

Anyway, to update the pointer, just make sure to re-set it to the address of the base of the array, when/if ever you redim it:

Code: Select all

Redim StringArray(5)
DT.StringArry = @StringArray(0)
D.J.Peters
Posts: 8642
Joined: May 28, 2005 3:28
Contact:

Post by D.J.Peters »

delete me
wrong thread sorry

Joshy
Last edited by D.J.Peters on Jan 16, 2008 19:01, edited 2 times in total.
notthecheatr
Posts: 1759
Joined: May 23, 2007 21:52
Location: Cut Bank, MT
Contact:

Post by notthecheatr »

counting_pine wrote:There's no problem with that. The actual string data isn't stored in the array. The array just holds the string descriptors - a descriptor, being a small, fixe-size structure that contains the length of the string, and a pointer to the actual data. This information is automatically updated whenever you change the string. It works just the same as when you're using a normal array, or even just a single string variable.
Oh, OK. But it would be much nicer to put it in an OOP interface so we don't always have to update the pointer.
Post Reply