ReDim of a variable of a custom type

classic Classic list List threaded Threaded
11 messages Options
Johnny Rosenberg Johnny Rosenberg
Reply | Threaded
Open this post in threaded view
|

ReDim of a variable of a custom type

I was, in a reply to another question, told that my questions were way
too basic for the [hidden email] list, so I'll give it a try
here instead.

I wrote some example code:

REM  *****  BASIC  *****

Option Compatible
Option Explicit

Type MyType
       a As Integer
       b As Integer
End Type
Dim x(1) As MyType

Sub Main
       Dim y As MyType
       Dim i As Integer
       For i=0 To 1
               x(i).a=1+i*2
               x(i).b=2+i*2
       Next i
       y.a=5
       y.b=6

       MsgBox x(0).a & x(0).b & x(1).a & x(1).b & y.a & y.b ' Displays 123456

       ReDim x(1) As MyType
       ReDim y As MyType
       MsgBox x(0).a & x(0).b & x(1).a & x(1).b & y.a & y.b ' Displays 123400
End Sub

So if we have an array as MyType (or any other custom type, I
suppose), ReDim will not reset it.
Is this the expected behaviour or should I write a bug report?
If it is expected, why?

I guess it's unnecessary to mention that I am a complete beginner in
everything that has anything to do with computers; we guitar players
are quite stupid, you know.

Oh, and in case it matters:
LibreOffice 3.3.3, Ubuntu 10.10.
LibreOffice installed with the debs at http://www.libreoffice.org/.

Best regards

Johnny ”The Moron” Rosenberg
ジョニー・ローゼンバーグ

--
For unsubscribe instructions e-mail to: [hidden email]
Problems? http://www.libreoffice.org/get-help/mailing-lists/how-to-unsubscribe/
Posting guidelines + more: http://wiki.documentfoundation.org/Netiquette
List archive: http://listarchives.libreoffice.org/global/users/
All messages sent to this list will be publicly archived and cannot be deleted
Jay Lozier Jay Lozier
Reply | Threaded
Open this post in threaded view
|

Re: ReDim of a variable of a custom type

Johnny,

On Thu, 2011-08-18 at 18:11 +0200, Johnny Rosenberg wrote:

> I was, in a reply to another question, told that my questions were way
> too basic for the [hidden email] list, so I'll give it a try
> here instead.
>
> I wrote some example code:
>
> REM  *****  BASIC  *****
>
> Option Compatible
> Option Explicit
>
> Type MyType
>        a As Integer
>        b As Integer
> End Type
> Dim x(1) As MyType
>
> Sub Main
>        Dim y As MyType
>        Dim i As Integer
>        For i=0 To 1
>                x(i).a=1+i*2
>                x(i).b=2+i*2
>        Next i
>        y.a=5
>        y.b=6
>
>        MsgBox x(0).a & x(0).b & x(1).a & x(1).b & y.a & y.b ' Displays 123456
>
>        ReDim x(1) As MyType
>        ReDim y As MyType
>        MsgBox x(0).a & x(0).b & x(1).a & x(1).b & y.a & y.b ' Displays 123400
> End Sub
>
> So if we have an array as MyType (or any other custom type, I
> suppose), ReDim will not reset it.
> Is this the expected behaviour or should I write a bug report?
> If it is expected, why?
>
> I guess it's unnecessary to mention that I am a complete beginner in
> everything that has anything to do with computers; we guitar players
> are quite stupid, you know.
>
> Oh, and in case it matters:
> LibreOffice 3.3.3, Ubuntu 10.10.
> LibreOffice installed with the debs at http://www.libreoffice.org/.
>
> Best regards
>
> Johnny ”The Moron” Rosenberg
> ジョニー・ローゼンバーグ
>

I had to look up ReDim behavior myself, so do not to hard on yourself.
ReDim is used to re-dimension an array. It is particular useful when you
are likely to encounter an variable length array (different length each
time the code executes). Normally when the array is re-dimensioned the
previous values are destroyed because you normally want to enter new
values into the array. Some flavors of basic allow to use ReDim Preserve
to preserve the array's original value - you may want to try this to see
if works.

A common use is
Dim MyArray() as type

ReDim MyArry(1 to x), x will be supplied by the procedure.

Often there are little subtleties that can drive you crazy when program,
the compiler/interpreter is very fussy that you follow all the grammar
and syntax rules for the langauge.
--
Jay Lozier
[hidden email]

--
For unsubscribe instructions e-mail to: [hidden email]
Problems? http://www.libreoffice.org/get-help/mailing-lists/how-to-unsubscribe/
Posting guidelines + more: http://wiki.documentfoundation.org/Netiquette
List archive: http://listarchives.libreoffice.org/global/users/
All messages sent to this list will be publicly archived and cannot be deleted
Johnny Rosenberg Johnny Rosenberg
Reply | Threaded
Open this post in threaded view
|

Re: ReDim of a variable of a custom type

2011/8/18 planas <[hidden email]>:

> Johnny,
>
> On Thu, 2011-08-18 at 18:11 +0200, Johnny Rosenberg wrote:
>
>> I was, in a reply to another question, told that my questions were way
>> too basic for the [hidden email] list, so I'll give it a try
>> here instead.
>>
>> I wrote some example code:
>>
>> REM  *****  BASIC  *****
>>
>> Option Compatible
>> Option Explicit
>>
>> Type MyType
>>        a As Integer
>>        b As Integer
>> End Type
>> Dim x(1) As MyType
>>
>> Sub Main
>>        Dim y As MyType
>>        Dim i As Integer
>>        For i=0 To 1
>>                x(i).a=1+i*2
>>                x(i).b=2+i*2
>>        Next i
>>        y.a=5
>>        y.b=6
>>
>>        MsgBox x(0).a & x(0).b & x(1).a & x(1).b & y.a & y.b ' Displays 123456
>>
>>        ReDim x(1) As MyType
>>        ReDim y As MyType
>>        MsgBox x(0).a & x(0).b & x(1).a & x(1).b & y.a & y.b ' Displays 123400
>> End Sub
>>
>> So if we have an array as MyType (or any other custom type, I
>> suppose), ReDim will not reset it.
>> Is this the expected behaviour or should I write a bug report?
>> If it is expected, why?
>>
>> I guess it's unnecessary to mention that I am a complete beginner in
>> everything that has anything to do with computers; we guitar players
>> are quite stupid, you know.
>>
>> Oh, and in case it matters:
>> LibreOffice 3.3.3, Ubuntu 10.10.
>> LibreOffice installed with the debs at http://www.libreoffice.org/.
>>
>> Best regards
>>
>> Johnny ”The Moron” Rosenberg
>> ジョニー・ローゼンバーグ
>>
>
> I had to look up ReDim behavior myself, so do not to hard on yourself.
> ReDim is used to re-dimension an array. It is particular useful when you
> are likely to encounter an variable length array (different length each
> time the code executes). Normally when the array is re-dimensioned the
> previous values are destroyed because you normally want to enter new
> values into the array. Some flavors of basic allow to use ReDim Preserve
> to preserve the array's original value - you may want to try this to see
> if works.

No, Preserve does the opposite, it preserves all the values. I want an
EMPTY array without having to empty them with a time consuming For
loop, so I though ReDim should do the job very quickly. It does in
most cases, but as my example shows, not always…

As you can see in my example, after ReDim y is reset to 0 (y.a=0,
y.b=0) but x is not (x(0).a=1, for example). The difference between x
and y is that x is an array and x is declared above the subroutine
(this was a couple of days ago, so I don't remember if I tested
absolutely everything, but as far as I remember, my conclusion was
that an array of a custom type isn't reset to 0 after ReDim, I am not
sure if there was a difference if it was defined in a subroutine or
outside.

So my question still is: Is this what I should expect or is it a bug?
If it is a bug I will need to write a bug report, won't I? But I don't
want to do that if this is the expected behaviour, because it only
annoys people…


Kind regards

Johnny Rosenberg
ジョニー・ローゼンバーグ

>
> A common use is
> Dim MyArray() as type
>
> ReDim MyArry(1 to x), x will be supplied by the procedure.
>
> Often there are little subtleties that can drive you crazy when program,
> the compiler/interpreter is very fussy that you follow all the grammar
> and syntax rules for the langauge.
> --
> Jay Lozier
> [hidden email]
>
> --
> For unsubscribe instructions e-mail to: [hidden email]
> Problems? http://www.libreoffice.org/get-help/mailing-lists/how-to-unsubscribe/
> Posting guidelines + more: http://wiki.documentfoundation.org/Netiquette
> List archive: http://listarchives.libreoffice.org/global/users/
> All messages sent to this list will be publicly archived and cannot be deleted
>

--
For unsubscribe instructions e-mail to: [hidden email]
Problems? http://www.libreoffice.org/get-help/mailing-lists/how-to-unsubscribe/
Posting guidelines + more: http://wiki.documentfoundation.org/Netiquette
List archive: http://listarchives.libreoffice.org/global/users/
All messages sent to this list will be publicly archived and cannot be deleted
Jay Lozier Jay Lozier
Reply | Threaded
Open this post in threaded view
|

Re: ReDim of a variable of a custom type

Johnny,

On Thu, 2011-08-18 at 21:46 +0200, Johnny Rosenberg wrote:

> 2011/8/18 planas <[hidden email]>:
> > Johnny,
> >
> > On Thu, 2011-08-18 at 18:11 +0200, Johnny Rosenberg wrote:
> >
> >> I was, in a reply to another question, told that my questions were way
> >> too basic for the [hidden email] list, so I'll give it a try
> >> here instead.
> >>
> >> I wrote some example code:
> >>
> >> REM  *****  BASIC  *****
> >>
> >> Option Compatible
> >> Option Explicit
> >>
> >> Type MyType
> >>        a As Integer
> >>        b As Integer
> >> End Type
> >> Dim x(1) As MyType
> >>
> >> Sub Main
> >>        Dim y As MyType
> >>        Dim i As Integer
> >>        For i=0 To 1
> >>                x(i).a=1+i*2
> >>                x(i).b=2+i*2
> >>        Next i
> >>        y.a=5
> >>        y.b=6
> >>
> >>        MsgBox x(0).a & x(0).b & x(1).a & x(1).b & y.a & y.b ' Displays 123456
> >>
> >>        ReDim x(1) As MyType
> >>        ReDim y As MyType
> >>        MsgBox x(0).a & x(0).b & x(1).a & x(1).b & y.a & y.b ' Displays 123400
> >> End Sub
> >>
> >> So if we have an array as MyType (or any other custom type, I
> >> suppose), ReDim will not reset it.
> >> Is this the expected behaviour or should I write a bug report?
> >> If it is expected, why?
> >>
> >> I guess it's unnecessary to mention that I am a complete beginner in
> >> everything that has anything to do with computers; we guitar players
> >> are quite stupid, you know.
> >>
> >> Oh, and in case it matters:
> >> LibreOffice 3.3.3, Ubuntu 10.10.
> >> LibreOffice installed with the debs at http://www.libreoffice.org/.
> >>
> >> Best regards
> >>
> >> Johnny ”The Moron” Rosenberg
> >> ジョニー・ローゼンバーグ
> >>
> >
> > I had to look up ReDim behavior myself, so do not to hard on yourself.
> > ReDim is used to re-dimension an array. It is particular useful when you
> > are likely to encounter an variable length array (different length each
> > time the code executes). Normally when the array is re-dimensioned the
> > previous values are destroyed because you normally want to enter new
> > values into the array. Some flavors of basic allow to use ReDim Preserve
> > to preserve the array's original value - you may want to try this to see
> > if works.
>
> No, Preserve does the opposite, it preserves all the values. I want an
> EMPTY array without having to empty them with a time consuming For
> loop, so I though ReDim should do the job very quickly. It does in
> most cases, but as my example shows, not always…
>
> As you can see in my example, after ReDim y is reset to 0 (y.a=0,
> y.b=0) but x is not (x(0).a=1, for example). The difference between x
> and y is that x is an array and x is declared above the subroutine
> (this was a couple of days ago, so I don't remember if I tested
> absolutely everything, but as far as I remember, my conclusion was
> that an array of a custom type isn't reset to 0 after ReDim, I am not
> sure if there was a difference if it was defined in a subroutine or
> outside.
>
> So my question still is: Is this what I should expect or is it a bug?
> If it is a bug I will need to write a bug report, won't I? But I don't
> want to do that if this is the expected behaviour, because it only
> annoys people…
>

It looks like a bug to me, go ahead and post a bug report, thanks

> Kind regards
>
> Johnny Rosenberg
> ジョニー・ローゼンバーグ
>
> >
> > A common use is
> > Dim MyArray() as type
> >
> > ReDim MyArry(1 to x), x will be supplied by the procedure.
> >
> > Often there are little subtleties that can drive you crazy when program,
> > the compiler/interpreter is very fussy that you follow all the grammar
> > and syntax rules for the langauge.
> > --
> > Jay Lozier
> > [hidden email]
> >
> > --
> > For unsubscribe instructions e-mail to: [hidden email]
> > Problems? http://www.libreoffice.org/get-help/mailing-lists/how-to-unsubscribe/
> > Posting guidelines + more: http://wiki.documentfoundation.org/Netiquette
> > List archive: http://listarchives.libreoffice.org/global/users/
> > All messages sent to this list will be publicly archived and cannot be deleted
> >
>
> --
> For unsubscribe instructions e-mail to: [hidden email]
> Problems? http://www.libreoffice.org/get-help/mailing-lists/how-to-unsubscribe/
> Posting guidelines + more: http://wiki.documentfoundation.org/Netiquette
> List archive: http://listarchives.libreoffice.org/global/users/
> All messages sent to this list will be publicly archived and cannot be deleted



--
Jay Lozier
[hidden email]

--
For unsubscribe instructions e-mail to: [hidden email]
Problems? http://www.libreoffice.org/get-help/mailing-lists/how-to-unsubscribe/
Posting guidelines + more: http://wiki.documentfoundation.org/Netiquette
List archive: http://listarchives.libreoffice.org/global/users/
All messages sent to this list will be publicly archived and cannot be deleted
TomW TomW
Reply | Threaded
Open this post in threaded view
|

Re: ReDim of a variable of a custom type

In reply to this post by Johnny Rosenberg
On 2011-08-18 12:11, Johnny Rosenberg wrote:

> I was, in a reply to another question, told that my questions were way
> too basic for the [hidden email] list, so I'll give it a try
> here instead.
>
> I wrote some example code:
>
> REM  *****  BASIC  *****
>
> Option Compatible
> Option Explicit
>
> Type MyType
>         a As Integer
>         b As Integer
> End Type
> Dim x(1) As MyType
>
> Sub Main
>         Dim y As MyType
>         Dim i As Integer
>         For i=0 To 1
>                 x(i).a=1+i*2
>                 x(i).b=2+i*2
>         Next i
>         y.a=5
>         y.b=6
>
>         MsgBox x(0).a&  x(0).b&  x(1).a&  x(1).b&  y.a&  y.b ' Displays 123456
>
>         ReDim x(1) As MyType
>         ReDim y As MyType
>         MsgBox x(0).a&  x(0).b&  x(1).a&  x(1).b&  y.a&  y.b ' Displays 123400
> End Sub
>
> So if we have an array as MyType (or any other custom type, I
> suppose), ReDim will not reset it.
> Is this the expected behaviour or should I write a bug report?
> If it is expected, why?
>
> I guess it's unnecessary to mention that I am a complete beginner in
> everything that has anything to do with computers; we guitar players
> are quite stupid, you know.
>
> Oh, and in case it matters:
> LibreOffice 3.3.3, Ubuntu 10.10.
> LibreOffice installed with the debs at http://www.libreoffice.org/.
>
> Best regards
>
> Johnny ”The Moron” Rosenberg
> ジョニー・ローゼンバーグ
>
>    

Johnny:

'Dim x(1) As MyType' is a static array.  ReDim is for use with dynamic arrays.

Dim x() as MyType 'Dynamic Array

sub main()

        ReDim x(1) as MyType
        ...
        ...
End sub

Dim y as MyType is a variable, not an array, so ReDim should not work. But it does apparently, which may be the actual bug, unless there is a nuance I am missing.  Excel 03 does not allow it.

Strangely enough I could get your code to work in Excel with the correct usage of the ReDim (minus the ReDim y variable), but could not get it to work in LibreOffice.  Ran out of time as it is way past my bedtime.


TomW

--
For unsubscribe instructions e-mail to: [hidden email]
Problems? http://www.libreoffice.org/get-help/mailing-lists/how-to-unsubscribe/
Posting guidelines + more: http://wiki.documentfoundation.org/Netiquette
List archive: http://listarchives.libreoffice.org/global/users/
All messages sent to this list will be publicly archived and cannot be deleted
Niklas Johansson Niklas Johansson
Reply | Threaded
Open this post in threaded view
|

Re: ReDim of a variable of a custom type

In reply to this post by Johnny Rosenberg
To empty the array use x = Nothing. So before the ReDim-statement do...


       x = Nothing
       ReDim x(1) As MyType


I think you stumbled upon a bug since your using Option Compatible it should have the same behavior as VBA. In VBA both ReDim statements yields an error. “ReDim x(1) As MyType” because it has already been dimensioned and ReDim y As MyType  because it is not a dynamic Array.
Johnny Rosenberg Johnny Rosenberg
Reply | Threaded
Open this post in threaded view
|

Re: ReDim of a variable of a custom type

2011/8/19 Niklas Johansson <[hidden email]>:

> To empty the array use x = Nothing. So before the ReDim-statement do...
>
> …
>       x = Nothing
>       ReDim x(1) As MyType
> …
>
> I think you stumbled upon a bug since your using Option Compatible it should
> have the same behavior as VBA. In VBA both ReDim statements yields an error.
> “ReDim x(1) As MyType” because it has already been dimensioned and ReDim y
> As MyType  because it is not a dynamic Array.

Okay, thanks everyone for your input. It seems like I have
misunderstood the concept a little bit. I'll take a closer look at it
when I get home after the weekend.

J.R.

--
For unsubscribe instructions e-mail to: [hidden email]
Problems? http://www.libreoffice.org/get-help/mailing-lists/how-to-unsubscribe/
Posting guidelines + more: http://wiki.documentfoundation.org/Netiquette
List archive: http://listarchives.libreoffice.org/global/users/
All messages sent to this list will be publicly archived and cannot be deleted
Johnny Rosenberg Johnny Rosenberg
Reply | Threaded
Open this post in threaded view
|

Re: ReDim of a variable of a custom type

2011/8/20 Johnny Rosenberg <[hidden email]>:

> 2011/8/19 Niklas Johansson <[hidden email]>:
>> To empty the array use x = Nothing. So before the ReDim-statement do...
>>
>> …
>>       x = Nothing
>>       ReDim x(1) As MyType
>> …
>>
>> I think you stumbled upon a bug since your using Option Compatible it should
>> have the same behavior as VBA. In VBA both ReDim statements yields an error.
>> “ReDim x(1) As MyType” because it has already been dimensioned and ReDim y
>> As MyType  because it is not a dynamic Array.
>
> Okay, thanks everyone for your input. It seems like I have
> misunderstood the concept a little bit. I'll take a closer look at it
> when I get home after the weekend.
>
> J.R.
>

I am now at home again, so I did some more tests. Here's the current
example code (I modified the first one a little bit):
REM  *****  BASIC  *****

Option Compatible
Option Explicit

Type MyType
        a As Integer
        b As Integer
End Type
Dim x() As MyType ' Comment 1a: See Comment 2 below.
Dim x(1) As MyType ' Comment 1b: See Comment 2 below.
Dim z() As Integer

Sub Main
        Dim y As MyType
        Dim i As Integer
        ReDim x(1) As MyType ' Comment 2: Gives an error if x was declared as
at Comment 1a,
' and the line at Comment 1b is omitted.
' However, if both lines (1a and 1b) are there, there is no error.
        ReDim z(3) As Integer
        For i=0 To 1
                x(i).a=i*2
                x(i).b=1+i*2
        Next i
       
        For i=0 To 3
                z(i)=6+i
        Next i
       
        y.a=4
        y.b=5

        MsgBox x(0).a & x(0).b & x(1).a & x(1).b & y.a & y.b & _
        z(0) & z(1) & z(2) & z(3) ' Comment 3: Displays 12345678910

        x=Nothing
        ReDim x(1) As MyType
        ReDim z(3)
        ReDim y As MyType ' Comment 4: This doesn't give an error, but it should?
        MsgBox x(0).a & x(0).b & x(1).a & x(1).b & y.a & y.b & _
        z(0) & z(1) & z(2) & z(3) ' Comment 5: Displays 0000000000
End Sub
———End of code———

”x=Nothing” seems to be the easiest solution or workaround at the
moment. The suggestion to define x as a dynamic array didn't work with
MyType. When doing ReDim, see Comment 2 within the code, I got an
error message about the index, that was out of bounds, an error
message I didn't get for z(), which I declared as Integer instead of
MyType.
It seems like I can ReDim x outside the subroutine but not inside it,
see comments 1a, 1b and 2.

So, if I declare something as a custom data type above a subroutine or
function, I can not ReDim it inside a subroutine or function. Why is
that? Or is it a bug? Because I can do that if it's declared as one of
the existing data types, such as Integer.

So after all, it seems like the combination of custom data types and
ReDim doesn't behave in an expected way, does it? So there is maybe a
bug there to report.
And, as already pointed out, accepting ReDim for y in my example is
probably also a bug, right?



Kind regards

Johnny Rosenberg
ジョニー・ローゼンバーグ

--
For unsubscribe instructions e-mail to: [hidden email]
Problems? http://www.libreoffice.org/get-help/mailing-lists/how-to-unsubscribe/
Posting guidelines + more: http://wiki.documentfoundation.org/Netiquette
List archive: http://listarchives.libreoffice.org/global/users/
All messages sent to this list will be publicly archived and cannot be deleted
Johnny Rosenberg Johnny Rosenberg
Reply | Threaded
Open this post in threaded view
|

Re: ReDim of a variable of a custom type

2011/8/23 Johnny Rosenberg <[hidden email]>:

> 2011/8/20 Johnny Rosenberg <[hidden email]>:
>> 2011/8/19 Niklas Johansson <[hidden email]>:
>>> To empty the array use x = Nothing. So before the ReDim-statement do...
>>>
>>> …
>>>       x = Nothing
>>>       ReDim x(1) As MyType
>>> …
>>>
>>> I think you stumbled upon a bug since your using Option Compatible it should
>>> have the same behavior as VBA. In VBA both ReDim statements yields an error.
>>> “ReDim x(1) As MyType” because it has already been dimensioned and ReDim y
>>> As MyType  because it is not a dynamic Array.
>>
>> Okay, thanks everyone for your input. It seems like I have
>> misunderstood the concept a little bit. I'll take a closer look at it
>> when I get home after the weekend.
>>
>> J.R.
>>
>
> I am now at home again, so I did some more tests. Here's the current
> example code (I modified the first one a little bit):
> REM  *****  BASIC  *****
>
> Option Compatible
> Option Explicit
>
> Type MyType
>        a As Integer
>        b As Integer
> End Type
> Dim x() As MyType ' Comment 1a: See Comment 2 below.
> Dim x(1) As MyType ' Comment 1b: See Comment 2 below.
> Dim z() As Integer
>
> Sub Main
>        Dim y As MyType
>        Dim i As Integer
>        ReDim x(1) As MyType '  Comment 2: Gives an error if x was declared as
> at Comment 1a,
> '                               and the line at Comment 1b is omitted.
> '                               However, if both lines (1a and 1b) are there, there is no error.
>        ReDim z(3) As Integer
>        For i=0 To 1
>                x(i).a=i*2
>                x(i).b=1+i*2
>        Next i
>
>        For i=0 To 3
>                z(i)=6+i
>        Next i
>
>        y.a=4
>        y.b=5
>
>        MsgBox x(0).a & x(0).b & x(1).a & x(1).b & y.a & y.b & _
>        z(0) & z(1) & z(2) & z(3) ' Comment 3: Displays 12345678910
>
>        x=Nothing
>        ReDim x(1) As MyType
>        ReDim z(3)

Sorry, a type there. Should have been:
ReDim z(3) As Integer

It didn't matter for this test though.


Kind regards

Johnny Rosenberg
ジョニー・ローゼンバーグ

>        ReDim y As MyType ' Comment 4: This doesn't give an error, but it should?
>        MsgBox x(0).a & x(0).b & x(1).a & x(1).b & y.a & y.b & _
>        z(0) & z(1) & z(2) & z(3) ' Comment 5: Displays 0000000000
> End Sub
> ———End of code———
>
> ”x=Nothing” seems to be the easiest solution or workaround at the
> moment. The suggestion to define x as a dynamic array didn't work with
> MyType. When doing ReDim, see Comment 2 within the code, I got an
> error message about the index, that was out of bounds, an error
> message I didn't get for z(), which I declared as Integer instead of
> MyType.
> It seems like I can ReDim x outside the subroutine but not inside it,
> see comments 1a, 1b and 2.
>
> So, if I declare something as a custom data type above a subroutine or
> function, I can not ReDim it inside a subroutine or function. Why is
> that? Or is it a bug? Because I can do that if it's declared as one of
> the existing data types, such as Integer.
>
> So after all, it seems like the combination of custom data types and
> ReDim doesn't behave in an expected way, does it? So there is maybe a
> bug there to report.
> And, as already pointed out, accepting ReDim for y in my example is
> probably also a bug, right?
>
>
>
> Kind regards
>
> Johnny Rosenberg
> ジョニー・ローゼンバーグ
>

--
For unsubscribe instructions e-mail to: [hidden email]
Problems? http://www.libreoffice.org/get-help/mailing-lists/how-to-unsubscribe/
Posting guidelines + more: http://wiki.documentfoundation.org/Netiquette
List archive: http://listarchives.libreoffice.org/global/users/
All messages sent to this list will be publicly archived and cannot be deleted
Johnny Rosenberg Johnny Rosenberg
Reply | Threaded
Open this post in threaded view
|

Re: ReDim of a variable of a custom type

2011/8/23 Johnny Rosenberg <[hidden email]>:

> 2011/8/23 Johnny Rosenberg <[hidden email]>:
>> 2011/8/20 Johnny Rosenberg <[hidden email]>:
>>> 2011/8/19 Niklas Johansson <[hidden email]>:
>>>> To empty the array use x = Nothing. So before the ReDim-statement do...
>>>>
>>>> …
>>>>       x = Nothing
>>>>       ReDim x(1) As MyType
>>>> …
>>>>
>>>> I think you stumbled upon a bug since your using Option Compatible it should
>>>> have the same behavior as VBA. In VBA both ReDim statements yields an error.
>>>> “ReDim x(1) As MyType” because it has already been dimensioned and ReDim y
>>>> As MyType  because it is not a dynamic Array.
>>>
>>> Okay, thanks everyone for your input. It seems like I have
>>> misunderstood the concept a little bit. I'll take a closer look at it
>>> when I get home after the weekend.
>>>
>>> J.R.
>>>
>>
>> I am now at home again, so I did some more tests. Here's the current
>> example code (I modified the first one a little bit):
>> REM  *****  BASIC  *****
>>
>> Option Compatible
>> Option Explicit
>>
>> Type MyType
>>        a As Integer
>>        b As Integer
>> End Type
>> Dim x() As MyType ' Comment 1a: See Comment 2 below.
>> Dim x(1) As MyType ' Comment 1b: See Comment 2 below.
>> Dim z() As Integer
>>
>> Sub Main
>>        Dim y As MyType
>>        Dim i As Integer
>>        ReDim x(1) As MyType '  Comment 2: Gives an error if x was declared as
>> at Comment 1a,
>> '                               and the line at Comment 1b is omitted.
>> '                               However, if both lines (1a and 1b) are there, there is no error.
>>        ReDim z(3) As Integer
>>        For i=0 To 1
>>                x(i).a=i*2
>>                x(i).b=1+i*2
>>        Next i
>>
>>        For i=0 To 3
>>                z(i)=6+i
>>        Next i
>>
>>        y.a=4
>>        y.b=5
>>
>>        MsgBox x(0).a & x(0).b & x(1).a & x(1).b & y.a & y.b & _
>>        z(0) & z(1) & z(2) & z(3) ' Comment 3: Displays 12345678910
>>
>>        x=Nothing
>>        ReDim x(1) As MyType
>>        ReDim z(3)
>
> Sorry, a type there. Should have been:

Aaaarghhh! I can't type today…! I meant ”typo”, not ”type”…


Johnny Rosenberg

> ReDim z(3) As Integer
>
> It didn't matter for this test though.
>
>
> Kind regards
>
> Johnny Rosenberg
> ジョニー・ローゼンバーグ
>
>>        ReDim y As MyType ' Comment 4: This doesn't give an error, but it should?
>>        MsgBox x(0).a & x(0).b & x(1).a & x(1).b & y.a & y.b & _
>>        z(0) & z(1) & z(2) & z(3) ' Comment 5: Displays 0000000000
>> End Sub
>> ———End of code———
>>
>> ”x=Nothing” seems to be the easiest solution or workaround at the
>> moment. The suggestion to define x as a dynamic array didn't work with
>> MyType. When doing ReDim, see Comment 2 within the code, I got an
>> error message about the index, that was out of bounds, an error
>> message I didn't get for z(), which I declared as Integer instead of
>> MyType.
>> It seems like I can ReDim x outside the subroutine but not inside it,
>> see comments 1a, 1b and 2.
>>
>> So, if I declare something as a custom data type above a subroutine or
>> function, I can not ReDim it inside a subroutine or function. Why is
>> that? Or is it a bug? Because I can do that if it's declared as one of
>> the existing data types, such as Integer.
>>
>> So after all, it seems like the combination of custom data types and
>> ReDim doesn't behave in an expected way, does it? So there is maybe a
>> bug there to report.
>> And, as already pointed out, accepting ReDim for y in my example is
>> probably also a bug, right?
>>
>>
>>
>> Kind regards
>>
>> Johnny Rosenberg
>> ジョニー・ローゼンバーグ
>>
>

--
For unsubscribe instructions e-mail to: [hidden email]
Problems? http://www.libreoffice.org/get-help/mailing-lists/how-to-unsubscribe/
Posting guidelines + more: http://wiki.documentfoundation.org/Netiquette
List archive: http://listarchives.libreoffice.org/global/users/
All messages sent to this list will be publicly archived and cannot be deleted
Andreas Säger Andreas Säger
Reply | Threaded
Open this post in threaded view
|

Re: ReDim of a variable of a custom type

Userdefined structs are pointers. You've got to instanciate one new instance every time you need one and Basic does not support arrays well.
++++++++++++++++++++++++++++++++
REM test routing
Sub Main
REM load one of my helper libs:
REM globalscope.basiclibraries.loadLibrary("myTools")
x() = Array()
REM input loop
for i = 0 to 9
        ms = StructFactory.getNewMyStruct()
        ms.a = "test_"& i
        ms.c = i + i/100
        ms.d = today()
        myTools.Basic.bas_PushArray(x(), ms)
next
REM output loop
for i = 0 to 9
        y = x(i)
        print y.a, y.b, y.c, y.d
next
End Sub
++++++++++++++++++++++++++++++++++++++
REM myTools.Basic
Sub bas_PushArray(xArray(),vNextElement)
Dim iUB%,iLB%
        iLB = lBound(xArray())
        iUB = uBound(xArray())
        If iLB > iUB then
                iUB = iLB
                redim xArray(iLB To iUB)
        else
                iUB = iUB +1
                redim preserve xArray(iLB To iUB)
        endif
        xArray(iUB) = vNextElement
End Sub
++++++++++++++++++++++++++++++++++
REM module StructFactory
Type MyStruct
        a as String
        b as Boolean
        c as Currency
        d as Date
End Type

Function getNewMyStruct()
        getNewMyStruct = new MyStruct
End Function