Issue 47215 - WITH statement does not work correctly
Summary: WITH statement does not work correctly
Status: CLOSED NOT_AN_OOO_ISSUE
Alias: None
Product: General
Classification: Code
Component: scripting (show other issues)
Version: 680m91
Hardware: PC Windows XP
: P3 Trivial (vote)
Target Milestone: OOo 2.0.3
Assignee: ab
QA Contact: issues@framework
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2005-04-12 05:29 UTC by mjneedles
Modified: 2010-10-23 15:36 UTC (History)
1 user (show)

See Also:
Issue Type: DEFECT
Latest Confirmation in: ---
Developer Difficulty: ---


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description mjneedles 2005-04-12 05:29:31 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.
Comment 1 newt 2005-07-18 14:14:03 UTC
This feature works as advertised. Structure members must be preceded by '.' as
in VBA.
Comment 2 mjneedles 2005-07-19 04:37:55 UTC
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.
Comment 3 noel.power 2005-09-02 09:00:34 UTC
reassign to ab
Comment 4 ab 2005-09-02 13:46:28 UTC
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
Comment 5 mjneedles 2005-09-03 06:22:11 UTC
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
Comment 6 ab 2006-03-06 15:23:07 UTC
ab: According to discussion with sw/kso -> OOo 2.0.3
Comment 7 ab 2006-04-13 14:28:43 UTC
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
Comment 8 mjneedles 2006-04-13 22:19:38 UTC
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.
Comment 9 mortmercoleth 2010-10-23 15:36:11 UTC
Created attachment 72571