On Apr 16, 2007, at 2:13 PM, Dennis Birch wrote:

> On 4/16/07, Charles Yeomans <[EMAIL PROTECTED]> wrote:
>>
>> 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
>
>
> This is becoming clearer to me, thanks to your and Norman's
> perseverance. There's at least one more point I'm confused on. Without
> having tried this, it seems like the last code snippet would be
> calling a nil object. In other words, it looks like:
>
> dim foo as SomeClass = SomeClass.NewSomeClass
>
> is calling a method in an instance of an object that hasn't yet been
> created. Have you left out a step, or does it just work? I've never
> used shared methods before, so please pardon my ignorance.

Shared methods belong to the class, not an object of the class.  So  
you call them using the syntax

ClassName.MethodName.

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