By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,498 Members | 1,564 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,498 IT Pros & Developers. It's quick & easy.

Question on Casting to a BaseType

P: n/a
I created a control that derives from the System.Windows.Forms.Control
namespace.
I then created an interface to which this new control must adhere, so that I
could require future controls to provide certain functionality.

In my form code, I'm not going to be sure of what types of controls the form
will be using until run-time. So, I created an array of controls, all cast
as Control. This all works fine, so far.

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("InterfaceNa me"). However, because
currentControl is reference as a System.Windows.Forms.Control, rather than
as a MyControl, I can't cast to the interface.

Does anyone know a way of casting to the interface and accessing the
properties that the interface defines? Obviously, it's going to be very
counter-productive to have to create a whole set of custom controls that
merely inherit from the various Windows forms controls, just so that I can
make sure that all of the implement my interface.

The closest MSDN help article I could find is this:
<ms-help://MS.VSCC/MS.MSDNVS/vbcon/html/vbconwhenshouldiimplementinterfacesi
nmycomponent.htm>, and indicates code like the following:

BusinessAccount business = new BusinessAccount();
IAccount account = business;
account.PostInterest();

Here's some sample code:

using System.Windows.Forms

Control[] controlArray;

..... get controls into the array, from various sources....

if (controlArray[i].GetType().GetInterface( "MyInterface", true ) != null )
{
MyInterface interfaceInst = controlArray[i];
int propertyValue = interfaceInst.Property1;
}

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?

Thanks,

Chris
Nov 15 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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").
It would be eaiser and faster to do

if ( currentControl is InterfaceName )

However, because
currentControl is reference as a System.Windows.Forms.Control, rather than
as a MyControl, I can't cast to the interface.
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.

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?


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.
Nov 15 '05 #2

P: n/a
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" <ma********************@mvps.org> wrote in message
news:eM**************@TK2MSFTNGP12.phx.gbl...
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").


It would be eaiser and faster to do

if ( currentControl is InterfaceName )

However, because
currentControl is reference as a System.Windows.Forms.Control, rather thanas a MyControl, I can't cast to the interface.


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.

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?


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.

Nov 15 '05 #3

P: n/a
Hi Chris,
In addition to Mattias's question:
1. are you sure that all objects in controllArray are implementing the
interfaces?
2. Here in the group somewhere I have read something interesting about
"is" keyword, i.e. if you are planning to use "is" only as preparation
to a cast, its better to use "as", so maybe you should try:

instead

Control[] controlArray;

.... get controls into the array, from various sources....

if (controlArray[i].GetType().GetInterface( "MyInterface", true ) != null )
{
MyInterface interfaceInst = controlArray[i];
int propertyValue = interfaceInst.Property1;
}


try this:

MyInterface obj = controlArray[i] as MyInterface;
int propertyValue;
if (obj != null)
{
propertyValue = obj.Property1;
}

Sunny
Nov 15 '05 #4

P: n/a
Chris,
Do you see any problems with this implementation?


No, other than the fact that the MySecondIntProp is lacking a return
type, but I assume that's just a typo.

Is the interface defined in a separate assembly? If so, double check
that you're referencing the same version of that assembly.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/
Please reply only to the newsgroup.
Nov 15 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.