Page 1 of 1

[Solved] Assigning NULL to an Object [Calc] Use NOTHING

Posted: Wed Feb 22, 2017 6:53 pm
by Herb40
I apparently have a fundamental misunderstanding about my failure to be able to assign the NULL value to an Object. In the following code, the spreadsheet mSS and the sheet mSheet are assigned a SS object and a sheet object, as expected. But my code is not allowed to assign NULL to mSheet in the last line of code. Instead, I get the error message "Object variable not set." I thought NULL was a special value that could be assigned to something, but the error message seems to be saying that NULL is an object variable that has not been set. ???

I need to pass mSheet to another function (in a more complicated situation than shown here). Sometimes there is an actual sheet but at other times there is no sheet, and I need to pass a Null value to indicate this. After the SS is closed, mSheet still refers to the original object, as one would expect, since mSS.close(True) does not "know" what use was being made of its sheets.

What's a workable way to do this? As I said, I seem to have a basic misunderstanding about this. Thanks for your help.

Code: Select all

Sub NullTest
	Dim mSS as Object
	Dim mSheet as Object

	mSS = StarDesktop.LoadComponentFromUrl( "private:factory/scalc", "_blank", 0, Array())
	mSheet = mSS.Sheets(0)	
	mSS.close(True)
	mSheet = Null   ' fails: Object variable not set
End Sub

Re: Assigning NULL to an Object [Calc]

Posted: Wed Feb 22, 2017 7:37 pm
by RoryOF
I'm fairly sure NULL is not assignable. Perhaps you should test for the existence of mSS.

This thread may be helpful
viewtopic.php?f=25&t=58429

Re: Assigning NULL to an Object [Calc]

Posted: Wed Feb 22, 2017 7:54 pm
by RPG
Hello

In BASIC there is an object NOTHING. Maybe it can work.

Romke

Re: Assigning NULL to an Object [Calc]

Posted: Wed Feb 22, 2017 8:23 pm
by Herb40
Thanks, fellows!

Here's an example of a typical situation I have:

Code: Select all

if mSS.Sheets.hasByName("sheet name") then
  mSheet = mSS.Sheets.getByName("sheet name")
else
  mSheet = NOTHING
end if
MyFunction(mSS, mSheet, ...)
I need to discover in MyFunction whether the second argument is Null.

Fortunately, using NOTHING works just fine in the assignment to mSheet. 'Never heard of it before.

Re: [Solved] Assigning NULL to an Object [Calc] Use NOTHING

Posted: Wed Feb 22, 2017 11:30 pm
by JeJe
The explanation is here (OOBasic looks to be the same as VB6 here)

http://stackoverflow.com/questions/2327 ... ing-in-vb6

Null is a type of variant. Objects can't be null
dim a as variant
a = null 'fine
dim b as object
b= null 'error - object variable not set

Re: [Solved] Assigning NULL to an Object [Calc] Use NOTHING

Posted: Thu Feb 23, 2017 12:11 am
by Herb40
Thanks for the info, JeJe.

I like Null more than Nothing. So I plan to change some of my Objects to Variants so I can set them to Null instead of Nothing. Andy Pitonyak says to use Variant, but I always figured that "the Basic system" would spend extra time trying to determine which flavor of Variant was being used.

Re: [Solved] Assigning NULL to an Object [Calc] Use NOTHING

Posted: Fri Feb 24, 2017 2:26 am
by Villeroy
Today's Basic is Python. Your office suite comes with a whole Python runtime as an alternative macro language with batteries included (lots of libraries for everyday's tasks). Python can be used for macros, stand-alone programs and for office extensions. In Python None is what Empty, Missing, Null and Nothing is in Basic. Python is by far easier to learn than Basic and much more powerful at the same time.

Re: [Solved] Assigning NULL to an Object [Calc] Use NOTHING

Posted: Fri Feb 24, 2017 6:01 pm
by Herb40
Thanks, Villeroy, for taking the time to reply to my post and writing your suggestion that I should be using Python instead of Basic.

After a couple of years in the use of OpenOffice, I realize that I may have been better off starting with some language other than Basic. But with so much of the OO documentation suggesting Basic, and with the Basic IDE that works like a charm to an "old-tyme" programmer like me, I went with Basic, and now have a functional, sophisticated system all written in Basic. I appreciate all of the help I have received from this forum in making this possible.