PiggyBank1974 Posted October 12, 2009 Posted October 12, 2009 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: 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 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 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# 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. Quote
Administrators PlausiblyDamp Posted October 15, 2009 Administrators Posted October 15, 2009 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. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.