Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / Interop / March 2007

Tip: Looking for answers? Try searching our database.

Exposing "Count" in a custom class

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Dave Young - 09 Mar 2007 19:32 GMT
C#, VS2003, 1.1 Framework

I have a custom class implementation thats based on
System.Collections.CollectionBase.

I've implemented the Add, Remove, Contains, CopyTo etc... all the normal
stuff.  This also has to be registered for COM Interop so classic ASP pages
can access it's properties and methods.  Everything works, with the
exception of "Count"  I've tried using the keyword "new" but that doesn't
help either.  The error message begin returned is that the object doesn't
support the property or method "Count".

This one eludes me.  I've tried just defining the Count property in the
Interface declaration, but that doesn't work either.  I also wonder if
perhaps I could just implement ICollection instead.

Sample

[Guid("E9C924C6-540D-4b36-94DB-79B3244C9907")]
[InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
public interface ISimpleList
{
 [DispId(1)]
 int Add(object item);
 [DispId(2)]
 void Delete(object item);
 [DispId(3)]
 bool Contains(object item);
 [DispId(4)]
 void CopyTo(object[] array, int index);
 [DispId(5)]
 object this[int index]{get;}
 [DispId(6)]
 int Count{get;}
 [DispId(7)]
 IEnumerator GetEnumerator();
}
[Guid("E7231AC1-9215-42e3-B86E-7AA2FEDD4011")]
[ClassInterface(ClassInterfaceType.None)]
[ProgId("ssiAcres.SimpleList")]
public class SimpleList : System.Collections.CollectionBase, IEnumerable,
ICollection, ISimpleList
{
 public SimpleList(){}
 public int Add (object item)
 {
  return List.Add(item);
 }

 public void Delete (object item)
 {
  List.Remove(item);
 }

 public bool Contains(object item)
 {
  return List.Contains(item);
 }

 public void CopyTo(object[] array, int index)
 {
  List.CopyTo(array, index);
 }

 public object this[int index]
 {
  get{return (object)this.List[index];}
 }

 IEnumerator System.Collections.IEnumerable.GetEnumerator()
 {
  return List.GetEnumerator();
 }

 public int Count
 {
  get{return List.Count;}
 }
}
Dave Young - 12 Mar 2007 22:43 GMT
Resolved

Rather than inherit from CollectionsBase, I just just wrote the ISimpleList
definition to implement IList, IEnumerable and ICollection.  Then, I went
back and changed the class definition to implement ISimpleList instead.
That fixed it.

> C#, VS2003, 1.1 Framework
>
[quoted text clipped - 75 lines]
>  }
> }

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.