PC Review
Forums
Newsgroups
Microsoft DotNet
Microsoft VB .NET
Class.New and DB mapping
Forums
Newsgroups
Microsoft DotNet
Microsoft VB .NET
Class.New and DB mapping
![]() |
Class.New and DB mapping |
|
|
Thread Tools | Rate Thread |
|
|
#1 |
|
Guest
Posts: 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 ! |
|
|
|
#2 |
|
Guest
Posts: 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" <nospam@ihatespam.com> wrote in message news:455c21be$1@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 ! > > |
|
![]() |
|
| Thread Tools | |
| Rate This Thread | |
|
|

Main Page 

.
