Simple Classes and BSTR

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
theNerd
Enthusiast
Enthusiast
Posts: 131
Joined: Sun Mar 20, 2005 11:43 pm

Simple Classes and BSTR

Post by theNerd »

I like the changes being made for PureBasic 4.0. It's these changes that have led me to decide to ditch most other languages (except the mainstream ones I need for work.) However, I am wondering what your opinions\reasons in regards to the following are:

BSTR - I like that you have added this as a virtual type but is there any reason why it's not as a native type?

CLASSES - I know PureBasic is not intended to be an OOP language but incorporating classes does not necessarily mean it is an OOP language. For example, VBScript (so others are not confused, I am referring to the scripting language, not VB) is not OO but includes the ability to create classes. Here's a VBScript example of a class so you can see what I am talking about:

Code: Select all

'**********************************************************************************************************************
' Allows for the efficient (very fast) building/concatenating of String variables.
'----------------------------------------------------------------------------------------------------------------------
' Usage:
'	dim example
'	set example = new StringBuilder
'	example.Append("string 1")
'	example.Append(":")
'	example.Append("string 2")
'	
'	dim finalString
'	finalString = example.Value ' finalString = "string 1:string 2"
'	
'	set example = nothing
'**********************************************************************************************************************
Class StringBuilder
	Private StringCounter
	Private StringArray()
	Private StringLength
	
	' Called at creation of instance.
	Private Sub Class_Initialize()
		const INIT_STRING_LENGTH = 128
		StringCounter = 0
		
		ReDim StringArray(INIT_STRING_LENGTH - 1)
		StringLength = INIT_STRING_LENGTH
	End Sub
	
	Private Sub Class_Terminate()
		Erase StringArray
	End Sub

	' Add new string to array
	Public Sub Append(byref NewString)
		if IsNull(NewString) then
			StringArray(StringCounter) = ""
		else
			StringArray(StringCounter) = NewString
		end if
		
		StringCounter = StringCounter + 1
		
		' ReDim array if necessary
		If StringCounter MOD StringLength = 0 Then
			' Redimension
			ReDim Preserve StringArray(StringCounter + StringLength - 1)
			
			' Double the size of the array next time
			StringLength = StringLength * 2
		End If
	End Sub
	
	' Return the concatenated string
	Public Property Get Value
		Value = Join(StringArray, "")
	End Property 
	
	' Resets array
	Public Function Clear()
		const INIT_STRING_LENGTH = 128
		StringCounter = 0
		
		Redim StringArray(INIT_STRING_LENGTH - 1)
		StringLength = INIT_STRING_LENGTH
	End Function
End Class 
Is there any chance you will incorporate basic classes like this? I find them very useful when using VBScript for developing ASP pages. It can make for much cleaner code and can help eliminate speghetti logic.
Last edited by theNerd on Sun Mar 12, 2006 1:57 am, edited 1 time in total.
User avatar
Joakim Christiansen
Addict
Addict
Posts: 2452
Joined: Wed Dec 22, 2004 4:12 pm
Location: Norway
Contact:

Post by Joakim Christiansen »

We all want that :D
He might decide to make something like that one day, but I don't get the feeling we'll have it soon.
Post Reply