Apache OpenOffice (AOO) Bugzilla – Issue 47215
WITH statement does not work correctly
Last modified: 2010-10-23 15:36:11 UTC
Test this: Type tUserStruct sString1 as String nNumber1 as Single iNumber2 as Integer cNumber3 as Currency bTV1 as Boolean End Type Sub TestWith Dim myStruct as tUserStruct With myStruct nNumber1 = sqr(3.0) print nNumber1 cNumber3 = 2.23606999 * 2.23606999 bTV1 = (1 = 1) ' TRUE print bTV1 print "Tax on " & cNumber3 & " is " & cNumber3 * 0.065 End With End Sub and you'll get a whole bunch of runtime errors (Variable not defined) in the With block. The With <Object> statement should (a la VisualBasic and other variants of Basic), treat the variables referenced inside its block as elements of the referenced Object.
This feature works as advertised. Structure members must be preceded by '.' as in VBA.
What do you mean "as advertised"? Where is it "advertised"? The online help says nothing to indicate this. I have used Visual Basic (not VBA), and many other flavors of Basic, and none of them use the "advertised" behavior. If this is the desired way for Star Basic to work, it should be documented in the online help, and illustrated with an example.
reassign to ab
ab->mjneedles: In a m125 build your example works for me. But it does not work any more if I use "option explicit". So this problem seems to be only related to using "option explicit". -> Started
My example does not work at all under m125 with the code as given. If I prepend the dot to the structure elements, it works partly. Try the following modified code, which demonstrates the failures in Len and With, as well as some other inconsistencies. For example, the Print statements require me to use the complete structured element name instead of the short form with just the dot, or I get a runtime error. REM ***** BASIC ***** REM option explicit ' try it either way :) Type tUserStruct sString1 as String * 10 nNumber1 as Single iNumber2 as Integer cNumber3 as Currency bTV1 as Boolean end type Sub TestWith Dim myStruct as tUserStruct, iLen1 as Integer, iLen2 as Integer iLen1 = Len(myStruct) ' Fails with runtime error, so comment it out to let the rest run Print "Structured variable length is "; iLen1 With myStruct .sString1 = "This is a test" ' this string is too long for the field and should truncate Print .sString1, len(myStruct.sString1)' BUT... it does not, Len returns 14 .nNumber1 = sqr(3.0) iLen2 = Len(myStruct.nNumber1) print .nNumber1, iLen2 'Prints "1.732051 8" but the length should be 4 bytes for a Single .cNumber3 = 2.23606999 * 2.23606999 Print .cNumber3, len(myStruct.cNumber3) ' prints "5.0000 6", where the length should be 8 bytes .bTV1 = (1 < 1) ' FALSE print .bTV1, len(myStruct.bTV1) ' Prints "False 5" but length should be 2 or 4 (not sure which) End With End Sub See also issues 47214 and 47216
ab: According to discussion with sw/kso -> OOo 2.0.3
ab->mjneedles: I had another look and now think this task is invalid. I used a SRC680 m162 to test. To start with your second example: I neither had any problem with nor without option explicit (besides the problems covered by your other tasks of course). Now to your first example: I think, it's simply wrong. You can't access object members inside a with block without a . in front of the name. Without option explicit this sample only works respectively _seems_ to work because all used variables are simply created on the fly when used the first time. But then they are completely different variables and not the Type members that should be accessed. Add "print myStruct.nNumber1" after "print nNumber1" and you will see that myStruct.nNumber1 hasn't been changed at all. With option explicit the code doesn't work at all as the variables cannot be created implicitely. Your code does not work with VB and VBA either. -> INVALID
Okay, I'll concede that in VBA (for MSOffice XP), this is true. I _know_ that I used this without the dot in VB 1.0 and all MS Basic flavors for DOS. I did not know it had changed. Closing for now.
Created attachment 72571