Interface returns user defined type??? 
Author Message
 Interface returns user defined type???

How to implement an interface that needs to return a user
defined type.  In the following, class NNN.Menu implements
IMenu.  Method GetFeatures returns an ArrayList of a user
defined type, which is defined in the implementing class.  
This does not work because the clients would only have the
interface not the implementation.  It does not know the
class FeaturePath.  

How does the server communicate with the client to
understand the user defined type?   Thank you.

interface IMenu
{
  ArrayList GetFeatures( int fid );

Quote:
}

namespace NNN
{
  public class FeaturePath
  {
    public int    Id;
    public string  UrlPath;
    ......
  }

  public class Menu : IMenu
  {
    public ArrayList GetFeatures( int fid )
    {
      ArrayList afp = new ArrayList();
      for( int i = 0; i < 10; i++ )
      {
        FeaturePath fp = new FeaturePath();
        fp.Id          = i;
        fp.UrlPath     = "url" + i.ToString();

        afp.Add( fp );
      }
      return afp;
    }
  }

Quote:
}



Mon, 18 Apr 2005 14:58:58 GMT  
 Interface returns user defined type???

Quote:

> How to implement an interface that needs to return a user
> defined type.  In the following, class NNN.Menu implements
> IMenu.  Method GetFeatures returns an ArrayList of a user
> defined type, which is defined in the implementing class.  
> This does not work because the clients would only have the
> interface not the implementation.  It does not know the
> class FeaturePath.  

Well, to put it simply, the client will *have* to have the FeaturePath
class. If you're putting an object in an ArrayList and the client
doesn't know anything about that object's class, it's going to have no
way of using it.

You could make FeaturePath implement another interface, and only expose
that *interface* to the client, however.

That's in terms of just "calling code" vs "library code" though - when
it comes to client/server communications, I believe you'll need to
specify how you're communicating first - if you're remoting, you may be
able to just get away with exposing the interface (I'm not sure). If the
object itself is actually passed down the wire, then the client will
need the actual concrete class.

--

http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too



Mon, 18 Apr 2005 16:09:54 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Returning user defined data types

2. Convert C++ (return type int, long, user-define, etc) function into COM/ATL

3. Returning a user-defined class

4. Use of enums for user defined types

5. User Defined Data Types and Pointers

6. HELP! user-defined data types

7. User defined types

8. User defined Type in ATL

9. Passing User defined data Type in ATL

10. An array of user defined types

11. User Defined Types in Safearrays

12. Passing user defined type var between VB and VC

 

 
Powered by phpBB® Forum Software