VB/C# Retrieval Collection problem

PiggyBank1974

Newcomer
Joined
Sep 28, 2004
Messages
9
Hi There,

I'm using the (Compact framework)

As I said previous I thought I would dabble in c# for my new project, and as I thought its turning out to be a nightmare :eek: I created this control in VB and now I'm porting it to c#.

In my control I have a PageListCollection (Similar to Listviews ListViewCollection)

Here is the VB version:
Visual Basic:
 Public Class PageItemCollection
 Private mPages As List(Of PageItem)
 Private mPageList As PageList

  Public Sub New(ByVal oPageList As PageList)
   mPageList = oPageList
  End Sub

  Private ReadOnly Property Pages() As List(Of PageItem)
   Get
      If Me.mPages Is Nothing Then
        mPages = New List(Of PageItem)
      End If
      Return mPages
   End Get
  End Property

  Public Function Add(ByVal [Text] As String) As PageItem
  Dim mItem As New PageItem([Text])
   Pages.Add(mItem)
   Return mItem
  End Function

  Public Function Add(ByVal Item As PageItem) As PageItem
  Dim mItem As PageItem = Item
   Pages.Add(Item)
   Return mItem
  End Function

  Public ReadOnly Property Count() As Int32
   Get
      Return IIf(Pages Is Nothing, 0, Pages.Count)
   End Get
  End Property

  Public Sub Clear()
   Pages.Clear()
  End Sub

  Default Public ReadOnly Property Item(ByVal Index As Integer) As PageItem
   Get
      Return Pages.Item(Index)
    End Get
  End Property
 End Class

And the C# Version

Visual Basic:
  public class PageItemCollection
  {
   private List<PageItem> mPages;
   private PageList mPageList;

   public PageItemCollection(PageList oPageList)
   {
    mPageList = oPageList;
   }

   public List<PageItem> Pages
   {
    get
    {
     if (this.mPages == null)
     {
      mPages = new List<PageItem>();
     }
     return mPages;
    }
   }

   public PageItem Add(string Text)
   {
    PageItem mItem = new PageItem(Text);
    Pages.Add(mItem);
    return mItem;
   }

   public PageItem Add(PageItem Item)
   {
    PageItem mItem = Item;
    Pages.Add(Item);
    return mItem;
   }

   public Int32 Count
   {
    get { return (Pages == null ? 0 : Pages.Count); }
   }

   public void Clear()
   {
    Pages.Clear();
   }

   public PageItem this[Int32 Index]
   {
    get { return Pages[Index]; }
   }
  }

The only real difference is I had to use this[Index] in c# where as VB I can use .Item[Index] or <object>[Index]

So In my control I have this in VB

Visual Basic:
 Private mPages As PageItemCollection

 Public ReadOnly Property Pages() As PageItemCollection
  Get
     If (mPages Is Nothing) Then
       mPages = New PageItemCollection(Me)
     End If
     Return mPages
  End Get
 End Property

So I can use it in VB like this:

<Control Object>.Pages[Index] or <Control Object>.Pages.Item[Index]

So In my control I have this in C#

Visual Basic:
  private PageItemCollection mPages;

  public PageItemCollection Pages
  {
   get
      {
       if ((mPages == null))
         {
          mPages = new PageItemCollection(this);
         }
       return mPages;
      }
  }


When I I try to access the items by index in C# I get the follow error

RegisterItem mItem = (RegisterItem)this.PageList1.Pages[this.PageList1.SelectedIndex];

Cannot apply indexing with [] to an expression of type 'MobileControls35.PageList.PageItemCollection

It all work great in VB.
 
Just copied and pasted your code into a new C# project (not the CF version however as I don't have that installed) and once I had created a couple of dummy classes for things like the PageList and PageItem everything seemed to work fine and I could happily use [] to index the objects.

Unless there is a difference between the CF and desktop versions of .Net regarding this I have no real idea where the problem could lie.
 
Back
Top