[Gambas-user] Question on string[]

Eilert eilert-sprachen at ...221...
Wed Dec 15 08:53:49 CET 2004


Benoit Minisini schrieb:
> On Tuesday 14 December 2004 17:55, Eilert wrote:
> 
>>Well, now, is it allowed or depreciated? Will I run into problems if I
>>create a new String[] object over and over again (maybe because this sub
>>is called again and again?).
>>
>>In my concrete project this is not the case, but I know how easily you
>>can run into such problems... :-)
>>
>>And what do you mean "another variable has a reference on it"?
>>
>>Rolf
>>
> 
> 
> Do you know object programming ? 

Well, you got me on that one :-) Actually, I know objects by theory but 
I never really got in touch with them before.

> 
> Objects are created (with NEW) and then they are always manipulated by 
> reference. Each object has a reference counter that is increased each time it 
> is affected to any variable, and decremented each time the affected variable 
> is replaced by something else.

So this would increase the counter further and further and create ever 
more object references:

STATIC myArray AS String[]

SUB SomeButton_Click()
	
	myProcedure

END SUB

SUB myProcedure()

	myArray = NEW String[]

	'do something with it

END SUB


As myArray is static here, it will be there all the time. Each time I 
click SomeButton, there will be a new reference to it without killing 
the old reference, but the array object still is the same, or am I 
wrong? So it would be better to provide some flag to be set to avoid this:

STATIC myArray AS String[]
STATIC arrayDone AS Boolean

SUB SomeButton_Click()
	
	myProcedure

END SUB

SUB myProcedure()

	IF NOT arrayDone then
		myArray = NEW String[]
		arrayDone = True
	END IF

	'do something with it

END SUB


And of course, when I define it this way, there will be a whole new 
object every time, right?

SUB SomeButton_Click()
	
	myProcedure

END SUB

SUB myProcedure()
DIM myArray AS String[]

	myArray = NEW String[]
	
	'do something with it

END SUB

But you are right, I don't know much about objects. I even keep 
wondering why it takes 2 steps each time to define them - plain BASIC 
thinking I guess :-) Sometimes there is/maybe a change in names, 
sometimes not... Confusing :-))

Rolf





More information about the User mailing list