By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,344 Members | 1,139 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,344 IT Pros & Developers. It's quick & easy.

Class.New and DB mapping

P: n/a
Hello,

I map objects to a DB.

Currently, I have a shared function like :
clsCustomer.getForId(customerId as integer) as clsCustomer.
It returns Nothing when the customerId does not exist.

I would like to do that within the New :
clsCustomer.New(customerId as integer)

But how can I handle the non-existence of customerId then ?

Is it possible that New returns Nothing ?

Would it be better to throw an Exception ?

Thanks !
Nov 16 '06 #1
Share this Question
Share on Google+
1 Reply


P: n/a

I wouldn't advise you to do anything hardcore like that within the
constructor. Logically, you should throw an exception, because constructing
the object with the required state has failed. You may also consider
setting an "IsValid" flag inside the class set to true or false depending on
constructor success or failure, however that is a slippery slope. My
preferred solution would be:
Dim theClass As New myClass

Result = theClass.LoadCustomer ( theID )

or in the case of your instance factory:

Dim theClass As myClass = clsCustomer.getForId ( customerId )

If theClass Is Nothing Then
' Failed.
End If
There is a lot to be said for being more explicit in code and less
obfuscating ;).

"myname" <no****@ihatespam.comwrote in message
news:45********@news.sncf.fr...
Hello,

I map objects to a DB.

Currently, I have a shared function like :
clsCustomer.getForId(customerId as integer) as clsCustomer.
It returns Nothing when the customerId does not exist.

I would like to do that within the New :
clsCustomer.New(customerId as integer)

But how can I handle the non-existence of customerId then ?

Is it possible that New returns Nothing ?

Would it be better to throw an Exception ?

Thanks !


Nov 16 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.