PC Review


Reply
Thread Tools Rate Thread

Can a constructor return Nothing??

 
 
Eidolon
Guest
Posts: n/a
 
      11th May 2004
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.


 
Reply With Quote
 
 
 
 
Marina
Guest
Posts: n/a
 
      11th May 2004
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.
"Eidolon" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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.
>
>



 
Reply With Quote
 
Mitchell S. Honnert
Guest
Posts: n/a
 
      11th May 2004
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


"Eidolon" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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.
>
>



 
Reply With Quote
 
Chris Dunaway
Guest
Posts: n/a
 
      11th May 2004
On Tue, 11 May 2004 09:06:41 -0400, in microsoft.public.dotnet.languages.vb
you wrote:

> 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

--
Chris

To send me an E-mail, remove the "[", "]", underscores ,lunchmeat, and
replace certain words in my E-Mail address.
 
Reply With Quote
 
Jay B. Harlow [MVP - Outlook]
Guest
Posts: n/a
 
      11th May 2004
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.

"Eidolon" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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.
>
>



 
Reply With Quote
 
Eidolon
Guest
Posts: n/a
 
      11th May 2004
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!


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to return null from c# class constructor after a validation? Saravanan Microsoft C# .NET 12 24th Mar 2008 10:03 PM
Calling overloaded constructor with values inside another constructor Tristan Microsoft C# .NET 2 30th Mar 2004 01:05 PM
Constructor return value? =?Utf-8?B?Qm9uag==?= Microsoft VC .NET 7 12th Feb 2004 11:49 AM
beginner C# constructor, how to return null ? Romain TAILLANDIER Microsoft Dot NET Framework 3 6th Oct 2003 09:44 PM
Does DataView(DataTable) constructor just return DataTable.DefaultView? Neil Price Microsoft ADO .NET 1 22nd Sep 2003 12:05 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 05:30 PM.