[Gambas-user] String array empty after SUB (Gambas 2)
Emil Lenngren
emil.lenngren at ...626...
Thu Feb 23 13:21:37 CET 2012
The problem is not that feld.copy does not copy the array or something like
that, it really returns a NEW deep copy of the array, i.e. a new object.
When you call the function by executing
ldsv.ListeKundeFelder(schlyssel[ls.Index], felder)
you pass the reference to a String[] you want to populate with data.
But, if you run felder = feld.copy() in the ListeKundeFelder, you don't
populate the passed array with data, but instead throw away the reference
and create a new array that is assigned to the local variable felder (which
gets removed when the function exits).
You can use the Insert method on the String[], which does the job:
felder.Insert(feld)
Another option would be to pass the felder String[] by reference. That
should actually be more optimized in this case since all you want is to
replace the instance variable felder array (in the caller) by another array
that is generated by ListeKundeFelder. Then no array copy has to be done.
(Why copy an array if you want to remove the source array right after?)
Or even better, let the ListeKundeFelder return a new array that you can
assign to the felder instance variable ;)
/Emil
2012/2/23 Bruce Bruen <bbruen at ...2308...>
> On Thu, 2012-02-23 at 09:09 +0100, Rolf-Werner Eilert wrote:
> > That was it, Jussi, thanks a lot!
> >
> > Here we can see what it is good for to know about OOP :-)
> >
> > Regards
> >
> > Rolf
> >
> > Am 22.02.2012 18:49, schrieb Jussi Lahtinen:
> > > Just quick thought... maybe this "felder = feld.Copy()" gives new
> object
> > > reference feld to felder.
> > > And because feld is declared locally, the it is null after the sub and
> so
> > > is also felder.
> > >
> > > Perhaps you need to do "felder = feld.Copy()" manually with for each...
> > >
> > > Jussi
> > >
> > >
> Interesting! I must admit that I have never used String[].Copy but I
> would have expected that a "deep" copy meant that every string in the
> array was copied to the target array, not just that a reference was
> created.
>
> So what is the difference between
> felder=feld
> and
> felder=feld.Copy
> ?
>
> In the former case I would expect a reference but in the latter I would
> have assumed a full transfer of the whole array replacing whatever was
> in felder.
>
> Further, within the example quoted by Rolf, if I explicitly copy every
> string in feld to felder, viz
> Dim wkStr as String
> felder.Clear (or felder = New String[] **)
> for each wkstr in feld
> felder.Add(wkstr)
> next
> will it work?
>
> ** the latter would be interesting, re-initialising a String[] array in
> a called routine???
>
> Bruce
>
>
>
>
>
> ------------------------------------------------------------------------------
> Virtualization & Cloud Management Using Capacity Planning
> Cloud computing makes use of virtualization - but cloud computing
> also focuses on allowing computing to be delivered as a service.
> http://www.accelacomm.com/jaw/sfnl/114/51521223/
> _______________________________________________
> Gambas-user mailing list
> Gambas-user at lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/gambas-user
>
More information about the User
mailing list