From: shapper on
Hello,

Is there a difference between using:
controller = ObjectFactory.GetInstance(controllerType) as
IController;

Or
controller = (IController)ObjectFactory.GetInstance(controllerType);

I see the first one with a VB similar sintax and the second one as C#.
But C# accepts both ...

How should I use it?

Thank You,
Miguel
From: Arne Vajhøj on
On 16-05-2010 18:07, shapper wrote:
> Is there a difference between using:
> controller = ObjectFactory.GetInstance(controllerType) as
> IController;
>
> Or
> controller = (IController)ObjectFactory.GetInstance(controllerType);
>
> I see the first one with a VB similar sintax and the second one as C#.
> But C# accepts both ...
>
> How should I use it?

The first return null if cast not possible the last throw an
exception if cast not possible.

In most cases I will recommend the last, because it gives
much easier to find errors.

Arne
From: Jackie on
On 5/17/2010 00:07, shapper wrote:
> Hello,
>
> Is there a difference between using:
> controller = ObjectFactory.GetInstance(controllerType) as
> IController;
>
> Or
> controller = (IController)ObjectFactory.GetInstance(controllerType);
>
> I see the first one with a VB similar sintax and the second one as C#.
> But C# accepts both ...
>
> How should I use it?
>
> Thank You,
> Miguel

The first one will simply return null and not throw an exception if the
cast fails, while the other one will.
From: shapper on
On May 16, 11:15 pm, Jackie <Jac...(a)an.on> wrote:
> On 5/17/2010 00:07, shapper wrote:
>
>
>
> > Hello,
>
> > Is there a difference between using:
> >    controller = ObjectFactory.GetInstance(controllerType) as
> > IController;
>
> > Or
> >    controller = (IController)ObjectFactory.GetInstance(controllerType);
>
> > I see the first one with a VB similar sintax and the second one as C#.
> > But C# accepts both ...
>
> > How should I use it?
>
> > Thank You,
> > Miguel
>
> The first one will simply return null and not throw an exception if the
> cast fails, while the other one will.

Thank you for the clarification
 | 
Pages: 1
Prev: Application Domain
Next: Help Break-In Mockina