473,396 Members | 1,918 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Can a constructor return Nothing??

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.
Nov 20 '05 #1
3 3600
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" <ei**************@yahoo.com> wrote in message
news:Od**************@TK2MSFTNGP10.phx.gbl...
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.

Nov 20 '05 #2
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" <ei**************@yahoo.com> wrote in message
news:Od**************@TK2MSFTNGP10.phx.gbl...
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.

Nov 20 '05 #3
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.
Nov 20 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Chris K | last post by:
I am relatively new to C++ and hope that this question is relevant. I have spent some time at the local library and some time on dejanews, but have no decided to go ahead with my question, since...
3
by: Pierre Rouleau | last post by:
The std::exception class defined in the Standard C++ <exception> header specifies that the constructors could throw any exception becuase they do not have a throw() specification. Why is that? ...
6
by: Henry | last post by:
I was trying to derive a class from System.Windows.Forms.ComboBox. My goal was to create a class that loaded its own data. I did not want to create too many objects, so I tried to share a Database...
26
by: Patient Guy | last post by:
The code below shows the familiar way of restricting a function to be a method of a constructed object: function aConstructor(arg) { if (typeof(arg) == "undefined") return (null);...
74
by: Zytan | last post by:
I have a struct constructor to initialize all of my private (or public readonly) fields. There still exists the default constructor that sets them all to zero. Is there a way to remove the...
1
by: Bart Simpson | last post by:
Can anyone explain the concept of "slicing" with respect to the "virtual constructor" idiom as explain at parashift ? From parashift: class Shape { public: virtual ~Shape() { } ...
22
by: clicwar | last post by:
A simple program with operator overloading and copy constructor: #include <iostream> #include <string> using namespace std; class Vector { private: float x,y; public: Vector(float u, float...
12
by: Rahul | last post by:
Hi Everyone, I have the following code and i'm able to invoke the destructor explicitly but not the constructor. and i get a compile time error when i invoke the constructor, why is this so? ...
9
by: Morten Lemvigh | last post by:
Is it possible to pass a pointer to a constructor or a class definition as argument to a function? Maybe in a way similar to passing function pointers...? The function should construct a number...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.