Constructor to create nil object

I have a situation where I’d like a new object not to be created when the constructor parameters fail to meet certain conditions. I realize that the object has already been created when the constructor method is called. So is it possible for the constructor to delete (destruct) the object so that the object becomes nil?

In my main program I’d like to be able to do this:

var myObj as new objType(SomeParameters)
if IsNull(myObj) then
  'Take remedial action when SomeParameters are bad
end if

Reason why I want to do this is that it’s less cumbersome for the constructor to check the validity of the input parameters than for the calling program to do it.

You should set a boolean that you can check afterward.

var myObj as new objType(SomeParameters)
if myObj.IsValid then

Add a constructor to your class & make it private - this forces you to use the Shared method below.
Add a shared method to your class, call it what you will (I use GetInstance as the method name) & pass the parameters.
Validate the parameters in the shared method & either create an instance of your class & return it or return nil.
In your code you would use

var myObj as objType = objType.GetInstance(SomeParameters)
if IsNull(myObj) then
  'Take remedial action when SomeParameters are bad
end if

HTH
Wayne

5 Likes

What might be the “remedial action” you’ll take if the parameters are bad? How will your code know what to do in order to properly satisfy the constructor?

Thanks for the replies so far. Here is a bit more information. One of the parameters of the constructor is an instance of the same class ObjType. The intention is for the constructor to duplicate the argument object with a few changes. It’s possible that the parameter object may be nil, in which case I’d like the new object to be nil as well. Perhaps this is a bad idea, but it would make my life easier, if the parameter object is nil, simply to return nil for the new object.

You can do exactly that with Wayne’s solution.
Using a shared method with a private constructor.

2 Likes

This is what exceptions are for. Create and raise an exception in the constructor and let the calling code decide the best action to take.

5 Likes

It’s not a great idea. :wink: Why not just check for the parameter being nil beforehand? You’re going to end up checking the returned value for nil anyhow. Unless there are other reasons the parameter can be unsuitable.

I see the IsNull function in use here.
I always used something like this: If myObj = Nil Then…

Is there any difference? Or is the IsNull function just the preferred way? :thinking:

i use mostly:

If myObj Is Nil Then // checks if it's a reference to nil directly
// This is my preferred way if you want to skip operator_convert which obviously is the case if you would check for Nil 
End if
// Also 
if Not (myObj Is Nil) Then
// reversed
End If

And using “=” gives operator convert availability

If myObj = Nil Then // checks if it's a reference to nil directly by operator_convert (if this is available)
End If

The IsNull is just a different function that accepts a variant so it may be more flexible (i guess it’s much slower)
https://documentation.xojo.com/api/language/isnull.html#isnull

2 Likes

Thanks for all of the replies. I decided that, as advised, trying to return a nil object is not a good idea, and I was able to come up with a way to avoid doing it without complicating the rest of the code.

I’m still interested in Wayne’s method, and I intend to explore it further for possible future use.

On a related note, Wayne’s method is also a way to maintain a singleton – a class where only one instance exists at a time.

Private Method Constructor()
    'the Constructor is private so it can't be instantiated
    'without going through the GetInstance method
End

Private Shared Property singleton as NewClass

Public Shared Function GetSharedInstance() as NewClass
    If singleton=nil Then
        singleton=new NewClass()
    End

    Return singleton
End
3 Likes

And sometimes you might need to reset the singleton

Public Shared Function GetSharedInstance(reset As Boolean = false) as NewClass
    If singleton is nil or reset = True then
        singleton=new NewClass()
    End

    Return singleton
End
1 Like