Thanks for responding!
I tried using the "is" statement, and it doesn't work; after reading up on
the keyword, I'm guessing that it's due to the same reason that's throwing
the InvalidCastException when I try to cast.
Regarding the casting, and the implementation of the interface, as far as I
can tell, I've implemented the same interface in both classes. I _have_
implemented two interfaces, the second of which inherits from the first.
The code is similar to the following (minus obvious references to control
assemblies, etc.):
namespace MyApp.MyInterfaces
{
public interface IMyFirstInterface
{
int MyIntValueProp { get; set; }
object[] MyObjArrayProp { get; set; }
string[] MyStrArrayProp { get; set; }
}
}
namespace MyApp.MyInterfaces
{
public interface IMySecondInterface : IMyFirstInterface
{
MySecondIntProp { get; set; }
string MyStrProp { get; set; }
}
}
using MyApp.MyInterfaces;
using System.Windows.Forms;
namespace MyApp.MyClasses
{
public class MyControlClass : System.Windows.Form.Control,
IMySecondInterface
{
public MyControlClass()
{
// some constructor code
}
// some other code
}
Do you see any problems with this implementation?
"Mattias Sjögren" <mattias.dont.want.spam@mvps.org> wrote in message
news:eMjRex7rDHA.1744@TK2MSFTNGP12.phx.gbl...[color=blue]
>[color=green]
> >Now, in the form code, I figured out how to determine whether or not the
> >control I'm currently accessing from the control array implements my
> >interface. I did this using a call to
> >currentControl.GetType().GetInterface("InterfaceN ame").[/color]
>
> It would be eaiser and faster to do
>
> if ( currentControl is InterfaceName )
>
>[color=green]
> >However, because
> >currentControl is reference as a System.Windows.Forms.Control, rather[/color][/color]
than[color=blue][color=green]
> >as a MyControl, I can't cast to the interface.[/color]
>
> If a cast succeeds or not depends on the actual type of the object,
> not the type of the reference you hold to it. So if the the interface
> is supported, the cast should succeed even though you hold a Control
> reference to it.
>
>[color=green]
> >Unfortunately, this code throws an InvalidCastException. Using a casting
> >statement (i.e. "MyInterface interfaceInst = (MyInterface)
> >controlArray[i];") doesn't work, either.
> >
> >Anyone have any ideas?[/color]
>
> In that case the class apparently doesn't implement the interface. I'm
> not sure why that might be. You haven't used "copy and paste" code
> reuse, have you? In other words, the implementing class and the
> consuming class are using the same definition of the interface, right?
>
>
>
> Mattias
>
> --
> Mattias Sjögren [MVP] mattias @ mvps.org
>
http://www.msjogren.net/dotnet/
> Please reply only to the newsgroup.[/color]