2011/8/17 Clio <menenem@bk.ru>:
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.
Well, my experiments indicates that as soon as there is a mathematical
operation going on (+, -, *, /, ^, math functions), the result is a
Double.
If I do them in a function and the function returns an Integer, the
return value is of course an Integer, which means that the return
value is converted automatically to Integer when the function return
the value.
In my little project I solved it by putting every operation within a
CInt(). The conversion will probably take some time, but probably not
more time than a function return takes to convert to an Integer, if
Integer is its return value.
I remember the 1980's when I played with an ABC80 computer. I remember
that everytime I only need integers, you could force it to keep to
integers, which was two bytes instead of five I think, making the
programs quite a lot faster. But in this case that is not possible. As
far as I'm calculating, a type conversion occurs and then I have to
convert it back again. No speed advantage there, I guess. Maybe
keeping to Double is faster, I haven't done any speed tests in that
matter. Yet…
I wish there was a way to indicate that numbers are Doubles or
Integers. In the old days I did that with %: i%=i%+1% was faster than
i=i+1
I tried that here, but the result was still converted to Double. I am
not sure why, because it's not really needed as long as we only use
integers and as long as we don't divide.
Kind regards
Johnny Rosenberg
ジョニー・ローゼンバーグ
--
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.