On Apr 16, 2007, at 1:52 PM, Dennis Birch wrote:

> On 4/16/07, Norman Palardy <[EMAIL PROTECTED]> wrote:
>>
>> On 16-Apr-07, at 11:38 AM, Dennis Birch wrote:
>>
>>> On 4/16/07, Charles Yeomans <[EMAIL PROTECTED]> wrote:
>>>>
>>>> On Apr 16, 2007, at 12:26 PM, Dennis Birch wrote:
>>>>
>>>>> Is it possible to not return an instance of a class from its
>>>>> constructor?
>>>
>>>> Not exactly.   If you want to return nil if the constructor fails,
>>>> then you can define a shared method NewSomeClass(rs as  
>>>> RecordSet) as
>>>> SomeClass and call it.
>>>
>>> Thanks Charles. I'm a little confused about this approach. Can you
>>> elaborate?
>>
>> You don't use the constructor directly.
>> You use a shared method that returns an instance of your class.
>> It becomes a factory (something that makes instances of the class)
>
> Thanks Norman. In other words, you're talking about creating a
> fallback that builds an instance of my class with default values?

Not quite.  You should do something like the following.


Sub Constructor(rs as RecordSet)
   if rs is nil then
     raise new RuntimeException //replace with a more descriptive  
subclass
   end if
   if rs.Field("Foo") is nil then
     raise new RuntimeException //replace with a more descriptive  
subclass
   end if

   //now that you have verified parameter, proceed with initialization
End Sub


Some people prefer to test the return value of a function for nil  
instead of catching exceptions.  NewSomeClass is a shared method.


Shared Function NewSomeClass(rs as RecordSet) as SomeClass
   return new SomeClass

exception theError as RuntimeException
   return nil
End Function


In other code, you might write

dim foo as SomeClass = SomeClass.NewSomeClass
if foo is nil then
...


Charles Yeomans
_______________________________________________
Unsubscribe or switch delivery mode:
<http://www.realsoftware.com/support/listmanager/>

Search the archives:
<http://support.realsoftware.com/listarchives/lists.html>

Reply via email to