17.08.2011 16:15, Johnny Rosenberg пишет:
I have an issue where I get error messages for type mismatch sometimes
and sometimes not. After a lot of debugging I found the reason.
The code looks like this, kind of:
Public CatPoints(1 To 20) As Integer
Sub Something
⁝
Dim Points(13) As Integer
⁝
Dim i As Integer
⁝
For i=7 To 20
CatPoints(i)=Iif(MaxiPlayerCategory(i),-1,Points(i-7)) ' An type
mismatch error sometimes occurs here.
⁝
Next i
⁝
End Sub
Function MaxiPlayerCategory() As Boolean
⁝
End Function
The MaxiPlayerCategory function works fine and returns True or False
depending on external things, in this case it returns False, which
means that Points(i-7) is returned by the Iif statement.
So after debugging for quite a while I noticed that the Points()'
array changes its type to double for some elements, so some of them
remains Integer and a few of them are suddenly Double!
Why is that? Well, because the elements of Points() are calculated
differently depending on some stuff that is not important. Some of the
elements are calculated by functions and some are calculated directly,
something like Points(7)=3*something+2*somethingElse.
So when something is just calculated the return value of the operation
seems to be Double no matter what, and arrays seems to adopt to that
automatically.
Of course I can correct this like this:
Points(7)=CInt(3*something+2*somethingElse).
But some of my lines are long enough already and I want compact code
if possible, so my question now is:
Is there some way I can define/declare an array making it to refuse to
change type of its elements? That is, something that makes the return
value of an operation automatically converted to an Integer or that
forces the operation to stay Integer?
By the way, isn't it strange that
”Points(7)=3*something+2*somethingElse” makes Points() change the type
of an element while
”CatPoints(i)=Iif(MaxiPlayerCategory(i),-1,Points(i-7))” produce an
error message instead? Both CatPoints() and Points() are declared the
same way, except that CatPoints() is public to all modules.
I also noticed that automatic type conversion occurs when mixing
Integers with Strings:
Sub Main
Dim x As Integer
Dim y As String
x=10
y=LTrim(Str(x)) ' y="10".
y=x ' y="10". This one is faster than the other one too.
End Sub
Haven't tried with Doubles, but I guess that works too.
Also the other way around works:
Sub Main
Dim x As Integer
Dim y As String
y="10"
x=Val(y) ' x=10.
x=y ' x=10.
End Sub
Kind regards
Johnny Rosenberg
ジョニー・ローゼンバーグ
This code works fine in LibO3.4.2, OOo3.3.0, Go-OO3.2.1, too:
Sub main
msgbox 2 + " " + cStr(10) ' =12
End Sub
vartype() function still gives different results for x and y in the
examples. Maybe the types are converted automatically in some cases if
type mismatch happens? If this happens you don't get any error messages
and all works as if there were no errors.
--
For unsubscribe instructions e-mail to: users+help@global.libreoffice.org
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
Context
Privacy Policy |
Impressum (Legal Info) |
Copyright information: Unless otherwise specified, all text and images
on this website are licensed under the
Creative Commons Attribution-Share Alike 3.0 License.
This does not include the source code of LibreOffice, which is
licensed under the Mozilla Public License (
MPLv2).
"LibreOffice" and "The Document Foundation" are
registered trademarks of their corresponding registered owners or are
in actual use as trademarks in one or more countries. Their respective
logos and icons are also subject to international copyright laws. Use
thereof is explained in our
trademark policy.