Can a constructor return Nothing??

  • Thread starter Thread starter Eidolon
  • Start date Start date
E

Eidolon

Is there any way that you can write your constructor so that it returns
nothing?

For example:
Take a Customer object which has a constructor whose constructor has the
signiture:

Public Sub New(CustomerID as String)

What i would like to do is to write it so that the the actual code of the
constructor goes something like the following pseudo-code:

<pcode>
Dim dt as datatable = getDataTable(SELECT * FROM CUSTOMER WHERE CUSTID =
CustomerID)
If dt.Rows.Count = 0 then
return nothing
Else
InitializeMe(dt.Rows(0))
End if
</pcode>

This way from my client code i can do something like:

<code>
Dim tmpCust As Customer = New Customer(MyParameter)
If IsNothing(tmpCust) Then
ShowNoCustMsg
Else
ConitnueOn()
End If
</code>

Thanks in advance.
 
No, I don't believe it can.

Here are a couple of other options:
1) Have a private constructor only. Create a shared method on your class
that creates the object. So users would call it:
Customer.CreateCustomer(MyParameter). This CreateCustomer method could then
create the Customer object, and make sure that your conditions are met. If
the conditions are met, it can return the object, otherwise, it will return
Nothing.
2) set a property on Customer, saying that it is not a valid customer. The
if statement users would use would be: If Not tmpCust.Valid Then
3) Throw an exception saying it's not a valid customer. The user would need
to place the call in a try/catch block.
 
I'm not positive, but I don't believe that a constructor can "return
nothing". However, I do have an alternative suggestion...

Why not use a custom exception? The Customer constructor would look like
so...

Public Sub New(customerID as String)
<pcode>
Dim dt as datatable = getDataTable(SELECT * FROM CUSTOMER WHERE CUSTID =
CustomerID)
If dt.Rows.Count = 0 then
Throw New CustomerDoesNotExistException(customerID)
Else
InitializeMe(dt.Rows(0))
End if
</pcode>

The calling code would look like so...

<code>
Try
Dim tmpCust As Customer = New Customer(MyParameter)
ConitnueOn()
Catch exc as CustomerDoesNotExistException
ShowNoCustMsg(exc.CustomerID)
End Try
</code>

IMHO, the code using the exception looks "cleaner" than the trick with the
nothing.

HTH

- Mitchell S. Honnert
 
Is there any way that you can write your constructor so that it returns
nothing?

The only way I can think of to accomplish that would be to make the
constructor private and create a shared method to create the instance of
the class

Public Class Customer

Private Sub New()
'Constructor code here
End Sub

Shared Function CreateCustomer()
'Code
If SomeCondition Then
Return New Customer
Else
Return Nothing
End If
End Function

End Class

Public Sub Main()
Dim cust As Customer = Customer.CreateCustomer()
If (cust Is Nothing) Then
ShowNoCustMsg
Else
ConitnueOn()
End If
End Sub
 
Eidolon,
Rather then returning Nothing and then needing a lot of checks for Nothing.

I would throw an exception as the others suggest or I would consider the
CreateCustomer Factory Method and the NullObject Pattern.

A NullObject is a specific case of the Special Case pattern:
http://www.martinfowler.com/eaaCatalog/specialCase.html

Basically the Customer.CreateCustomer routine that Marina suggests would
return either new Customer or a new NullCustomer.

The NullCustomer inherits from Customer, however all the methods return
reasonable defaults & all the setters are "ignored". This allows you to code
all your routines so they can operate on a Customer object, without
requiring a lot of:

Dim theCustomer As Customer = Cutomer.CreateCustomer(x)
If theCustomer Is Null then
' don't have a customer do one thing
Else
theCustomer.DoSomethingInteresting()
End If

You can use:

Dim theCustomer As Customer = Cutomer.CreateCustomer(x)
theCustomer.DoSomethingInteresting()

The DoSomethingInteresting routine on NullCustomer would not perform any
real work.

Hope this helps
Jay

P.S. Do not respond to any email your Reply to may have caused, if you want
a response, please post in the newsgroup, this way others can see the
answers & hopefully benefit from your questions. thanks for understanding.
 
Thank you all for the suggestions. I already use the IsValid() property way
in some apps where other properties must be valid even when the actual
object is not. and personally i think Try/Catch doesnt look as nice, ..
disrupts the visual flow.... but thats just preferences. :) . Each of those
methods also has benefits in different situations too of course, whats the
old adage? there is no one silver bullet.

I hadnt thought of the private constructor/class factory method though, i
think thats a good way of implementing the functionality.
Thanks for the advice and help.
Much appreciated.
Cheerz!
 
Back
Top