'Object variable not set' for variant parameters

Keyboard macros or custom scripts

'Object variable not set' for variant parameters

Postby sqykly » Tue Dec 30, 2008 4:48 am

My OOo Basic macros include a lot of functions that I wrote to get text from standard ASCII text files and embedded text in binaries via "open ... for binary", but also need to handle text in OOo Writer documents as objects. In this case and in many other cases, such as sort functions that deal with objects as well as primitives, and functions that insert strings in a document alongside text content objects, I thought it was reasonable to pass the object or primitive to the procedure in question using a variant parameter, e.g.

Code: Select all   Expand viewCollapse view
Sub inSort(anElement as variant, aList as object)
...
Dim anIndex as Long
anIndex = sortIndex(anElement)
...
End Sub

Function sortIndex(anElement as variant) as Long
If isObject(anElement) then
sortIndex = anElement.sortIndex
elseif isNumeric(anElement) then
sortIndex = anElement
else
sortIndex = len(anElement)
Endif
End Function


This works just fine when I give it an object variable (with a sortIndex property, of course), but I get an 'Object variable not set' error when I give it any other type. The error occurs on the call to sortIndex(...), not on the call to inSort(...), so I know that Basic is concerned about my use of the dot operator, disregarding my use of the Variant (or even the Any type) as well as my use of the isObject function as a condition.

The error also occurs any time the Null type or the Nothing object is passed to a procedure. This is less a pain to work around.

That accounts for about 96% of the errors I encounter during debugging, so if anyone could tell me how to coax Basic into letting me use the same parameter for both object and primitive types, it would be abundantly appreciated.
OOo 3.0.X on Ms Windows XP
sqykly
 
Posts: 27
Joined: Mon Nov 24, 2008 8:28 am

Re: 'Object variable not set' for variant parameters

Postby Villeroy » Tue Dec 30, 2008 4:25 pm

Reminds me of a bug I reported some years ago: [Issue 65555] New - Basic function does not accept variants
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 12.04, Apache OpenOffice 4.x
User avatar
Villeroy
Volunteer
 
Posts: 17278
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: 'Object variable not set' for variant parameters

Postby sqykly » Wed Dec 31, 2008 2:18 am

It does look a lot like that. I thought I'd add a new manifestation of this same error that came across last night, in which putting an Object in a Variant, then replacing it with a string, causes an "Invalid use of Object variable" error. I really really need to be able to do this because I'm converting binary text in pieces and inserting text fields at irregular intervals. The text has to be inserted as a string, and the text fields have to be inserted as objects. The item to be inserted has to be processed within the same function, and since a different function decides whether each chunk is a text field or a string, the same variable has to be able to store either a string or an object. The ability to do this is sort of a key feature of the Variant type, as well as part of the central idea of Basic itself.

Any ideas? Please?

Edit: Thought I'd include an example for clarity

Code: Select all   Expand viewCollapse view
Function Chunk as Variant
static sequence as object
'sequence is a queue in which every element is a string

dim element as string
if not sequence.isEmpty then
element = sequence.nextElement
else
Chunk = Empty
exit function
endif

if instr(element, "<") then
Chunk = createObject("com.sun.star.Text.Textfield.Macro")
'I think that's the service, I have it as a public const, but it's not important
'The important part is that the function returns an object in this case
'....
elseif element <> chr(0) then
Chunk = element
'In this case, the function returns a string
'...
else
Chunk = Empty
'This is how the other function knows the sequence is over
endif

Sub Foo as Object
dim bar as variant
'...
bar = Chunk
do until isEmpty(bar)
'...
bar = Chunk
'...
loop
'...
end sub


Let's say this runs and the first time through, Chunk returns an Object, and the next, it returns a string. That causes an error.
OOo 3.0.X on Ms Windows XP
sqykly
 
Posts: 27
Joined: Mon Nov 24, 2008 8:28 am

Re: 'Object variable not set' for variant parameters

Postby B Marcelly » Sun Jan 25, 2009 3:51 pm

Hi,
This works just fine when I give it an object variable (with a sortIndex property, of course), but I get an 'Object variable not set' error when I give it any other type. The error occurs on the call to sortIndex(...), not on the call to inSort(...), so I know that Basic is concerned about my use of the dot operator, disregarding my use of the Variant (or even the Any type) as well as my use of the isObject function as a condition.

This is Issue 70616, not yet corrected. A very nasty bug.
Solution: use an intermediary variable. Correction of your first code:
Code: Select all   Expand viewCollapse view
Function sortIndex( anElement2 as variant) as Long
Dim anElement As Variant
anElement = anElement2

If isObject(anElement) then
  sortIndex = anElement.sortIndex
elseif isNumeric(anElement) then
  sortIndex = anElement
else
  sortIndex = len(anElement)
Endif
End Function

______
Bernard
B Marcelly
 
Posts: 773
Joined: Mon Oct 08, 2007 1:26 am
Location: France, Paris area

Re: 'Object variable not set' for variant parameters

Postby sqykly » Mon Jan 26, 2009 9:24 am

Hey, thanks! Can't believe I didn't think of that! What I ended up doing was just adding an index property to my list nodes, then adding an optional parameter to manually set the element's index manually when it's inserted. In a lot of other cases that didn't involve a list or sorting or whatever, I had to make a box type with one variant property and pass that between functions. As an aside, does anyone know if declaring variables as 'any' is different than variant? It would be nice to have something analogous to a generic without wasting memory, but it's Basic, so I'm not getting my hopes up.
OOo 3.0.X on Ms Windows XP
sqykly
 
Posts: 27
Joined: Mon Nov 24, 2008 8:28 am

Re: 'Object variable not set' for variant parameters

Postby B Marcelly » Mon Jan 26, 2009 6:27 pm

Hi,
sqykly wrote:does anyone know if declaring variables as 'any' is different than variant? It would be nice to have something analogous to a generic without wasting memory, but it's Basic, so I'm not getting my hopes up.

I was not aware that any was accepted as a Basic type.
It is apparently same as Variant.
The Dev'Guide says that UNO any is bound to Basic Variant.
As to wasting memory, is it still a problem nowadays?
______
Bernard
B Marcelly
 
Posts: 773
Joined: Mon Oct 08, 2007 1:26 am
Location: France, Paris area


Return to OpenOffice Basic, Python, BeanShell, JavaScript

Who is online

Users browsing this forum: No registered users and 4 guests