views:

112

answers:

3

Well,

the question is kinda simple.

I have a object defined as:

public class FullListObject : System.Collections.ArrayList, IPagedCollection

And when i try to:

IPagedCollection pagedCollection = (IPagedCollection)value;

It don't work... value is a FullListObject... this is my new code trying to get around a issue with the "is" operator. When the system tests (value is IPagedCollection) it never gets true for FullListObject.

How to cast the object to another object with a interface type?

EDIT:

Just for the record: the bugger code

if (value is IPagedCollection)
{
    IPagedCollection pagedCollection = value as IPagedCollection;

The if was never hitting true, and forcing the conversion wasn't working too. So the issue was the double definition of classes. Now i defined the FullObjectList in a "Commom" project for classes used by the whole system. Problem gone!

+6  A: 

You're doing it right. Try this (it will fail also but show the problem):

IPagedCollection pagedCollection = (FullListObject)value;

The compiler should accept this just fine. If not, you have multiple definitions of either IPagedCollection and/or FullListObject which conflict each other. If that fails at runtime, your value is not a FullListObject.

Lucero
Right on the money. It appears that the developers from the project defined FullListObject in two pages like a public class. Before asking this question i've search for "find symbol" but only one page came up on the results. Finding by text cleared the path for me.Thanks!!!
jaderanderson
You're welcome... :)
Lucero
A: 

Your code should work fine - I may not be understanding what you're trying to accomplish.

You don't need to cast value in that assignment. Just doing

    IPagedCollection pagedCollection = value;

will be sufficient - pagedCollection will be declared as an IPagedCollection, and contain a FullListObject. The checks using the is operator should return true. Doing the reverse as Lucero suggested will tell you whether you actually have a FullListObject like you expect. Could you perhaps show us your code with the checks so we can see what your goal is?

Tesserex
Lucero has hit the spot. It was a double definition issue that has locked the cast and IS operation.I'll post the original bugging code in minutes. Thanks for your time!
jaderanderson
A: 

Does FullListObject explicitly implement IPagedCollection? If so, then according to Msdn

A class that implements an interface can explicitly implement a member of that interface. When a member is explicitly implemented, it cannot be accessed through a class instance, but only through an instance of the interface.

To identify an explicit implementation, inspect interface member implementations. For example,

public interface IService
{
    void Start ();
    void Stop ();
}

public class Service : IService
{
    #region IService Members

    // note interface-dot-membername signature. explicit
    // implementations *explicitly* declare the interface
    // they are members of. this allows an implementation
    // to contain members of the same name but of different
    // interface declarations
    void IService.Start () { }
    void IService.Stop () { }

    #endregion
}

If this is indeed the case, then you have one of two options,

  1. Change FullListObject to implement IPagedCollection implicitly, or
  2. Cast your FullListObject to an instance of IPagedCollection as you have done
johnny g
Thanks for the feedback :DI've tried explicit defining before too. But the issue was redundant public definitions of the same class. I would vote you up if i could!
jaderanderson
no worries - glad you got sorted though! :)
johnny g